Slow data reading using fscanf on serial over usb

4 次查看(过去 30 天)
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 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by