Code hangs while writing on a SerialPort

조회 수: 23(최근 30일)
Mario Silvagni
Mario Silvagni 2020년 9월 3일
댓글: Mario Silvagni 2020년 9월 8일
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);
This is the write commmand:
catch e
disp('Error Writing Serial SerConn')
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
data = varargin{1};
write(obj.StringClient, data);
% Wait for the write to be complete
waitfor(obj, "WriteComplete", true); %% HANGS HERE
catch ex
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?

채택된 답변

SUN ZEQING 2020년 9월 8일
편집: SUN ZEQING 2020년 9월 8일
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
Mario Silvagni
Mario Silvagni 2020년 9월 8일
same am mine. Problem using embedded Arduino USB connection (fdti?) or using a TTL serial + Serial2Usb converter

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Mario Silvagni
Mario Silvagni 2020년 9월 8일
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)
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!

Translated by