Serialport timeout doesn't work

13 views (last 30 days)
Martin Jurik
Martin Jurik on 29 Jan 2024
Commented: Hassaan on 4 Feb 2024
Hi,
I am trying to scan my COM ports, ask each of them something like "who are you?" and in case the serial port understands the question, it answers with it's name, version, etc. During the scanning I am having an issue opening some COM ports. Based on the Windows hardware manager I found that the problematic port is "Standard Serial over Bluetooth link" (see img below). It completely stops the MATLAB during the writeline function. Since it stops reacting on any input (Ctrl+C, "Stop" btn) I have to kill it from task manager.
I tried to add "Timeout" but it doesn't help at all. It looks like MATLAB bug because even the hardware doesn't work/doesn't respond/responds random stuff, it should not kill the MATLAB. May I ask to help, please?
Simplified code is:
ports = serialportlist;
for i = 1:numel(ports)
try
conn = COMConnection.OpenCOMPort(ports(i));
fprintf('Writing\n')
writeline(conn,'?');
fprintf('Pause\n')
pause(0.05);
fprintf('Reading\n')
name = read(conn, conn.NumBytesAvailable, "char");
%...
catch ex
exText = getReport(ex);
disp(exText);
fprintf('Port %s is unavailable\n', ports(i));
end
end
function [connection] = OpenCOMPort(port)
fprintf('Opening %s\n', port)
baudRate = 115200;
EOL_format = "CR/LF";
connection = serialport(port, baudRate, "Timeout", 1);
configureTerminator(connection, EOL_format);
end
What I get in a cmd window is:
ports =
1×5 string array
"COM3" "COM6" "COM8" "COM9" "COM10"
Opening COM3
Writing
Pause
Reading
Error using COMConnection.DetectHW
Expected input number 2, count, to be nonzero.
Port COM3 is unavailable.
Opening COM6
Error using serialport
Unable to connect to the serialport device at port 'COM6'.
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.
See related documentation for troubleshooting steps.
Error in COMConnection.OpenCOMPort (line 26)
connection = serialport(port, baudRate, "Timeout", 1);
Error in COMConnection.DetectHW (line 44)
conn = COMConnection.OpenCOMPort(ports(i));
Port COM6 is unavailable.
Opening COM8
Error using serialport
Unable to connect to the serialport device at port 'COM8'.
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.
See related documentation for troubleshooting steps.
Error in COMConnection.OpenCOMPort (line 26)
connection = serialport(port, baudRate, "Timeout", 1);
Error in COMConnection.DetectHW (line 44)
conn = COMConnection.OpenCOMPort(ports(i));
Port COM8 is unavailable.
Opening COM9
Writing
% <--- here it completely stops responding and I have to kill it
Just to make the report complete, here is the screenshot of COM ports. There is nothing interesting to connect to now, but it should not kill MATLAB.
Thank you,
Martin

Answers (2)

Hassaan
Hassaan on 29 Jan 2024
Edited: Hassaan on 29 Jan 2024
One of the possible solution maybe to ignore the undesired COM ports. Use this in MATLAB to query the Available COM ports with there full-name and COM number. And you can then implement some sort of filter to ignore the COM's with 'Standard Serial Over Bluetooth link'. Works on windows os only. For linux you may need to use different shell command.
[status, cmdout] = system('powershell "Get-WmiObject -Query ''SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0 AND PNPClass = ''''Ports'''''' | Select-Object Name, DeviceID"');
disp(cmdout)
Output
Name DeviceID
---- --------
USB-SERIAL CH340 (COM14) USB\VID_1226&PID_7523\6&232D61C&0&3
Communications Port (COM1) ACPI\PNP0501\2
USB-SERIAL CH340 (COM7) USB\VID_1A2&PID_7523\7&2FB24610&2
A initial Idea:
% Execute PowerShell command to get COM port info
[status, cmdout] = system('powershell "Get-WmiObject -Query ''SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0'' | Where-Object { $_.Name -match ''(COM\d+)'' } | Select-Object Name"');
% Check for successful command execution
if status == 0
% Split the output into lines
lines = strsplit(cmdout, '\n');
% Initialize an array to hold the filtered COM port names
filteredPorts = {};
% Loop through each line of the output
for i = 1:length(lines)
% Check if the line contains a COM port and does not contain the Bluetooth link text
if contains(lines{i}, 'COM') && ~contains(lines{i}, 'Standard Serial over Bluetooth link')
% Extract the COM port name
portName = strtrim(lines{i});
% Add to the list of filtered ports
filteredPorts{end+1} = portName; % Store the name
end
end
% Display the filtered COM ports
disp('Filtered COM Ports:')
disp(filteredPorts)
else
disp('Failed to query COM ports.')
end
Output
Filtered COM Ports:
{'USB-SERIAL CH340 (COM14)'} {'Communications Port (COM1)'} {'USB-SERIAL CH340 (COM7)'}
To return only COMs:
% Execute PowerShell command to get COM port info
[status, cmdout] = system('powershell -Command "Get-PnpDevice -PresentOnly | Where-Object { $_.Class -eq ''Ports'' } | Select-Object Name, DeviceID | Format-Table -HideTableHeaders | Out-String -Width 4096"');
% Check for successful command execution
if status == 0
% Split the output into lines
lines = strsplit(cmdout, '\n');
% Initialize a cell array to hold the COM port names
comPorts = {};
% Loop through each line of the output
for i = 1:length(lines)
line = strtrim(lines{i});
% Skip empty lines
if isempty(line)
continue;
end
% Check if the line contains 'COM' and does not contain 'Standard Serial over Bluetooth link'
if contains(line, 'COM') && ~contains(line, 'Standard Serial over Bluetooth link')
% Extract the COM port name
tokens = regexp(line, 'COM\d+', 'match');
if ~isempty(tokens)
comPorts{end+1} = tokens{1}; % Add the COM port name to the cell array
end
end
end
% Display the filtered COM ports
disp('Filtered COM Ports:');
disp(comPorts);
else
disp('Failed to query COM ports.');
end
Output:
Filtered COM Ports:
{'COM14'} {'COM1'} {'COM7'}
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 Comments
Martin Jurik
Martin Jurik on 30 Jan 2024
Thank you for response. I agree that this solution (filter out the ports which causes troubles) might work and is a quick fix.
Nevertheless, I think that this issue should be solved by MathWorks team directly in the MATLAB. Because this solution is strictly platform dependent. These troubles have many of my colleagues (also Linux and Mac). MATLAB has nice feature that it's platform independent and these hacks would break this major feature.
Hassaan
Hassaan on 30 Jan 2024
Edited: Hassaan on 30 Jan 2024
@Martin Jurik I agree. Anyhow, the fix can be replicated to Linux/MAC platform also.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Sign in to comment.


Hassaan
Hassaan on 30 Jan 2024
@Martin Jurik Try this.
% Assuming you have Parallel Computing Toolbox installed
ports = serialportlist("available");
timeoutDuration = 5; % timeout in seconds
for i = 1:numel(ports)
try
% Open the COM port
conn = serialport(ports(i), 115200, "Timeout", 1);
configureTerminator(conn, "CR/LF");
% Create a future object for the write operation
f = parfeval(@writeToPort, 0, conn, '?'); % function handle to writeToPort
% Wait for the write operation to complete or to time out
success = waitforFuture(f, timeoutDuration);
if success
fprintf('Data written to %s\n', ports(i));
% Read from the port or other operations
else
fprintf('Timeout occurred on %s\n', ports(i));
end
% Close the port if it's no longer needed
delete(conn);
catch ex
disp(getReport(ex));
end
end
function success = waitforFuture(f, timeoutDuration)
% Wait for the specified duration
waitfor(f, 'finished', timeoutDuration);
if strcmp(f.State, 'finished')
success = true;
else
% Cancel the future if it's still running
cancel(f);
success = false;
end
end
function writeToPort(conn, message)
% Attempt to write to the port
writeline(conn, message);
end
parfeval is used to execute the writeToPort function asynchronously. waitforFuture is a custom function that waits for the write operation to complete with a specified timeout. If the operation does not complete within the timeout, the future is canceled.
Please note that this example assumes you have the Parallel Computing Toolbox installed. If you don't have this toolbox, this approach won't work as MATLAB doesn't have built-in asynchronous execution capabilities outside of this toolbox.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 Comments
Martin Jurik
Martin Jurik on 1 Feb 2024
I had to separate the code into separate files otherwise it was complaining. I also had to change one line of code to
waitfor(f.State, 'finished', timeoutDuration); % <--- the "State" was missing there
It does not freeze the MATLAB anymore which is great. I'm just not sure if it does not create some buggy processes which will cause the troubles later.
Thank you for your solution. I'm the owner of the Parallel C. toolbox. But funny thing is that for correct work with serial port, I have to pay 1.120 Eur. Well....
Every time I try to save any script now I get this error in the command window... So the MATLAB restart is needed in the end again.
Error using assert
Unable to find containing attached file for:
... tmp.m
Error in parallel.internal.pool.FileManager/getContainingAttachedFile (line 465)
assert(false, "Unable to find containing attached file for: " + file);
Error in parallel.internal.pool.FileManager/findSpecificChangedFiles (line 395)
containingAttachedFile = obj.getContainingAttachedFile(attachedFile);
Error in parallel.internal.pool.FileManager/maybeUpdateAttachedFiles (line 112)
changedFilesMap = obj.findSpecificChangedFiles(filesToUpdate);
Error in parallel.internal.pool.maybeUpdateAttachedFiles (line 22)
aPool.hGetFileManager().maybeUpdateAttachedFiles(files, ASYNC_WAIT);
Hassaan
Hassaan on 4 Feb 2024
This issue might be unrelated to your serial communication script directly but could be a symptom of how MATLAB is managing (or failing to manage) files in its parallel execution environment.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!