How can I read the latest data at a specified frequency which is lesser than the frequency at which data is coming to a serial port?

12 visualizaciones (últimos 30 días)
I would like to know if there is any way to read the data which comes after every five seconds without having to worry about input buffer getting full.
My MATLAB code is grabbing data from the serial port which is pushed there at a rate of 10Hz(say) by a device. I would like to be able to read the latest string from the buffer every 5 seconds (say), and ignore the rest of it.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 22 de Mayo de 2012
There are four ways in which the data can be read at a specified frequency which is lesser than the frequency at which data is coming to a serial port without worrying about the input buffer getting full.
These are as given below. Option 1 is most recommended.
1) Set 'ReadAsyncMode' property to 'manual' for the serial port object.
This tells MATLAB to discard any data which arrives until you specifically ask to read data using the READASYNC command. Any read command you execute, will get whatever data arrives after that point. If data arrives infrequently, you will have to wait for the data. You can also specify a certain number of bytes to read using the READASYNC command.
One issue you will have to deal with is a partial packet, where you request to read in the middle of a packet arriving. You will need a reliable way to read at least two packets of data (and thus insuring that you will have at least one full packet regardless of when the read operation started) and be able to detect the start of the packet.
2) Only open the port when you want to receive data.
If the port is closed, data is not read. However, you could run into some issue here with how the sending device reacts to the port not being open. You could experiment with this option but it is not recommended.
3) Flush the buffer and then read
Here you would execute an FREAD to read all the bytes currently in the input buffer using the 'BytesAvailable' property of the serial port object. Then start a new read. This would be the same as option 1 but with some extra code and time on your part. Additionally, flushing the read buffer takes time and may have a noticeable impact on the acquisition rate if the rates are near or higher than 30Hz or if the input data is large. Hence option 1 is recommended over this option.
If you have the Instrument Control toolbox, you can also use the FLUSHINPUT command to flush the serial port input buffer instead of doing a read operation to flush the data.
4) Use a callback (like the TimerFcn or BytesAvailableFcn) to periodically read all the data from the serial port, find the latest complete packet received, and store that packet.
The issues that have to be dealt with here are being able to detect the beginning and end of a packet of data, and may be to store any partial packets received at the end of the input buffer to pre-prepend to the next read command. You would just need to make sure the input buffer is large enough by setting the 'InputBufferSize' property and the callback executes frequently enough to prevent loss of data. A good rule of thumb is to not plan on a callback executing more that 10 times per second. If MATLAB will be busy doing other operations, however, this is not a reliable method, because there is no way to predict how frequently the callback will be called when MATLAB is busy performing other operations.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2007b

Community Treasure Hunt

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

Start Hunting!

Translated by