Reading serial data continously

1 次查看(过去 30 天)
Martin
Martin 2014-10-23
评论: Nick 2014-10-24
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 个评论
Nick
Nick 2014-10-24
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);

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile 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!

Translated by