Filter löschen
Filter löschen

Reading serial data continously

1 Ansicht (letzte 30 Tage)
Martin
Martin am 23 Okt. 2014
Kommentiert: Nick am 24 Okt. 2014
Hello,
I'm trying to read serial data from an Arduino with a sample time of about 50ms. I have configured a simple serial port and have no problem in communicating single packets either way.
But when I try to put my serial read in a loop it timeouts every 10th or so read. If I have timeout at 10 seconds the 11th read is successful (until about the 20th). If the timeout is at 1 second the 11th to 19th read is failed and the 20th to 29th is successfull and so on. So it seems to have to do with time which is very strange.
The code in it's simplest form:
function [s] = Arduino_serial_0_3(comPort)
s = serial(comPort);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
set(s,'Timeout',1);
set(s, 'terminator', 'LF'); % define the terminator for println
fopen(s);
for i = 1:20
pause(0.05);
input = fscanf(s,'%s');
disp(input);
end
fclose(s);
delete(s);
end
I have tried fread as well with similar results.
Please advise.
  1 Kommentar
Nick
Nick am 24 Okt. 2014
I tried the code you gave and it looped 20 times everytime for me. My code (arduino) in the void loop was simply this though
Serial.println("Hi"); delay(50);

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by