Detect Serial Port disconnecttion

15 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2019-2-21
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.
  3 个评论
Joris Lambrecht
Joris Lambrecht 2021-2-17
This works the way I was hoping with serialport introduced in Matlab 2019b: the ErrorOccurredFcn gets called when a USB (virtual serial) port is unlplugged. serialport also appears to be faster than serial. In a loopback test, I measured time between data written on the srial port and the callback triggered to retrieve the same data serialport was about twice as fast. Looks like some good reasons to update old code from serial to serialport !
Walter Roberson
Walter Roberson 2021-2-17
Thanks for the update!
I had, so far, not seen any reason to use the new serialport(), only problems such as not being able to close it without deleting the object, so it is good to hear about the performance improvements.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by