Read serial port when a byte arrives

5 views (last 30 days)
Alessandro Russo
Alessandro Russo on 13 Jun 2016
Commented: Alessandro Russo on 21 Jun 2016
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!

Answers (1)

Kyle
Kyle on 14 Jun 2016
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 Comment
Alessandro Russo
Alessandro Russo on 21 Jun 2016
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!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!