reading with serial port in matlab

6 views (last 30 days)
Hello, I want to start the serial comport communication so as to read the continuous incoming data from serial comport which will be transmitting the a single sample in every 5ms uisng baudrate of 9600.The single samle will consists of 6ASCII characters(4 ASCII charcters+CR+CL) which will be transmitted in 5ms. I know there are so many available threads about this from which I found some related as here
s1 = serial('COM2', 'Baudrate', 9600, 'Terminator', 'LF');
fopen(s1);
But I don't understand how to move forward after this if I want to collect the 200 samples(1 sample in 5ms 200 makes 1 sec) and display the plot of the collected data after every 1 sec using the continuous incoming serial values.
Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Nov 2013
You should probably be using 'CR/LF' as the terminator.
Do you want to collect 200 samples exactly, or do you want to collect 1 second's worth?
If you want 200 samples, then see http://www.mathworks.com/help/instrument/bytesavailablefcn.html -- set the bytesavailablefcncount to 200*6 and the mode to byte and set bytesavailablefcn to a routine that will fread() the 200*6 bytes, decode the sequences, store the data into a cumulative buffer if required, and plot the new data.
If you want 1 second's worth, set the bytesavailablefcncount to infinity, the mode to byte, and no bytesavailablefcn callback. Then create a timer() object that fires every 1 seconds. The timer routine should grab the bytesavailablecount (no 'fcn' in that name), and fread() that many bytes, decode the sequences, store the data into a cumulative buffer if required, and plot the new data.
Either way you should be sure to set the InputBufferSize to several times one second's-worth of data, so that data can be accumulating in the buffer while the callback is processing and plotting.
Note: it will be notably more overhead to attempt to read and process each packet as it comes in. Unless you need to control something in real time, try to read several samples at once.
Note: if your serial port is "serial over usb" then you are going to encounter additional timing constraints and might not be able to sample more than 50 Hz unless the driver on the far end is smart about sending the bytes over the USB.
  20 Comments
Walter Roberson
Walter Roberson on 16 Dec 2013
I do not see anything obviously wrong. My reaction at the moment (and I haven't had any coffee yet this month) is to check the baudrate against both ends.
Stefan
Stefan on 16 Dec 2013
I am using the same baudrate on the both ends.For the hyperterminal printing I am using 115200 baudarate as shown in the image here.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!