MATLAB Answers

Dynamic input buffer size of a serial communication

62 ビュー (過去 30 日間)
Martin Maier
Martin Maier 2020 年 3 月 12 日
回答済み: Ameer Hamza 2020 年 3 月 12 日
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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 12 日
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.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by