Effacer les filtres
Effacer les filtres

Serial read with Arduino only works when debugging

2 vues (au cours des 30 derniers jours)
Jake Smith
Jake Smith le 2 Avr 2019
Modifié(e) : Jake Smith le 2 Avr 2019
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.

Réponses (0)

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by