2012-08-09 102 views
1

另一端的设备突然断开连接后,我无法重新连接到COM端口。 只有关闭并重新打开应用程序,我才能再次连接。丢失连接后重新连接到COM

这里是我的连接功能:

public bool connectTurboPump() 
    { 
     try 
     { 
      if (TPumpSerialPort != null && TPumpSerialPort.IsOpen == true) 
       return true; 

      DataRow dr = tblTPump.Rows[0]; 

      Types.Connection TPumpConnection = new Types.Connection(); 
      TPumpConnection.PORT = dr["port"].ToString(); 
      TPumpConnection.BAUD_RATE = Convert.ToInt32(dr["baud"]); 
      TPumpConnection.PARITY = (Parity)Enum.Parse(typeof(Parity), dr["parity"].ToString(), true); 
      TPumpConnection.DATA_BITS = Convert.ToInt32(dr["dataBits"]); 
      TPumpConnection.STOP_BITS = (StopBits)Enum.Parse(typeof(StopBits), dr["stopBits"].ToString(), true); 
      TPumpConnection.HANDSHAKE = (Handshake)Enum.Parse(typeof(Handshake), dr["handshake"].ToString(), true); 

      TPumpSerialPort = new SerialPort(TPumpConnection.PORT, TPumpConnection.BAUD_RATE, TPumpConnection.PARITY, TPumpConnection.DATA_BITS, TPumpConnection.STOP_BITS); 
      TPumpSerialPort.Handshake = TPumpConnection.HANDSHAKE; 
      TPumpSerialPort.Open(); 
      TPumpSerialPort.NewLine = "\r"; 
      TPumpSerialPort.ReadTimeout = 10000; 
      TPumpSerialPort.WriteTimeout = 10000; 

      if (TPumpSerialPort.IsOpen != true) 
       return false; 

      return true; 
     } 
     catch { return false; } 
    } 

这里是我重新连接功能:

public bool reconnectTurboPump(int attempts = 3) 
    { 
     try 
     { 
      if (TPumpSerialPort != null && TPumpSerialPort.IsOpen == true) 
      { 
       TPumpSerialPort.Close(); 
       TPumpSerialPort.Dispose(); 
      } 

      int i = 1; 
      while (true) 
      { 
       Log(string.Format("Reconnecting Turbo Pump attempt {0}", i)); 

       if (connectTurboPump()) 
        break; 

       if (i == attempts) 
        return false; 

       i++; 
      } 

      return true; 
     } 
     catch (Exception ex) 
     { 
      Log(string.Format("Could not reconnect to Turbo Pump: {0}", ex.Message)); 
      return false; 
     } 
    } 

真的很感激,如果有人可以帮助。

谢谢。

+1

请看看这个链接:http://stackoverflow.com/questions/4369679/com-port-is-denied。我想你会找到几个适用的建议。 – paulsm4 2012-08-09 19:59:44

+0

如果将if(TPumpSerialPort!= null && TPumpSerialPort.IsOpen == true)'改为'if(TPumpSerialPort!= null)',会发生什么? – 2012-08-09 20:06:48

+0

经过评估10+串口解决方案后,我只找到一个工作。你可以在这里看到我的答案详细信息:http://stackoverflow.com/a/8003897/362536我不知道这个问题是否适用于你,但如果是这样,我建议检查CommStudio。 – Brad 2012-08-09 20:07:03

回答

0

按照托马斯的建议,我已将重新连接脚本更改为以下内容。现在正在测试中。

public bool reconnectTurboPump(int attempts = 3) 
    { 
     try 
     { 
      //if (TPumpSerialPort != null && TPumpSerialPort.IsOpen == true) 
      if (TPumpSerialPort != null) 
      { 
       TPumpSerialPort.Close(); 
       TPumpSerialPort.Dispose(); 
      } 

      int i = 1; 
      while (true) 
      { 
       Log(string.Format("Reconnecting Turbo Pump attempt {0}", i)); 

       Thread.Sleep(2000); 

       if (connectTurboPump()) 
        break; 

       if (i == attempts) 
        return false; 

       i++; 
      } 

      return true; 
     } 
     catch (Exception ex) 
     { 
      Log(string.Format("Could not reconnect to Turbo Pump: {0}", ex.Message)); 
      return false; 
     } 
    } 
1

如果这是一个真正的串口连接,这没什么意义。没有“连接”状态,串行端口是非常简单的设备,没有建立连接的底层协议。

如果这实际上是一个USB设备,模拟串行端口,那么你确实会遇到这种问题。模拟串行端口的驱动程序在端口正在使用时拔出USB连接器时总会变得非常恶心。实际上有的USB设备连接协议,协商是由驱动程序完成的。它们通常使端口消失,这往往会给用户代码一个无法恢复的心脏病发作。行为非常不可预测,因司机而异。没有办法解决这个问题,将连接器粘贴到端口上,并且不要假设拔出它可以解决代码中的任何问题,即使这是您使用USB可以做的唯一的事情。

+0

+1这听起来对我来说很合适。我认为评论中的@ paulm4链接听起来像是一种撩拨USB驱动程序的方式。 http://stackoverflow.com/questions/4369679/com-port-is-denied – kenny 2012-08-10 12:53:27

+0

在我的情况下,这是直接RS232连接,没有任何适配器。 – 2012-08-11 20:04:16

+0

然后我的第一段适用。切勿在关闭后立即调用SerialPort.Open。它不起作用,工作线程需要关闭,并且需要不可预测的时间。没有意义。我无法以其他方式查看connectTurboPump()可能执行的操作。 – 2012-08-11 20:12:58