Read Data from Serial Device

5 visualizaciones (últimos 30 días)
Doug
Doug el 27 de En. de 2011
Hi,
I've got a serial device that outputs data once a second in the in following form: L,00002,00220, N,072, A,000, U,000, T,000, I,000, V,000, E,000,. I'm trying to design a GUI that will display the values in a readable format to an operator. The problem I am having is when I ask MATLAB to read in the data it often doesn't or the data is not complete.
I've included my code for the serial I/O below. Can anyone provide me with help or suggestions?
Thank you!
clear all
s = serial('COM1', 'BaudRate', 19200, 'DataBits', 8, 'Parity', 'none', 'StopBits', 1, 'FlowControl', 'none');
fopen(s);
a=fgetl(s);
fclose(s);
delete(s)
clear s

Respuestas (3)

Vieniava
Vieniava el 27 de En. de 2011
If your line length is constant you could try something like that:
s.BytesAvailableFcnCount = 40;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn = @instrcallback;

Walter Roberson
Walter Roberson el 27 de En. de 2011
It is not recommended to use flowcontrol none with RS232 above 4800 baud. If your cables are a reasonable (short) length you may get away with it at 9600, but by 19200 you will usually be losing characters. Unless you have a good reason not to, I recommend switching to flowcontrol 'hardware'.
  1 comentario
Doug
Doug el 27 de En. de 2011
I am using a custom controller that I am not able to modify, so I'm stuck with the 19200 without flow control. I've been using Hyper Terminal to monitor the device, and I haven't seen any data loss. Even though it isn’t the most ideal setup I’m stuck with it, and so far it’s worked fine.

Iniciar sesión para comentar.


Siddharth Shankar
Siddharth Shankar el 28 de En. de 2011
Douglas,
I would recommend adding a call to FLUSHINPUT (if you are using the Instrument Control Toolbox) as soon as you FOPEN the serial port before reading data in.
I would also second the suggestion by "Vieniava" to use a BytesAvailableFcn callback to read your data as and when X number of bytes become available.
It would also be better to use FREAD instead of FGETL since with FGETL, you are assuming that the input data stream has a terminator character (which it may or may not, you would know by looking at the output data specification of the device) and that this terminator happens to match the one that your SERIAL object is expecting (look at the terminator property of your object). With FREAD, you are making no such assumptions and you are reading raw binary data. You can choose to read the number of bytes that you expect, or you have the flexibility of checking the BytesAvailable property before reading the data with FREAD.
If you are (as you mentioned) able to receive all the data when using HyperTerminal, then it is most likely that something is amiss in the way the serial object is configured, or the way the data is being read in.

Categorías

Más información sobre Tall Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by