Matlab 2 Arduino serial communication

2 views (last 30 days)
Christopher Richards
Christopher Richards on 7 Jul 2016
Answered: Raúl Silvero on 7 Dec 2019
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
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
Christopher Richards
Christopher Richards on 7 Jul 2016
I am uncertain of the nature of the conflict, or really if it is even a conflict at all. Possibly a simple oversight on my part. The error at the end of the initial question is what led me to believe it is a conflict. The error is listed again below:
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.
As far as reading the output from the Serial.print in Matlab, I don't do this since at this point, I am unsuccessful at even communicating with the Arduino at all. Are you suggesting that this might have something to do with the issue?

Sign in to comment.

Answers (3)

Bahnisikha Dutta
Bahnisikha Dutta on 14 Apr 2018
Anyone solved this?

Tugba Kapan
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 ?

Raúl Silvero
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.

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!