Able to send/receive serial data with Serial Explorer but not a normal script?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm using MATLAB to send/receive serial data from an Arduino Mega. The Mega begins running its code when it receives a "1" from MATLAB, which I plan to do with a GUI (for now just a script). From my inital testing with just the Arduino, I got the code to start just fine using the Arduino command line, sending a "1" and reading using Serial.parseInt(). I then moved to using the Serial Explorer app on MATLAB, and could send a "1" and read back my "begin" string using the Serial.read(). (See a piece of my Arduino code below)
void loop() {
if (Serial.available()) { //read the incoming byte:
int inBuffer = Serial.read(); // read from MATLAB
//int inBuffer = Serial.parseInt(); // Command Line
if (inBuffer == 1) {
//initHoming(); //inital homing
Serial.println("begin"); //begin running cycle
homed = false;
digitalWrite(23,"HIGH"); //debug LED
Finally, I tried to recreate this in a MATLAB script, using the following code:
if ~isempty(instrfind)
fclose(instrfind);
delete (instrfind);
end
s = serialport("COM5",9600);
% Write the data 1 as uint8 using the serialport object s
write(s,1,"uint8");
n=readline(s)
This just kept giving me an error that said:
Warning: The specified amount of data was not returned within the
Timeout period for 'readline'.
'serialport' unable to read any data. For more information on
possible reasons, see serialport Read Warnings.
In addition, the script didn't turn on my debug LED from my Arduino code, confirming that it did not in fact successfully send/receive the "1" from MATLAB.
The code from my script is the exact code that the Serial Explorer had in the bottom "Code Log", and I've attached a screenshot below of it working there:
(The dummy data that I'm trying to read for my application is the integers separated by slashes.)
I feel like I'm going crazy here missing something, hopefully another set of eyes/brains can help me figure it out. Thanks!
0 commentaires
Réponses (1)
Githin George
le 7 Fév 2024
Hello Noah,
The timeout error that you are encountering may be caused due to attempting to write data to the Arduino, immediately after initializing the serial connection. To resolve this, you could introduce a brief delay after establishing the serial connection in your MATLAB script using the “pause” function. Here's a modified version of your code.
s = serialport("COM5",9600);
% Adding a slight pause to the MATLAB script so that write works without
% issue.
pause(1);
% Write the data 1 as uint8 using the serialport object s
write(s,1,"uint8");
n=readline(s);
Also make sure that the “COM” port and “BaudRate” values are the same in Arduino setup and in the MATLAB script. An example Arduino code is shown below.
void setup() {
pinMode(23, OUTPUT); // Set pin 23 as an output for the debug LED
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
// ... any other setup code ...
}
void loop() {
if (Serial.available()) { //read the incoming byte:
int inBuffer = Serial.read(); // read from MATLAB
//int inBuffer = Serial.parseInt(); // Command Line
if (inBuffer == 1) {
//initHoming(); //inital homing
Serial.println("begin"); //begin running cycle
homed = false;
digitalWrite(23,HIGH); //Slight correction in this line
}
}
}
Some of the common troubleshooting steps may also be of help. You can take a look at the following MATLAB answers post for the same.
I hope this helps.
0 commentaires
Voir également
Catégories
En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!