Code hangs while writing on a SerialPort

27 views (last 30 days)
I had a code running for 24/24 7/7 for almost a month. Now the same code is no loger working properly with no apparent change (I was also using a compiled version).
Matlab (2020a last updated) GUI is communicationg (serial port by a USB converter) with and Arduino witha custom protocol: sending (from GUI) 32 byte each second (triggered by a timer object in the gui) and receving 80 byte, twice each second (reading with a callback triggered by the terminator)
This is the serial configuration:
app.serConn = serialport(app.SelectedSerialPort,115200);
app.serConn.Timeout=1;
configureTerminator(app.serConn,"CR/LF");
configureCallback(app.serConn,"terminator",@(src,event,DataStorage)serConn_instrcallback(app,event));
flush(app.serConn);
This is the write commmand:
try
writeline(app.serConn,Serial_Send_Data);
catch e
disp(e.message);
disp('Error Writing Serial SerConn')
end
it was working properly now (after some working cycles, please note remote connection to PC make things worse) the system hangs
Pressing ^C the error show that is locked in the Serialport.m function
here at the wait for
try
data = varargin{1};
write(obj.StringClient, data);
% Wait for the write to be complete
waitfor(obj, "WriteComplete", true); %% HANGS HERE
catch ex
throwAsCaller(ex);
end
I have no choiche to catch the error just the ^C showing the problem
Commenting the waitfor command system works properly
Where am I wrong? How can avoid hanging or manage the problem?

Accepted Answer

SUN ZEQING
SUN ZEQING on 8 Sep 2020
Edited: SUN ZEQING on 8 Sep 2020
Got the same problem after 2000 times of sucess writeline(), hanging at the waitfor() function without throwing any error.
It is found that, for Matlab 2019b, there is no waitfor(obj, "WriteComplete", true) line in the SerialPort.m file and thus does not has this issue.
  3 Comments
SUN ZEQING
SUN ZEQING on 8 Sep 2020
I'm using Ardinuo Mega 2560. It is connected to PC through USB emulating a serial port.
Mario Silvagni
Mario Silvagni on 8 Sep 2020
same am mine. Problem using embedded Arduino USB connection (fdti?) or using a TTL serial + Serial2Usb converter

Sign in to comment.

More Answers (1)

Mario Silvagni
Mario Silvagni on 8 Sep 2020
Little Update.
Tested on a new laptop (always with same prolific usb-serial convert) device driver form July 2020.
The program run for almost 24 hour, without ANY modification in the matlab serialport.m (as is provided by installation)
Got:
Unable to detect connection to the serialport device. Ensure that the device is plugged in and create a new serialport object.
Error writing String.
Invalid operation. Object must be connected to the serial port.
Error Writing Serial SerConn
Error writing String.
[...]
and the programs stops
Changed driver (old working prolific driver working fine)...
Same error after a couple of hours....
Thinking is something related to the usage of USB adapter... (i need using 3 connected to the pc)

Community Treasure Hunt

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

Start Hunting!