Matlab 2 Arduino serial communication
Afficher commentaires plus anciens
I'm attempting to communicate serially with an Arduino Due while a sketch is already uploaded and running. For debugging purposes, I would like to print results to the terminal screen. I am successful when using the programming port for uploading and terminal communication and the native USB port for communication from Matlab to the Due. However, a conflict occurs when only using the programming port for both tasks. I am pretty certain that the latter is possible as all of the examples on Matlab Answers appear to use only one port (though the use of Arduino is not specified). Therefore, I attempted the same task using a Mega 2560, but got the same conflict. Any insight would be greatly appreciated!
The following is the Matlab script:
s1 = serial('COM24', 'BaudRate', 9600);
fopen(s1);
fwrite(s1, 4);
fclose(s1);
And the following is the Arduino sketch:
byte matlab_var; // for incoming serial data
void setup() {
Serial.begin(9600);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
matlab_var = Serial.read(); // read incoming data
Serial.print("Matlab = ");
Serial.println(matlab_var);
}
}
Lastly, here is the error:
Error using serial/fopen (line 72)
Open failed: Cannot connect to the COM24 port. Possible reasons are another
application is connected to the port or the port does not exist.
2 commentaires
Walter Roberson
le 7 Juil 2016
You do not mention the nature of the "conflict"
I notice that on the MATLAB side you are not reading the output of the Serial.print done by the Arudino
Christopher Richards
le 7 Juil 2016
Réponses (3)
Bahnisikha Dutta
le 14 Avr 2018
0 votes
Anyone solved this?
Tugba Kapan
le 11 Mar 2019
0 votes
I am trying to combine 2 arduino in Simulink. But i couldn't do that. How can i do ?
Raúl Silvero
le 7 Déc 2019
0 votes
When you running the code in Matlab, do you have the serial monitor of the arduino IDE open? Beacuse I think this is the problem.
Catégories
En savoir plus sur MATLAB Support Package for Arduino Hardware dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!