Click here to Skip to main content
15,914,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

I'm making a WinForms application to access a serial port(I'm using a arduino nano)

It's just a user interface to change ports and set baud rates etc. I've been following youtube tutorials mostly and it's pretty much done.

But now, I would like to add a function where, a MessageBox appears when the serial com device disconnects from the computer. I'm guessing I am supposed to use a PinChanged event? How do I write it though?

Thanks for reading! Any response is appreciated!

What I have tried:

private void SerialPinChangedEventHandler(object sender, SerialPinChangedEventArgs e)
        {
            if (SerialPinChange)
            {
                MessageBox.Show("Hardware disconnected", "Serial Port interface", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
Posted
Updated 30-Sep-20 21:32pm

You could use following two information:
SerialPort.PinChanged Event (System.IO.Ports) | Microsoft Docs[^]
SerialPinChange Enum (System.IO.Ports) | Microsoft Docs[^]

Similar question asked and answered: How to detect when hardware connected to serial com port is (disconnected)removed?[^].

A diffeent approach discussed here[^] where you can use SerialPort.GetPortNames() only return the ports that are available. Then make the deduction.
 
Share this answer
 
v2
Comments
pogcat5000 1-Oct-20 3:47am    
I've read those documents beforehand already, but am still unsure...
Didn't know this was asked before, sorry everyone
Will try looking at the alternate approach. Thanks!
You can't definitely detect physical disconnection, unless some signal is held high by the hardware at the other end of the wire - and most wires don't do that, they use "local loopback" by physically wiring RTS to CTS at the plug for example, if they even do that - many serial interfaces literally just have physical wires connected to pins 2, 3, and 5 (TD, RD, and ground) as the minimum connection required.

Start by looking at your actual physical connections: if you are connected via USB, you can tell when the plug is undone from your end, but once it gets to actual RS232 / RS485 wiring you need to know exactly what is wired to what from end to end.

The easier way is probably to put a "watchdog timer" in the software to detect when there has been no communications between them for a period of time, and try to query the remote device to see if it responds. (Most protocols include a "are you alive?" callback request for this purpose, but you'll have to look at the comms manual for the actual device to check)
 
Share this answer
 
Comments
pogcat5000 1-Oct-20 3:45am    
I see, so physical connection cannot be detected...
Will try looking into the timer. Thank You!
OriginalGriff 1-Oct-20 3:50am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900