Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How could Verify the connection and disconnection of a port COM port. What I want is that if the cable is disconnected it tells me that there is no connection and reboot until it connects. The problem is that I can't get it to respond when disconnecting and connecting. Thank you.

What I have tried:

SerialPort Puerto_serie = new SerialPort("COM4");
            Puerto_serie.BaudRate = 9600;
            Puerto_serie.Parity = Parity.None;
            Puerto_serie.StopBits = StopBits.One;
            Puerto_serie.DataBits = 8;
            Puerto_serie.Handshake = Handshake.None;
            Puerto_serie.RtsEnable = true;

 ConsoleKey tecla;
                Console.WriteLine("Pulse tecla Escape (esc) para apagar:");

                do
                {
    
                string[] ports = SerialPort.GetPortNames();
                    foreach (string port in ports) 
                    {
                     
                    if (port.Equals("COM4"))
                      {
                        Puerto_serie.Open(); // Abrir puerto.
                        Console.WriteLine(port);
                       
    
                    }
                      else
                      {
                        Console.WriteLine("Error opening port");
                        Puerto_serie.Close();
                        Main(args);
                      }
                    }
                    
    
                 tecla = Console.ReadKey(true).Key; // Espera pulsación de teclas.
                
                }
                while (tecla != ConsoleKey.Escape); // Pulsa Escape para salir del menú.
Posted
Updated 8-Dec-20 4:16am

Pretty much, you can't: there isn't an absolute "disconnected" status you can pick up on as there is no way to tell if a device is unplugged or turned off in the serial data. All you can do is - if you are lucky - monitor a status pin that your remote device holds high that connects to a status in on on your PC. Assuming your cable has more than three wires, and no local loopbacks!
 
Share this answer
 
As far as I am aware there is no facility to do what you're asking.

You must make your COM port protocol support it. Basically what you do is send an idle packet periodically, and check for it on the other end. If you timeout with receiving it, your cable is disconnected or the other device is down.
 
Share this answer
 

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