Serial read with Arduino only works when debugging
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm attempting to receive a simple echo from an Arduino Pro Mini connected via USB, but receiving Timeout errors with no response:
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period..
MATLAB code:
%Barebones serial testing
clear;
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
s = serial('com5', 'Baudrate', 9600, 'Timeout', 1);
fopen(s);
scommand = 12;
fscan(s,scommand);
packet = uint8(fread(s,1,'uint8'));
fclose(s);
Arduino code:
unsigned char temp;
void setup() {
Serial.begin(9600);
while(!Serial){
delay(1);
}
}
void loop() {
while (Serial.available()>0){
temp = Serial.read();
Serial.write(temp);
}
}
But, if I activate a debug flag at:
fscan(s,scommand);
and execute line by line it works fine. Things I attempted/checked:
- Trying a variety of "Timeout" settings in serial from 0.5 up to 5.
- Trying a variety of "Baudrate" settings in serial from 4800 to 19200.
- Verified correct COM port for MATLAB and the Arduino.
- Verified both MATLAB and the Arduino have the same Baudrate.
- Tried with a different Arduino Pro Mini and an Arduino Uno with similar results.
- Tried "Serial.print" instead of "Serial.write".
- Tried "fprintf" instead of "fwrite".
- Tried "packet = fscanf(s);" instead of "packet = uint8(fread(s,1,'uint8')".
Are there any glaringly obivous answers to the Timeout error?
Thanks in advance.
댓글 수: 0
답변 (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!