【问题标题】:Reconnecting to COM after lost connection断开连接后重新连接到 COM
【发布时间】:2012-08-07 02:56:42
【问题描述】:

在另一端的设备突然断开连接后,我无法重新连接到 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;
        }
    }

如果有人能提供帮助将不胜感激。

谢谢。

【问题讨论】:

  • 请查看此链接:stackoverflow.com/questions/4369679/com-port-is-denied。我想你会找到一些适用的建议。
  • 如果您将 if (TPumpSerialPort != null && TPumpSerialPort.IsOpen == true) 更改为 if (TPumpSerialPort != null) 会发生什么?
  • 在评估了 10+ 个串口解决方案后,我只找到了一个可行的。您可以在此处查看我的答案以了解详细信息:stackoverflow.com/a/8003897/362536 我不知道该问题是否适用于您,但如果适用,我建议您查看 CommStudio。
  • 布拉德,我无法在 Windows 7 64-it 上安装 CommStudio

标签: c# serial-port reconnect


【解决方案1】:

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

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

【讨论】:

  • +1 这听起来对我来说是正确的。我认为 cmets 中的@paulm4 链接听起来像是一种让 USB 驱动程序发痒的方法。 stackoverflow.com/questions/4369679/com-port-is-denied
  • 在我的情况下,这是直接的 RS232 连接,没有任何适配器。
  • 那么我的第一段适用。切勿在关闭后立即调用 SerialPort.Open。它不起作用,工作线程需要关闭,这需要不可预测的时间。而且没有意义。否则我无法看到 connectTurboPump() 可能会做什么。
【解决方案2】:

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

 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;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    相关资源
    最近更新 更多