Thread Subject: Serial communication-newbie

Subject: Serial communication-newbie

From: MIKE AUTHERTON

Date: 10 Feb, 2011 21:24:04

Message: 1 of 4

New to matlab and programming. Trying to interface a range sensor through serial port using matlab. The intention is to send the commands(about four binary strings with each receiving a response) through the port and read the output from the sensor continously.

My code for single command after reading matlab help

S=serial('COM1','Baudrate',9600);
fopen(S);
fwrite(S,'1.............10');
fprintf(S,'*IDN?');
out=fscanf(S);
fclose(S);
delete(S);
clear S;

but i did get the values from the sensor a few times, sometimes only 30 bits are received but mostly no data is being read. messages

A timeout occurred before the terminator was reached
and\\The specified amount of data was not returned in the time period

how to go about this????

Thanks in advance.

Subject: Serial communication-newbie

From: Trent Jarvi

Date: 11 Feb, 2011 14:57:14

Message: 2 of 4



"MIKE AUTHERTON" <satautherton@rediff.com> wrote in message
news:<ij1l1k$cm4$1@fred.mathworks.com>...

> New to matlab and programming. Trying to interface a range sensor through
> serial port using matlab. The intention is to send the commands(about four
> binary strings with each receiving a response) through the port and read
> the output from the sensor continously.

>

> My code for single command after reading matlab help

>

> S=serial('COM1','Baudrate',9600);

> fopen(S);

> fwrite(S,'1.............10');

> fprintf(S,'*IDN?');

> out=fscanf(S);

> fclose(S);

> delete(S);

> clear S;

>

> but i did get the values from the sensor a few times, sometimes only 30
> bits are received but mostly no data is being read. messages

>

> A timeout occurred before the terminator was reached

> and\\The specified amount of data was not returned in the time period

>

> how to go about this????



Hi Mike,



FSCANF is used to read terminated strings. Newline is usually the
termination character (ASCII 10). '*IDN?' is a SCPI command used to
communicate with instruments like Digital Multimeters and Oscilloscopes.
The instruments usually return the model and firmware information as a
terminated string. If a terminating character is not received, FSCANF will
continue trying to find one until a timeout occurs (10 seconds by default).



Sensors are usually fairly simple. They usually won't have a SCPI
interpreter but may respond to specific values to trigger a reading.
Sometimes they just transmit the data at regular intervals. The manual will
describe those details.



To help you get started, You will want to use FREAD to obtain the data
currently available and then try to convert that into the form you require.



while( S.BytesAvailable == 0 )

   pause(0.1);

end

out = fread(S, S.BytesAvailable);



After reading your sensor manual, you may decide to send some values first
and then wait for a specific number of bytes.

Subject: Serial communication-newbie

From: MIKE AUTHERTON

Date: 14 Feb, 2011 16:52:04

Message: 3 of 4

"Trent Jarvi" wrote in message <ij3ioa$it9$1@fred.mathworks.com>...
>
>
> "MIKE AUTHERTON" <satautherton@rediff.com> wrote in message
> news:<ij1l1k$cm4$1@fred.mathworks.com>...
>
> > New to matlab and programming. Trying to interface a range sensor through
> > serial port using matlab. The intention is to send the commands(about four
> > binary strings with each receiving a response) through the port and read
> > the output from the sensor continously.
>
> >
>
> > My code for single command after reading matlab help
>
> >
>
> > S=serial('COM1','Baudrate',9600);
>
> > fopen(S);
>
> > fwrite(S,'1.............10');
>
> > fprintf(S,'*IDN?');
>
> > out=fscanf(S);
>
> > fclose(S);
>
> > delete(S);
>
> > clear S;
>
> >
>
> > but i did get the values from the sensor a few times, sometimes only 30
> > bits are received but mostly no data is being read. messages
>
> >
>
> > A timeout occurred before the terminator was reached
>
> > and\\The specified amount of data was not returned in the time period
>
> >
>
> > how to go about this????
>
>
>
> Hi Mike,
>
>
>
> FSCANF is used to read terminated strings. Newline is usually the
> termination character (ASCII 10). '*IDN?' is a SCPI command used to
> communicate with instruments like Digital Multimeters and Oscilloscopes.
> The instruments usually return the model and firmware information as a
> terminated string. If a terminating character is not received, FSCANF will
> continue trying to find one until a timeout occurs (10 seconds by default).
>
>
>
> Sensors are usually fairly simple. They usually won't have a SCPI
> interpreter but may respond to specific values to trigger a reading.
> Sometimes they just transmit the data at regular intervals. The manual will
> describe those details.
>
>
>
> To help you get started, You will want to use FREAD to obtain the data
> currently available and then try to convert that into the form you require.
>
>
>
> while( S.BytesAvailable == 0 )
>
> pause(0.1);
>
> end
>
> out = fread(S, S.BytesAvailable);
>
>
>
> After reading your sensor manual, you may decide to send some values first
> and then wait for a specific number of bytes.
>

Hi Trent,
                             Thanks for your inputs, I did read the data out from my sensor, but once I power the sensor off and then ON, The program no longer works, Is there any way tho increase the buffer bytes more than 512?.I have to read a lot of data more than 712 bytes in a cycle.

Subject: Serial communication-newbie

From: Trent Jarvi

Date: 14 Feb, 2011 18:54:28

Message: 4 of 4


"MIKE AUTHERTON" <satautherton@rediff.com> wrote in message
news:ijbmjk$flo$1@fred.mathworks.com...
> "Trent Jarvi" wrote in message <ij3ioa$it9$1@fred.mathworks.com>...
>>
>>
>> "MIKE AUTHERTON" <satautherton@rediff.com> wrote in message
>> news:<ij1l1k$cm4$1@fred.mathworks.com>...
>>
>> > New to matlab and programming. Trying to interface a range sensor
>> > through serial port using matlab. The intention is to send the
>> > commands(about four binary strings with each receiving a response)
>> > through the port and read the output from the sensor continously.
>>
>> >
>>
>> > My code for single command after reading matlab help
>>
>> >
>>
>> > S=serial('COM1','Baudrate',9600);
>>
>> > fopen(S);
>>
>> > fwrite(S,'1.............10');
>>
>> > fprintf(S,'*IDN?');
>>
>> > out=fscanf(S);
>>
>> > fclose(S);
>>
>> > delete(S);
>>
>> > clear S;
>>
>> >
>>
>> > but i did get the values from the sensor a few times, sometimes only 30
>> > bits are received but mostly no data is being read. messages
>>
>> >
>>
>> > A timeout occurred before the terminator was reached
>>
>> > and\\The specified amount of data was not returned in the time period
>>
>> >
>>
>> > how to go about this????
>>
>>
>>
>> Hi Mike,
>>
>>
>>
>> FSCANF is used to read terminated strings. Newline is usually the
>> termination character (ASCII 10). '*IDN?' is a SCPI command used to
>> communicate with instruments like Digital Multimeters and Oscilloscopes.
>> The instruments usually return the model and firmware information as a
>> terminated string. If a terminating character is not received, FSCANF
>> will continue trying to find one until a timeout occurs (10 seconds by
>> default).
>>
>>
>>
>> Sensors are usually fairly simple. They usually won't have a SCPI
>> interpreter but may respond to specific values to trigger a reading.
>> Sometimes they just transmit the data at regular intervals. The manual
>> will describe those details.
>>
>>
>>
>> To help you get started, You will want to use FREAD to obtain the data
>> currently available and then try to convert that into the form you
>> require.
>>
>>
>>
>> while( S.BytesAvailable == 0 )
>>
>> pause(0.1);
>>
>> end
>>
>> out = fread(S, S.BytesAvailable);
>>
>>
>>
>> After reading your sensor manual, you may decide to send some values
>> first and then wait for a specific number of bytes.
>>
>
> Hi Trent,
> Thanks for your inputs, I did read the data
> out from my sensor, but once I power the sensor off and then ON, The
> program no longer works, Is there any way tho increase the buffer bytes
> more than 512?.I have to read a lot of data more than 712 bytes in a
> cycle.


Nice Mike,

It usually takes a while to get a robust solution to handle all conditions
such as power cycling the sensor.

To increase the buffer, just set the InputBufferSize while the serial port
is closed.

    fclose(S); % just needed if the port is open. You will probably set the
value prior to opening.
    set(S, 'InputBufferSize', 712);

The buffer can be set considerably higher (Mb's).

As you learn more about the serial object, you may find that the following
are handy commands.

    set(S) % display all the properties with options and defaults
    inspect(S) % graphical visualization of the properties.

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 port MIKE AUTHERTON 10 Feb, 2011 16:29:06
rssFeed for this Thread

Contact us at files@mathworks.com