C# 尝试在C中打开com端口时发生UnauthorizedAccessException#

C# 尝试在C中打开com端口时发生UnauthorizedAccessException#,c#,serial-port,C#,Serial Port,我用Arduino板在串行COM端口上写,用C#app读取。但每当我试图运行C#程序时,当它试图打开串行端口时,就会发生异常(UnauthorizedAccessException)。如何正确打开串行端口 我对C++的知识很少。我的大部分知识来自于它们与C++的相似之处,以及大量的Google教程。以下是我从找到的一个教程中复制的代码之一: public partial class Form1 : Form { public Form1() { Initializ

我用Arduino板在串行COM端口上写,用C#app读取。但每当我试图运行C#程序时,当它试图打开串行端口时,就会发生异常(UnauthorizedAccessException)。如何正确打开串行端口

我对C++的知识很少。我的大部分知识来自于它们与C++的相似之处,以及大量的Google教程。以下是我从找到的一个教程中复制的代码之一:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if(!serialPort1.IsOpen)
        {
            serialPort1.PortName = "COM3";

            serialPort1.Open();

            richTextBox1.Text = "opened";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if(serialPort1.IsOpen)
        {
            string text = richTextBox1.Text;
            serialPort1.WriteLine(text);
        }
    }
}

发生的情况是,您已经在另一个应用程序中打开了COM3(请参阅)。当您单击按钮1时,程序可以看到COM端口已打开(在另一个应用程序中),并尝试对其进行写入,即使您的程序没有访问权限

首先,您需要创建一个串行端口的实例(我怀疑您已经这样做了,因为您将收到不同的错误)。我在创建Form1时创建实例:

    public Form1()
    {
        InitializeComponent();

        // Create an instance of a serial port
        serialPort1 = new SerialPort();
    }
在以下内容中,我已将button2重命名为openPortButton:

    private void openPortButton_Click(object sender, EventArgs e)
    {
如果串行端口已经打开,则可以通过关闭串行端口使程序具有攻击性:

        // Check if the serial port is already open. If it is close it down first.
        if (serialPort1.IsOpen == true)
        {
            serialPort1.Close();
        } 
在打开串行端口时,也可以通过将其包装在try-catch语句中来捕获任何异常:

        if(!serialPort1.IsOpen)
        {
            serialPort1.PortName = "COM3";

            try
            {
                serialPort1.Open();
            }
            catch
            {
                // Add exception handling here
            }
        }
然后测试串行端口是否实际打开:

        // Test connection
        if (serialPort1.IsOpen == true)
        {
            richTextBox1.Text = "opened";
            commsEstablished = true;
        }
        else
        {
            richTextBox1.Text = "not successful";
            commsEstablished = false;
        }
    }
其中,
commsestabled
是表1中的私有布尔值

现在,当按下send按钮时,测试
commsestabled
变量:(我还将button1重命名为sendButton)


UnauthorizedAccessException通常表示端口已在某处打开。您是否有ArduinoIDE中的open SerialMonitor或其他可以访问SerialPort的软件?另外,请发布您使用的实际代码,而不是网络中的一些示例-这一个缺少创建
new SerialPort()
我不知道在哪里创建串行端口。我刚刚在VisualStudio中使用了design视图来拖放串行元素。此外,我还尝试在串行监视器关闭时运行它。它仍然不工作。实际上,可能是串行监视器导致了问题。我现在正在彻底检查。谢谢理论上你是对的,但是。。。例如,我们关闭了一个端口,然后检查IsOpen。。。打开它——猜猜看——例外就在那里。在关闭和等待之后,它可以重新打开…@Offler,很高兴知道。您能告诉我们在打开com端口之前需要等待多长时间吗?这似乎取决于硬件和系统目前正在做什么。表明超过300毫秒,我们有大约250毫秒,200毫秒有时失败,250毫秒几年都没有失败。
    private void sendButton_Click(object sender, EventArgs e)
    {
        if(commsEstablished && serialPort1.IsOpen)
        {
            string text = richTextBox1.Text;
            try
            {
                serialPort1.WriteLine(text);
            }
            catch
            {
                // Add exception handling here
            }
        }
    }