MATLAB: Detect Serial Port disconnecttion

callbackpinstatusringindicatorserial

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.
s.PinStatus
% 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?
Thanks!

Best Answer

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.
Related Question