Clear Filters
Clear Filters

Problem with serialportlist: how to find/connect port being used in another application (Arduino) in MATLAB?

3 views (last 30 days)
I am using MATLAB 2020b, and my late 2018 macbook air is running on macOS Big ßur 11.1.
When I input serialportlist("available") in the command window to see if the port I need is available, it is not there -- it just shows a couple bluetooth ports.
My intended setup is as follows:
1) Arduino prints something in its serial monitor.
2) Arduino is connected to my macbook via USB cable.
2) Then I want MATLAB to read the output in the serial monitor through serialport function (e.g., readline).
(The output will be always the same -- some simple text serving as a signal for matlab.)
What should I do to make MATLAB recognize the USB connection from arduino?
Relevant part of my code is shown below:
arduinoObj = serialport("/dev/cu.usbmodem141201", 115200);
configureTerminator(arduinoObj, "LF");
Error message:
Error using serialport (line 116)
Unable to connect to the serialport device at port '/dev/cu.usbmodem141201'. Verify that a device is connected to the port, the port
is not in use, and all serialport input arguments and parameter values are supported by the device.
Error in DriftingGrating (line 65)
arduinoObj = serialport("/dev/cu.usbmodem141201", 115200);

Answers (1)

Chetan
Chetan on 29 Feb 2024
I understand that you are working to create a serial link between MATLAB and your Arduino, but it seems that MATLAB isn't picking up the USB connection.
Firstly if you define a serial port object in MATLAB using a variable name that's already in use, MATLAB will try to initiate a connection to that port again.
This action can lead to errors if the port is still open. It's essential to disconnect and delete any prior serial port objects before you attempt to establish a new connection.
If you run the following code it will throw the error:
s=serialport("COM1",9600);
% reusing the object again
s=serialport("COM1",9600);
You can follows these step for the resolution:
  • Arduino IDE Confirmation: Check that the Arduino is detected in the Arduino IDE and that its port is visible under "Tools" > "Port".
  • Port Name Verification: Ensure that the port name you're using in MATLAB (`/dev/cu.usbmodem141201`) is identical to the one displayed in the Arduino IDE.
  • Shut Down Competing Programs: Exit any other programs that might be engaging the serial port, like the Arduino IDE, to avoid interfering with MATLAB's access.
  • Remove Existing Serialport objects: If you need to make a new serial connection but have an existing serial port object, it's important to remove that object first. To do this in MATLAB, you can use the commands:
delete(instrfind); % This removes any existing serial port objects
clear s; % This clears the 's' variable if it's been set before in your workspace
s = serialport("/dev/cu.usbmodem141201", 115200); % This creates a fresh serial port object
Refer to the following MathWorks Documentation for details regarding the 'serialport':
Hope it helps

Community Treasure Hunt

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

Start Hunting!