Code hangs while writing on a SerialPort
20 views (last 30 days)
Show older comments
Mario Silvagni
on 3 Sep 2020
Commented: Mario Silvagni
on 8 Sep 2020
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?
0 Comments
Accepted Answer
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
on 8 Sep 2020
I'm using Ardinuo Mega 2560. It is connected to PC through USB emulating a serial port.
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!