views:

791

answers:

5

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

A: 

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
}
Jonathan
I believe that this form of .Connect() in a loop binding to the port for each connection is clunky, Especially if i want a nice event driven SerialPortDisconnected Event.
maxfridbe
A: 

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.

ChrisW
True if your device is using hardware handshaking. Many devices do not. In fact some serial ports don't even have those pins connected.
Jim C
+8  A: 

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.)

andersop
A: 

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.

Joe
+1  A: 

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.

Jim C