MATLAB Answers

Warning: Unsuccessful read: A timeout occurred before the Terminator was reached. 'serial' unable to read any data.

56 views (last 30 days)
Abduljabbar Bamhel
Abduljabbar Bamhel on 25 Dec 2020
Commented: Walter Roberson on 28 Dec 2020
Hello!
I am trying to read data from Arduino Uno but i get this error "Warning: Unsuccessful read: A timeout occurred before the Terminator was reached. 'serial' unable to read any data."
Can any one help me please ?
Thank you in advance!
clear All
close All
COM = serial('COM2','BaudRate',9600);%5,'Terminator','CR');
set(COM,'TimeOut',10)
fopen(COM);
interv = 240;
init_time = 1;
x =0;
while (init_time < interv)
b = fscanf(COM) ;
b = b(2:end-2);
b = bin2dec(b);
x = [x,b];
plot(x);
grid on ;
init_time = init_time+1;
drawnow;
end
fclose(COM);
delete(COM);
clear COM;

Answers (1)

Aghamarsh Varanasi
Aghamarsh Varanasi on 28 Dec 2020
Edited: Aghamarsh Varanasi on 28 Dec 2020
Hi,
Did you try using 'serialport' function to create the serial connection, as 'serial' function is not recommended to use due to a few compatibility issues.
You can refer to this document to tranform your code to use 'serialport' function.
Also, as the issue seems to be with timeout, you can check with two changes:
  1. Check if the input from Arduino actually has the Terminator 'CR'
  2. Increasing the timeout period
  1 Comment
Walter Roberson
Walter Roberson on 28 Dec 2020
The compatibility considerations link just come down to saying not to use fopen() and fclose() on serialport() objects. Nothing in that link indicates why serialport is better, faster, more flexible, fewer bugs, than serial(). Meanwhile there are things that serialport() cannot do that could be done with serial(). Such as fclose() a port without having to destroy the object and recreate it. (Think performance!!)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by