MATLAB Answers

How to read COM port caption

2 views (last 30 days)
I want to connect a device with multiple COM ports at different baud rate. However, every time I change HW or USB port, the COM port changes and I have to look into the device manager and then change COM ports in my script to make it work. However, I noticed that in the device manager the COM ports have captions which don't change with COM port number. I would like to know how can I automatically select the COM ports based on their captions.

  0 Comments

Sign in to comment.

Accepted Answer

Abhishek Singh
Abhishek Singh on 5 Mar 2019
Hello Parth,
It is not possible to address the COM ports with the captions.
However as a work-around for this problem, before your actual code on your devices, you could continuously publish a particular value.
Simulink can search for these values across all COM ports and assign the COM ports accordingly.
For example, if you have an Arduino Uno and an Arduino Mega, you could publish '1' continuously from the Uno and '2' continuously from the Mega.
Simulink can sniff through all COM ports and look for a '1' or a '2', and assign the COM port to the device

  0 Comments

Sign in to comment.

More Answers (2)

Madhu Govindarajan
Madhu Govindarajan on 5 Mar 2019
seriallist command is used to detect additions to the COM port #s (only available starting R2017a). However in this technique you are assuming that no other COM port devices gets added at the time of addition and you know the order in which you are adding the devices in. If these two can be guaranteed, then seriallist command output can be saved as a variable and see how it is changing and assign COM ports to appropriate devices based on the order.

  0 Comments

Sign in to comment.


Parth-Raj Singh
Parth-Raj Singh on 21 Mar 2019
I found a solution using Python. Please find below the script:
function scpInfo=getSCPInfo
scpInfo=struct();
if isempty(seriallist)
error('SCP:InvalidSCP','No Serial Communication Port Detected.');
else
scpLis=py.serial.tools.list_ports.comports();
scpLisLen=size(scpLis,2);
if scpLisLen>0
scpInfo=repmat(struct(),[scpLisLen,1]);
for scpi=1:scpLisLen
fnSCP=sort(fieldnames(scpLis{scpi}));
for fnscpi=1:length(fnSCP)
scpInfo(scpi).(fnSCP{fnscpi})=char(scpLis{scpi}.(fnSCP{fnscpi}));
end
end
end
end
To run the above script, Python should be installed with pyserial package (pip install pyserial) and python location should be registered in the environment variable setting so that MATLAB can find it.

  0 Comments

Sign in to comment.

Products


Release

R2018b

Translated by