C# check if a COM (Serial) port is already open

前端 未结 7 2017
南旧
南旧 2020-12-05 03:27

Is there an easy way of programmatically checking if a serial COM port is already open/being used?

Normally I would use:

try
{
    // open port
}
cat         


        
7条回答
  •  心在旅途
    2020-12-05 03:31

    You can try folloing code to check whether a port already open or not. I'm assumming you dont know specificaly which port you want to check.

    foreach (var portName in Serial.GetPortNames()
    {
      SerialPort port = new SerialPort(portName);
      if (port.IsOpen){
        /** do something **/
      }
      else {
        /** do something **/
      }
    }
    

提交回复
热议问题