Question

In .Net's SerialPort object, is there a way to determine if a serial port gets unplugged or plugged in.

Was it helpful?

Solution

Unlike USB, the serial port does not have any built-in way to detect a physical change in link status. A limited form of "device ready/not ready" signalling can be done using some of the pins (namely DTR, DSR, and sometimes DCD), but this doesn't seem like exactly what you're looking for (it's not built in to RS232 -- the device must support it, you mainly use it to talk to modems.)

So, in short: no, in the general case. If you know/can program the device you're trying to communicate with, and you know that it will hold a certain line high (for example), you could poll it looking for that line to go high. But if you plug in a device which isn't programmed to do something predictable like that, then there's really no way to tell. (Some devices may hold DSR high by default but it's in no way a sure bet.)

OTHER TIPS

Most serial devices have some type of ack response to a query. just send a simple query and wait for response. If you don't get it, the device is not there or at least not responding.

I haven't tried it, but look at the SerialPort.PinChanged event and DsrChanged.

Whenever there's any normal device plugged in to the serial port and switched on, then I'd expect the port's DSR pin to be asserted; and conversely if that device is unplugged, or when it's switched off, then I'd expect the DSR pin state to change/drop.


The usual meaning of the various pins is:

  • DSR: device is plugged in and switched on
  • CTS: device is ready to receive data (this may be down even when a device is plugged in, e.g. when the device has a limited on-board buffer and uses this pin to flow-control data transfer from the PC)
  • DCD: device (modem) has established a connection over the phone line to another modem (so anything you send is treated as data to be transferred to the remote modem)

Of these, the one that answers your OP is DSR.

There is a way to detect Serial Port removal/insertion in .NET - see my answer at Detect serial port insertion/removal

    Public Class SerialPort
        Inherits IO.Ports.SerialPort

        Event Disconnected()

        Public Sub OpenWithDisconnectionevent()
            Me.Open()
            Dim t As New Threading.Thread(AddressOf ConnectivityChecker)
            t.Start()
        End Sub


        Sub ConnectivityChecker()
            Do
                If Me.IsOpen = False Then
                    RaiseEvent Disconnected()
                    GoTo ThreadExit
                End If
            Loop
ThreadExit:
        End Sub
    End Class

you can detect the availables serial ports, the you can try to comunicate with them inside a try...catch block.

This is an example of ports detection in c#

using System;
using System.Collections.Generic;
using System.IO.Ports;

public class MyClass
{
 public static void Main()
 {
   string[] sPorts = SerialPort.GetPortNames();
   foreach(string port in sPorts)
     WL(port);
  RL();
 }

 #region Helper methods

 private static void WL(object text, params object[] args)
 {
  Console.WriteLine(text.ToString(), args); 
 }

 private static void RL()
 {
  Console.ReadLine(); 
 }

 private static void Break()
 {
  System.Diagnostics.Debugger.Break();
 }

 #endregion
}

It will depend on what kind of device you connect with what kind of cable.

Your best bet is to try handling the PinChanged event handler.

Some devices will raise DSR when connected and switched on, others CTS, others will use these for handshaking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top