Slow data reading using fscanf on serial over usb

조회 수: 2 (최근 30일)
Tillmann Spellauge
Tillmann Spellauge 2019년 1월 5일
댓글: Tillmann Spellauge 2019년 1월 10일
Hi everybody,
I have set up a serial connection over USB between Matlab and and an ESP8266 running a simple Arduino sketch.
The arduino sketch simply switches a LED on or off, when i sent it a 2 or 1 via serial respectively. After switching the LED it echoes back the character I send.
Using the Matlab code below, I was able to establish a working connection, however it takes Matlab slightly more than one second to read the data sent by the ESP8266.
This seems quite axcassive considering the relatively high boud rate of 460800 and the fact that i only read one byte of data. When using the Arduino Serial Monitor to do the same task i get a response almost instantly, just as expected.
Dose someone have a solution for this, or any ideas how to solve the problem?
Port = 'COM';
BaudRate = 460800;
comPort = serial(Port, 'BaudRate', BaudRate);
fopen(arduinoCom);
for ii = 1:1:5
fwrite(comPort, '1');
tic
A = fscanf(comPort,'%u',1);
toc
fwrite(comPort, '2');
tic
B = fscanf(comPort,'%u',1);
toc
end
fclose(comPort);
delete(instrfindall);
clear comPort;

답변 (1개)

Vishal Bhutani
Vishal Bhutani 2019년 1월 8일
The issue might be happening because writing data to your instrument takes a finite amount of time. When you do a read immediately after the write operation, the write operation might not have been complete so that is creating some delay.
  댓글 수: 1
Tillmann Spellauge
Tillmann Spellauge 2019년 1월 10일
Hi Vishal,
thanks for your answer.
The problem was, that I wasn't sending the correct terminator to my arduino, resulting in the arduino running into a timeout. The problem was solved by setting the terminator correctly and using fprintf(); instead of fwrite();.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by