关闭USB串行端口将使该端口不可用

问题描述 投票:0回答:2

我的应用使用基于USB的串行端口连接到物理硬件设备。我可以打开任何有效的USB端口并与外部设备通信。但是,当我关闭连接时,USB端口会以某种不确定的状态放置一段时间,在此期间,进一步尝试重新连接会导致“访问端口“ COM--”被拒绝”错误。但是,几秒钟后,尝试重新连接成功。我如何确定USB端口何时再次支持新连接?

代码看起来像这样:

    private void Setup(string Port)
    {
        bool ValidPort = false;
        int CloseSleep = 10;

        _PortName = Port;
        _PortType = this;

        string[] AvailablePorts = SerialPort.GetPortNames();  

        foreach(string aPort in AvailablePorts)
        {
            if (aPort == _PortName)
            {
                // The required port is listed in the list of available ports)
                ValidPort = true;
                break;
            }
        }

        if (ValidPort)
        {
            try
            {
                if (_ThePort != null)
                {
                    _ThePort.Close();
                    _ThePort.DataReceived -= ReceivedDataEventHandler;

                    while(CloseSleep-- > 0)
                        System.Threading.Thread.Sleep(100);

                    _ThePort.Dispose();
                    _ThePort = null;
                }
            }
            catch (Exception ex)
            {
                EMS_Config_Tool.ModalDialog md = new EMS_Config_Tool.ModalDialog("Closing Port: " + ex.Message, "System Exception");
                md.ShowDialog();
            }

            System.IO.Ports.SerialPort TheNewPort = new System.IO.Ports.SerialPort(Port, 38400);

            // Setup the event handlers from Tx and Rx
            Handler.DataOutEvent    += CommsSender;
            TheNewPort.DataReceived += ReceivedDataEventHandler;

            TheNewPort.DataBits  = 8;
            TheNewPort.Parity    = Parity.None;
            TheNewPort.Handshake = System.IO.Ports.Handshake.None;
            TheNewPort.StopBits  = System.IO.Ports.StopBits.One;

            // We will try 3 times to open the port, and report an error if we fail to open the port
            try
            {
                TheNewPort.Open();
            }
            catch (Exception)
            {
                System.Threading.Thread.Sleep(1000);

                try
                {
                    TheNewPort.Open();
                }
                catch (Exception)
                {
                    System.Threading.Thread.Sleep(1000);

                    try
                    {
                        TheNewPort.Open();
                    }
                    catch (Exception ex)
                    {
                        EMS_Config_Tool.ModalDialog md = new EMS_Config_Tool.ModalDialog("Opening Port: " + ex.Message, "System Exception");

                        return;
                    }
                }
            }

最终的catch语句是发出有关拒绝Access的错误的地方。请注意,我尝试重试打开端口3次实际上并没有帮助。如果我将端口搁置约5到10秒钟,然后重试调用Setup方法,它将立即成功。

c# wpf .net-4.0
2个回答
0
投票

正如@Neil所说,有很多问题。在我看来,最好的办法是将搜索置于一个循环中,一旦打开端口,便会打开。

我以前经常这样做:

public Task WaitingPort()
{
    while (port is null)
        {
            port = CheckPort();
        }
}

private SerialPort CheckPort()
{
    string[] listPort = SerialPort.GetPortNames();
    foreach(string namePort in listPort)
    {
        SerialPort port = new SerialPort(namePort, 9600);
        if (!port.IsOpen)
        {
            try
            {
                port.Open();
                port.ReadTimeout = 1500;
                string data = port.Readline();
                // I programmed my device to send an "A" until it receives
                // "777" to be able to recognize it once opened
                if (data.Substring(0, 1) == "A") 
                {
                    port.ReadTimeout = 200;
                    port.Write("777"); // to make it stop sending "A"
                    return port;
                }
                else
                {
                port.Close();
                }
            }
            catch (Exception e1)
            {
                port.Close();
            }
        }
    }
    return null;
}

当然,这只是某种模板,您必须根据使用情况对其进行重塑


0
投票

我已经修改了代码,以使用约束循环来为其提供更好的工作机会,这通常是这样做的。我希望有一种更好的方法,因为我倾向于急躁的用户,如果他们必须等待5或10秒才能建立连接,他们将发布缺陷报告。...

            // We will try several times to open the port, upto 10 times over 5 seconds, and report an error if we finally fail to open the port
            try
            {
                TheNewPort.Open();
            }
            catch (Exception ex)
            {
                RetryOpenTimer.Interval = 500;
                RetryCount = 10;
                RetryOpenTimer.Elapsed += new System.Timers.ElapsedEventHandler(RetryOpenTimer_Elapsed);
                WaitForOpen = true;
                RetryOpenTimer.Start();

                while (WaitForOpen && RetryCount > 0)
                {
                    System.Threading.Thread.Sleep(500);
                }
                if (WaitForOpen)
                {
                    EMS_Config_Tool.ModalDialog md = new EMS_Config_Tool.ModalDialog("Opening Port: " + ex.Message, "System Exception");
                    return;
                }
            }

...

    void RetryOpenTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        RetryOpenTimer.Stop();
        RetryOpenTimer.Elapsed -= RetryOpenTimer_Elapsed;

        try
        {
            if (RetryCount-- > 0)
            {
                TheNewPort.Open();

                WaitForOpen = false;
            }
            else
                return;
        }
        catch (Exception)
        {
            RetryOpenTimer.Start();
            RetryOpenTimer.Elapsed += RetryOpenTimer_Elapsed;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.