Reading from serial port

조회 수: 15 (최근 30일)
Akash
Akash 2011년 2월 3일
Hi, I need to interface MATLAB with a COM port to read data from sensors. Here is my MATLAB code.
s = serial('COM5');
s.BaudRate = 115200;
fopen(s);
tic
for i = 1:25
an = fscanf(s,'%c');
end
toc
fclose(s);
I have used "tic" "toc" for measuring execution time to read 25 values and its almost 5 seconds. This is pretty slow. 115200 is the max Baud Rate.
Is there a better/faster way to read from a serial port???
Thanks all

답변 (1개)

Ankit Desai
Ankit Desai 2011년 2월 9일
Akash,
You might be getting the data slowly. The delay might be caused by fscanf waiting for data to be available. As listed in the documentation - fscanf waits for data to be available until a timeout occurs.
You can confirm the same by checking that there 50 bytes available (25 bytes for character values + 25 bytes for terminator values) before you call the fscanf(s) in the for loop.
You can do so by checking the BytesAvailable field of the object.
s.BytesAvailable
I tried the same and was able to do so in < 1 second.
Hope this helps,
-Ankit

카테고리

Help CenterFile Exchange에서 External Language Interfaces에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by