Comm port connection error - error fixing

2 visualizaciones (últimos 30 días)
Liam Morris
Liam Morris el 2 de En. de 2020
Comentada: sanidhyak el 12 de Mzo. de 2025
I am trying to add a line in which forces a resume when an error occurs. I am querying a piece of hardware every few seconds, so there is a lot of connections occuring. Occasionally I am getting a timeout error and when this occurs it completely stops my script from continuing. I was hoping to add something in which will essentially put NaN in the space which has the error occur, and then force the script to resume with the querying afterwards.
Thanks,

Respuestas (1)

sanidhyak
sanidhyak el 10 de Mzo. de 2025
I understand that you are trying to continuously query a hardware device through a serial port and are encountering occasional timeout errors that halt your script. You would like to handle these errors by logging "NaN" and ensuring that the script resumes querying without interruption.
The timeout error occurs due to frequent connections, which may cause the serial port to become unresponsive. To resolve this, you should implement a "try-catch" block that logs "NaN" in case of an error while ensuring the script continues execution.
Kindly refer to the following MATLAB code for the same:
while true
try
% Open serial port connection
s = serialport("COM3", 9600);
% Query hardware and read response (can change based on requirements)
writeline(s, "COMMAND");
data = readline(s);
disp("Received data: " + data);
% Close connection
clear s;
catch
% Handle error (Timeout, disconnection, etc.)
warning("Communication error occurred. Logging NaN and resuming.");
% Log NaN or take any other corrective action
data = NaN;
% Ensure the serial object is cleared
clear s;
end
% Pause before next query (adjust as needed)
pause(2);
end
The "try-catch" block ensures that any communication failure does not terminate the script but instead logs "NaN" and continues execution. Additionaly, the "clear s" command ensures that the serial object is properly released, preventing any resource locks.
For further reference on handling serial port communication errors in MATLAB, please refer:
I hope this helps!
  2 comentarios
Walter Roberson
Walter Roberson el 10 de Mzo. de 2025
I would suggest not reopening the device as long as there is no error.
% Open serial port connection
s = serialport("COM3", 9600);
while true
try
% Query hardware and read response (can change based on requirements)
writeline(s, "COMMAND");
data = readline(s);
disp("Received data: " + data);
catch
% Handle error (Timeout, disconnection, etc.)
warning("Communication error occurred. Logging NaN and resuming.");
% Log NaN or take any other corrective action
data = NaN;
% Ensure the serial object is cleared
clear s;
% reopen device
s = serialport("COM3", 9600);
end
% Pause before next query (adjust as needed)
pause(2);
end
sanidhyak
sanidhyak el 12 de Mzo. de 2025
Yes, nice!

Iniciar sesión para comentar.

Categorías

Más información sobre Communications Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by