Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Forcefully close SerialPort Connection?

Tags:

c#

I have device which is connect to PC via USB Serial communication. I am performing following steps

  1. Start Device (Power On)
  2. Device will detected in my PC as COMx name
  3. Start my application
  4. On base of COM PID/VID, I connect to device
  5. Perform communication. (Up to this I am not facing any problem)
  6. when I switch off device, device is disconnected but I cannot able close connection.
  7. When I again start(Switch on) device, Device is detected but my application cannot able to connect to COMx(device). It throws IOException "COMx does not exist"

So I think I have some way to close communication port forcefully.

like image 915
Pritesh Avatar asked Oct 07 '10 10:10

Pritesh


People also ask

How do I force close a COM port?

There is no way to force another process to close the port so you can take it. But Windows isn't opening up the port by itself - some other application running in the background is probably doing it. Download Process Explorer and use the "Find Handle or DLL" on the Find menu to find the process with the com port open.


2 Answers

If you are communicationg with your hardware over a serial port and using a SerialEventListener, then I think a Serial.DATA_BREAK(Dont remember the exact name) event occurs. You could catch this in the SerialEvent method and close the connection from your program to your port. Hope this helps you to forcibly close your connection at the time of disconnection of the hardware.

like image 74
Gyan Avatar answered Oct 12 '22 01:10

Gyan


I have found solution though it can not be called standard solution but though i don't have any problem with that solution because it solves my problem the solution is as per belove step

  1. Create class.
  2. Create and dispose instance of this class every time you want to send data.

FIRST STEP:-

    class clsRS232 : IDisposable
    {
        private SerialPort myPort;
        public clsRS232()
        {
            myPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
        }
        public void OpenPort()
        {
            myPort.Open();
        }
        public void SendFunc(string str)
        {
            myPort.Write(str);
        }
        public string ReadFunc()
        {
           return  myPort.ReadExisting();
        }

        public void Dispose()
        {
            myPort.Dispose();
        }

    }

SECOND STEP:-

using (clsRS232 mySerialPort = new clsRS232())
                    {
                        lock (mySerialPort)
                        {
                            mySerialPort.OpenPort();
                            mySerialPort.SendFunc(commandArg);//here "commandArg" is command or data you want to send to serial port
                            this.serialPortObj_DataReceived(mySerialPort.ReadFunc()); //here "serialPortObj_DataReceived()" is a user define method where received data will be passed
                        }
                    }
like image 21
4 revs, 2 users 98% Avatar answered Oct 12 '22 01:10

4 revs, 2 users 98%