Serial read with Arduino only works when debugging

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:
  1. Trying a variety of "Timeout" settings in serial from 0.5 up to 5.
  2. Trying a variety of "Baudrate" settings in serial from 4800 to 19200.
  3. Verified correct COM port for MATLAB and the Arduino.
  4. Verified both MATLAB and the Arduino have the same Baudrate.
  5. Tried with a different Arduino Pro Mini and an Arduino Uno with similar results.
  6. Tried "Serial.print" instead of "Serial.write".
  7. Tried "fprintf" instead of "fwrite".
  8. 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개)

카테고리

도움말 센터File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

제품

릴리스

R2017b

태그

질문:

2019년 4월 2일

편집:

2019년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by