Thread Subject: Serial Communication

Subject: Serial Communication

From: steve smith

Date: 30 Sep, 2008 15:42:01

Message: 1 of 5

I am attempting to communicate with a device via a 9pin through serial connection. The device is a syringe pumping apparatus. When configured according to the manufacturer's instructions through hyperterminal, the apparatus can understand ASCI string data such as 'RUN', 'REV', and 'STP'as well as general config data like inputting syringe diameter size and discharge rate.

When I try to operate the apparatus through MatLab, however, it fails to respond. I create a serial port using the s=serial('COM1') command and open the port in MAtLab. I configure the baudrate, # of stop bits, etc using MatLab. I then use the fwrite() command. I attempt to write the string fwrite(s,'RUN'); or fwrite(s,'REV') to the device with nothing occurring. (i.e., I am attempting to send the string 'RUN' or 'REV' to the device).

The device is also capable of transmitting its current status, i.e., if it is stopped or running. How do I accomplish this? Do I create a for loop in order for MatLab to continuously read information from the device and report its status?

Subject: Serial Communication

From: David

Date: 30 Sep, 2008 15:59:04

Message: 2 of 5

"steve smith" <stevesmith0518@comcast.net> wrote in message <gbthc9$8kj$1@fred.mathworks.com>...
> I am attempting to communicate with a device via a 9pin through serial connection. The device is a syringe pumping apparatus. When configured according to the manufacturer's instructions through hyperterminal, the apparatus can understand ASCI string data such as 'RUN', 'REV', and 'STP'as well as general config data like inputting syringe diameter size and discharge rate.
>
> When I try to operate the apparatus through MatLab, however, it fails to respond. I create a serial port using the s=serial('COM1') command and open the port in MAtLab. I configure the baudrate, # of stop bits, etc using MatLab. I then use the fwrite() command. I attempt to write the string fwrite(s,'RUN'); or fwrite(s,'REV') to the device with nothing occurring. (i.e., I am attempting to send the string 'RUN' or 'REV' to the device).
>
> The device is also capable of transmitting its current status, i.e., if it is stopped or running. How do I accomplish this? Do I create a for loop in order for MatLab to continuously read information from the device and report its status?
>

when you commanded the devide through hyperterminal did it reply before you hit enter? if not then you probably have to add \n or \r or some combination of those to the end of your fwrite string. also, what precision are you specifying for fwrite? to send characters you should specify 'uchar' as the precision, otherwise i don't know what it might default to.

Subject: Serial Communication

From: Sven Schroeder

Date: 30 Sep, 2008 16:09:02

Message: 3 of 5

Hi,

When I communicate with a microscope camera, I use this simple code to change status and get status reports:

s=serial('COM1');
set(s,'BaudRate',9600);
fopen(s);
fprintf(s, 'Illum 100'); %Illum 100 sets the Illumination to 100%

fscanf(s) %my camera answers with "Illum 100"

Hope this helps!

Subject: Serial Communication

From: Walter Roberson

Date: 30 Sep, 2008 17:00:50

Message: 4 of 5

steve smith wrote:

> When I try to operate the apparatus through MatLab, however, it fails to respond.
> I create a serial port using the s=serial('COM1') command and open the port in MAtLab.
> I configure the baudrate, # of stop bits, etc using MatLab.

What do you configure the flow control as? It might require that flow control
be set to hardware. And I'd want to double-check exactly which line terminator
character it uses -- CR only, CR LF, LF only ?

Are you able to monitor the apparatus serial communications at all, even if just
via a blinking light, to see whether the data you are sending is getting out
the PC serial port?

Subject: Serial Communication

From: Ankit Desai

Date: 1 Oct, 2008 00:09:01

Message: 5 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <KWsEk.27$BT1.24@newsfe04.iad>...
> steve smith wrote:
>
> > When I try to operate the apparatus through MatLab, however, it fails to respond.
> > I create a serial port using the s=serial('COM1') command and open the port in MAtLab.
> > I configure the baudrate, # of stop bits, etc using MatLab.
>
> What do you configure the flow control as? It might require that flow control
> be set to hardware. And I'd want to double-check exactly which line terminator
> character it uses -- CR only, CR LF, LF only ?
>
> Are you able to monitor the apparatus serial communications at all, even if just
> via a blinking light, to see whether the data you are sending is getting out
> the PC serial port?

Hi Steve,

Try using fprintf instead of fwrite (after checking flowcontrol, parity and terminator settings on the device). fprintf sends the ASCI text followed by the terminator which the fwrite does not send by default.

Similarly for reading data back, if you expect ASCI/string response, fscanf is what you can use.

help fprintf
help fscanf

should give you more details.

However I agree with comments above that checking that settings on instrument/device matches that of the MATLAB object should be the first test.

Hope this helps

-Ankit

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
serial ehab ibrahim 1 Oct, 2008 11:35:08
serial Ankit Desai 30 Sep, 2008 20:10:20
fread steve smith 30 Sep, 2008 11:45:05
fwrite steve smith 30 Sep, 2008 11:45:05
fscanf steve smith 30 Sep, 2008 11:45:05
communication steve smith 30 Sep, 2008 11:45:05
serial steve smith 30 Sep, 2008 11:45:05
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com