Reading serial data continously

3 vues (au cours des 30 derniers jours)
Martin
Martin le 23 Oct 2014
Commenté : Nick le 24 Oct 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 commentaire
Nick
Nick le 24 Oct 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);

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by