Slow data reading using fscanf on serial over usb
2 ビュー (過去 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;
0 件のコメント
回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で MATLAB Support Package for Arduino Hardware についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!