Read serial port when a byte arrives

9 次查看(过去 30 天)
Hi all!
I have a question about a project i am working on; i have a bluetooth device (an evaluation board) that sends the received data via uart, and i want this data to be read in Matlab. So i just connect a usb cable between the two devices, and use fread to receive the data sent with the UART module of the board. Note that this data is organized in packets, so i want to start reading in Matlab at the beginning of one packet! The problem is that this BT device sends data almost continuously (it's data from an ADC, so a packet every 60 ms), but in Matlab i trigger the fread manually at a certain moment, when i want to start saving the data, so i can't know when a packet is starting, and usually i start reading in the MIDDLE of a packet. Is there any way to make Matlab starts the fread operation just when a packet arrives via UART? Consider that the situation is:
packet of data ---- 60 ms idle (no data)---- packet of data ---- 60 ms idle (no data)....
and so on. Thanks in advance for any help! Any other solution to synchronize the beginning of a packet with the fread is also good!

回答(1 个)

Kyle
Kyle 2016-6-14
Hi Alessandro,
I see you are interested in a way to read data from your device when it has a packet available for reading, but not when a packet is in the middle of arriving.
You can use the BytesAvailableFcn property of your interface object to read in data asynchronously. When data is available on your device for reading, a BytesAvailable event will fire, and this function will then execute. You can move the code for reading data into this function.
For more information, these doc pages may help:
I hope this helps.
- Kyle
  1 个评论
Alessandro Russo
Alessandro Russo 2016-6-21
Ok but from what i understand, this function fires when a byte is available on the serial port for example; but how can i manage to synchronize the beginning of a packet? Can you suggest me something? Thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by