Reading serial data continously
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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 Center 및 File 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!