Slow data reading using fscanf on serial over usb

3 views (last 30 days)
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;

Answers (1)

Vishal Bhutani
Vishal Bhutani on 8 Jan 2019
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 Comment
Tillmann Spellauge
Tillmann Spellauge on 10 Jan 2019
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();.

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!