Dynamic input buffer size of a serial communication

10 views (last 30 days)
Hi all,
I'm trying to read out data from a sensor via RS485 communication. But the problem here is that the data array send from the sensor can vary.
Is it possible to set the inputbuffer or the fread() function to a dynamic input size?
Thank you and nice greetings,
Martin

Accepted Answer

Ameer Hamza
Ameer Hamza on 12 Mar 2020
fread() already allows specifying the number of bytes. For serial object, the input buffer size is a double value: https://www.mathworks.com/help/matlab/ref/serial-properties.html#mw_5d33f1bd-97a2-4474-b2ea-8499775ddce4 it needs to be constant. You can try using BytesAvailableFcn: https://www.mathworks.com/help/matlab/matlab_external/bytesavailablefcn.html callback function and load the data from the serial buffer to the base workspace (for example, using a global variable) every time the number of bytes in the serial buffer reaches a specified number.
However, starting from R2019b, MATLAB has introduced a function serialport: https://www.mathworks.com/help/matlab/ref/serialport.html which does not require to specify the size of the input buffer and handle it in the background.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!