Link to home
Start Free TrialLog in
Avatar of u2envy1
u2envy1

asked on

Divice existance on serail port

How do I check if a device is connected to a serial port ?
My device does not send something back when speaking to it.

Im using the System.IO.Ports namespace.
Avatar of pvginkel
pvginkel
Flag of Netherlands image

I do not think the operating systems can detect whether a cable is connected; only when the device sends something.
Avatar of u2envy1
u2envy1

ASKER

You must get a signal on a pin or something...
Again, I do not believe there is. I have found a fix though. Maybe it helps.

>>>>>
http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx

Q: How do you detect when a device is connected or disconnected?
Simply put, the device usually start or stop sending data.  There is no built in events on a connect or disconnect.  But there are a few tricks you can do, if youre creating the serial device you have more options.  Ive had my devices (and PC apps) send a constant are you there set of bytes, like 00 00 11 11 00 00 hex (Id use a are you there custom packet as in Q12 above) till a device on the other end responds.  You could also use hardware, thats what some of the other signals lines are for (CDC, DSR, RTS, CTS, RI), you can tie one of these high and then catch an event when the line goes high you know theres a device there, when it goes low the device is gone.
<<<<<
Avatar of illusio
Catch the PinChanged event on the serial port class.  Open your port and then attach the device. If you get an event, then you can detect it being present. Otherwise, you can't. Plain and simple.

Kind regards.
Avatar of u2envy1

ASKER

If a device is already connected & I want to see if one is connected. Will the PinChanged event still fire ?
Nope. Actually you use the events to detect a status change - connect/disconnect.
Once you know which pin can be used to detect the connect/disconnect you can use the appropriate property to detect the initial state. That way, if your program starts you know if you're connected or not.

Kind regards.
Avatar of u2envy1

ASKER

Im not with you on the last comment. Can you explain in more detail pls ?
Avatar of u2envy1

ASKER

How do I call the PinChanged Event. Before I open the port or after ?
See snippit below - beware about the correct naming of the props and so - I have created the snippit in notepad... without VS at hand.
private bool hasDeviceConnected;
public void Initialize() 
{
  hasDeviceConnected = false;
  SerialPort sp = new SerialPort();
  // Set up port properties
  sp.PortName = "COM1";
  sp.BaudRate = 9600;
  sp.Parity = Parity.Even;
  sp.DataBits = 8;
  sp.StopBits = 1;
  sp.Handshake = Handshake.Hardware;
 
  sp.PinChanged += new PinChangedEventHandler(SerialPort_PinChanged);
  
  sp.Open();
  hasDeviceConnected = sp.DsrHolding; // just to show the usage
}
 
public void SerialPort_PinChanged(Object sender, SerialPinChangedEventArgs e)
{
   // Try to find out what happens if you physically connect/disconnect your serial hardware
   switch(e.PinChange)
   {
       case SerialPinChange.CtsChanged: break;
       case SerialPinChange.DsrChanged: 
          hasDeviceConnected = (sender as SerialPort).DsrHolding; //e.g.
          break;
       case SerialPinChange.CDChanged: break;
       case SerialPinChange.Ring: break;
       case SerialPinChange.Break: break;
   }
}

Open in new window

Avatar of u2envy1

ASKER

On Handshake.Hardware I dont get Hardware only None, XOnXOff,RequestToSend,RequestToSendXOnXOff
also e.PinChange does not exist.
This is what you get from typing code without VS.

sp.Handshake = Handshake.RequestToSend // hardware handshake

and

e.PinChange --> e.EventType

The handshake you have to use depends on your serial device. In general there are three possibilities commonly used.
Handshake.None: sending starts right away - mostly used for one-directional devices - you just don't care about the receiver
Handshake.RequestToSend: you leave the synchronisation of send/receive to the hardware
Handshake.XOnXOff: the hardware doesn't support synchronisation and you do the sync through the software
Anyway, you have to consult the manual of your serial device to see what is supported. Also the baudrate, databits, stopbits, parity, ... all those things should be set according to what is mentioned in the manual.

Avatar of u2envy1

ASKER

Thx, my sPort_PinChanged event is not firing.....
I have added it like the above.
ASKER CERTIFIED SOLUTION
Avatar of illusio
illusio
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of u2envy1

ASKER

Thx, all your help is much appreciated.
Avatar of u2envy1

ASKER

illusio ,Could you please help with this question......
https://www.experts-exchange.com/questions/24083089/SDK-Class-Library.html