Serial read with Arduino only works when debugging
1 回表示 (過去 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!