Read serial port when a byte arrives

조회 수: 1 (최근 30일)
Alessandro Russo
Alessandro Russo 2016년 6월 13일
댓글: Alessandro Russo 2016년 6월 21일
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에서 Data Acquisition Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by