Clear Filters
Clear Filters

Serial read with Arduino only works when debugging

2 views (last 30 days)
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.

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!