MATLAB Answers

Detect Serial Port disconnecttion

11 views (last 30 days)
I'm trying to detect if a serial port disconnect even has occured.
Neither the BreakInterruptFcn or ErrorFcn are beign tripped when my device (a USB device thats hows up as virtual serial port) is unplugged.
When I am using the device with a simple terminal program (RealTerm) I can see that the RingIndicator lights up when I unplug it. In Matlab there is a PinStatusFcn, but when I try to change the PinStatus struct I get the following error whether the port is open or closed.
% ans =
% struct with fields:
% CarrierDetect: 'off'
% ClearToSend: 'on'
% DataSetReady: 'on'
% RingIndicator: 'off'
s.PinStatus.RingIndicator = 'on'
%Error using serial/subsasgn (line 146)
%Changing the 'PinStatus' property of serial port objects is not allowed.
Is there a way to get the Ring Indication in Matlab?


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Feb 2019
The only pin statuses that can be changed are DTR and RTS
RingIndicator is read only, intended to show whether there is currently a pulse on the Ring Indicator pin (which is often not connected, and would be at best emulated on a USB to serial cable.)
RingIndicator is not really asserted (or not) when a USB connection is connected or disconnected. RS232 does not offer any method of detecting connection / disconnection. What can be said is that on a true RS232 system, CTS would be off during disconnect.
RS232 signals such as RTS are carried as a leading bitfield on each packet for a USB device marked as serial emulation, and the driver is responsible for latching them for query by the application. What I do not know is if there is any particular timeout specified in the USB standards: how long after disconnect will the driver continue to latch the signal?
Actual USB connect / disconnect events cannot be detected at the RS232 emulation level: you need to talk to the host controller to receive those, and MATLAB does not offer any direct method of talking to host controllers. On MS Windows systems you just might be able to find an ActiveX control that is willing to intermediate.

  1 Comment

Joris Lambrecht
Joris Lambrecht on 22 Feb 2019
OK, I misunderstood what PinStatus struct did. I thought it affected which of the 4 pins would cause the PinStatusFcn to be called if that pin changed.
I was able to trip the callback using a real serial port, but wasn't able to with the USB virtual serial port. At this point it's not worth the effort for me to show the disconnect event. I was hoping it would be easy beacause in my terminal program the RI reliably lights up every time I disconnect
Thanks for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!