Matlab 2 Arduino serial communication
2 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 7 Jul 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
Answers (3)
Tugba Kapan
on 11 Mar 2019
I am trying to combine 2 arduino in Simulink. But i couldn't do that. How can i do ?
0 Comments
Raúl Silvero
on 7 Dec 2019
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.
0 Comments
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!