尝试访问通信端口时C#访问被拒绝? [英] C# access denied when trying to access communications port?

查看:116
本文介绍了尝试访问通信端口时C#访问被拒绝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Visual Studios中创建一个简单的Windows窗体应用程序,该应用程序将允许我控制连接到Arduino的LED灯条的亮度设置.

I'm trying to make a simple Windows Form Application in Visual Studios that will allow me to control the brightness setting of an LED strip hooked up to an Arduino.

Arduino内部的编程已经做好,但是C#编程却给我带来了麻烦.

The programming inside the Arduino is already well and done, but the C# programming is what's giving me trouble.

当尝试通过端口发送信息字符串时,我收到以下消息:

When trying to send strings of information through the port, I receive the following message:

System.dll中发生了类型为'System.UnauthorizedAccessException'的未处理的异常.附加信息:拒绝访问端口"COM3".

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.dll . Additional information: Access to the port 'COM3' is denied.

这是给我带来麻烦的一段代码:

Here is the piece of code that is giving me trouble:

  private void trackBar1_Scroll(object sender, EventArgs e)
    {    
        String color = trackBar1.Value.ToString();

        System.IO.Ports.SerialPort myPort = new System.IO.Ports.SerialPort("COM3");
        if (myPort.IsOpen == false){
         //if not open, open the port
            myPort.Open(); // Error is shown here.
        }
        if (myPort.IsOpen){
            myPort.WriteLine(color);
        }
    }

我没有包含任何关闭端口的信息,因为首先我想使其正常工作,并且我不了解发生了什么.我敢肯定,没有其他应用程序正在使用该端口.Arduino是通过USB插入的,以便接收它的电源,但除此之外,它不是从任何其他进程接收或发送.

I haven't included any closing of the port because first I want to get this to work, and I'm not understanding what's going on. I'm pretty sure no other applications are using the port; the Arduino is plugged in to it via USB in order to receive it's power but other than that, it's not receiving or transmitting from any other process.

我尝试将端口从COM3更改为其他端口,这是针对类似问题给出的主要解决方案之一,但仍然得到相同的结果.

I've tried changing the port from COM3 to other ports, which is one of the main solutions given for similar questions, but still I get the same result.

感谢您的帮助.

推荐答案

COM端口上拒绝访问错误的最常见原因是某些其他程序已经打开了COM端口.您可以使用 portmon 来查看哪个程序.可能是您自己的程序,因为您从未正确关闭端口.

The most common cause of Access Denied errors on COM ports is the COM port already being opened by some other program. You can use portmon to see which program that is. It could well be your own program since you never properly close the port.

这篇关于尝试访问通信端口时C#访问被拒绝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆