【问题标题】:Acces denied on com port c#com端口c#上的访问被拒绝
【发布时间】:2016-04-14 13:40:57
【问题描述】:

我一直在为机器编写一些代码。 它必须与 RS485 通信,我可以使用正常的串行通信,正如 .net 所提供的那样

但是,由于硬件可能出现故障,我必须构建测试例程。 所以我编写了重新检测可用comports的例程。 这将使用其字符串名称填充列表“allComPorts”,然后使用数字 updown 根据其可用comports 的列表索引来选择com 端口。 (是的,这听起来有点复杂,但出于其他原因,我使用了数字向上进行选择)。

问题是这个函数只在第一次工作。 如果我再次调用该函数,我会因为接缝已经打开而被拒绝访问。在不同的地方尝试了 RS485Port.Close(),但是如果没有打开它,问题就变成了崩溃(鸡蛋问题)。

我打开一个comport的代码是这样的

    private void RS485Activate()
    {
            lblNoRS485Communication.Visible = false;
        if (cmbRS485Port.Value != 0) // if 0 then there are no serial ports
        {
            //rs485Port is a global declared as > System.IO.Ports.SerialPort RS485Port;
            RS485Port = new System.IO.Ports.SerialPort(allComPorts[(int)cmbRS485Port.Value - 1]);
            if (!RS485Port.IsOpen)
            {
               // RS485Port.Close();
               // RS485Port = new System.IO.Ports.SerialPort(allComPorts[(int)cmbRS485Port.Value - 1]);
                RS485Port.BaudRate = 9600;
                RS485Port.Parity = System.IO.Ports.Parity.None;                 
                RS485Port.StopBits = System.IO.Ports.StopBits.One;             
                RS485Port.DataBits = 8;                                        
                RS485Port.Handshake = System.IO.Ports.Handshake.None;          
                RS485Port.RtsEnable = true;
                RS485Port.Open();  <== it crashes here with access denied
            }
        }
        else
        {
            MessageBox.Show("There is no COM port detected, the program will work but it cannot control any machinery", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            lblNoRS485Communication.Visible = true; // another warning
        }
     }

【问题讨论】:

  • 你检查过端口是否没有被其他进程使用吗?
  • 它以前被打开过,同样的程序。但是对于测试,我不能假设这一点(其他事情可能已经发生在机器上)。因为我不能像这样测试:if (RS485Port !=Null) RS485Port.close(); 我想知道如何解决它
  • 调用 Close() 不会立即使端口可用。有一个工作线程需要退出,它会生成 DataReceived 和 ErrorReceived 事件。这需要多长时间是不可预测的。但是您当然可以从异常中看出 :) 睡一会儿,然后再试一次。不要永远尝试,因为它实际上可能是另一个进程已经声明了该端口。

标签: c# serial-port


【解决方案1】:

我不喜欢这样的外观,所以我希望有更好的方法。 但是作为一种解决方法,使用 try catch 构造它现在首先尝试关闭,(如果尚未初始化,try-catch 确实可以防止 close() 错误。我不喜欢这种解决方法,所以更好的解决方案不客气,我认为代码不应该预测和基于错误。(或者现在使用 .net 可以吗?)

            //...                
            try
            { RS485Port.Close(); }
            catch
            { }
            RS485Port = new System.IO.Ports.SerialPort(allComPorts[(int)cmbRS485Port.Value - 1]);
            if (!RS485Port.IsOpen)
            {
               // RS485Port.Close();
               // RS485Port = new System.IO.Ports.SerialPort(allComPorts[(int)cmbRS485Port.Value - 1]);
                RS485Port.BaudRate = 9600;
                RS485Port.Parity = System.IO.Ports.Parity.None;          
                RS485Port.StopBits = System.IO.Ports.StopBits.One;    
                RS485Port.DataBits = 8;                                      
                RS485Port.Handshake = System.IO.Ports.Handshake.None;      
                RS485Port.RtsEnable = true;
                RS485Port.Open();
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多