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.
C#

Avatar of undefined
Last Comment
u2envy1

8/22/2022 - Mon
pvginkel

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

You must get a signal on a pin or something...
pvginkel

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.
<<<<<
Your help has saved me hundreds of hours of internet surfing.
fblack61
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.
ASKER
u2envy1

If a device is already connected & I want to see if one is connected. Will the PinChanged event still fire ?
illusio

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
u2envy1

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

How do I call the PinChanged Event. Before I open the port or after ?
illusio

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
u2envy1

On Handshake.Hardware I dont get Hardware only None, XOnXOff,RequestToSend,RequestToSendXOnXOff
also e.PinChange does not exist.
illusio

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.

ASKER
u2envy1

Thx, my sPort_PinChanged event is not firing.....
I have added it like the above.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
illusio

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
u2envy1

Thx, all your help is much appreciated.
ASKER
u2envy1

illusio ,Could you please help with this question......
https://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24083089.html