拒绝访问端口“COM5"

Access to the port #39;COM5#39; is denied(拒绝访问端口“COM5)
本文介绍了拒绝访问端口“COM5"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误消息访问端口COM5"被拒绝.从我的表单运行以下方法时.我尝试从设备管理器的端口设置中输入正确的波特率 9600.我也尝试过通过 Portmon 访问设备,但有一个错误阻止我连接.有什么办法可以解决这个问题?

I get the following error message Access to the port 'COM5' is denied. when running the method below from my form. I have tried entering the right baud rate of 9600 from the port setting of my device manager. I have also tried accessing the devices through Portmon but there is a bug that prevents me from being connected. Any alternative to solve this problem?

      //Fields
    List<string> myReceivedLines = new List<string>();

    //subscriber method for the port.DataReceived Event
    private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        while (sp.BytesToRead > 0)
        {
            try
            {
                myReceivedLines.Add(sp.ReadLine());
            }
            catch (TimeoutException)
            {
                break;
            }
        }
    }

    protected override void SolveInstance(IGH_DataAccess DA)
    {

        string selectedportname = default(string);
        DA.GetData(1, ref selectedportname);
        int selectedbaudrate = default(int);
        DA.GetData(2, ref selectedbaudrate);
        bool connecttodevice = default(bool);
        DA.GetData(3, ref connecttodevice);

        port.DtrEnable = true;   //enables the Data Terminal Ready (DTR) signal during serial communication (Handshaking)
        port.Open();             //Open the port
        if (!(port.IsOpen == true)) port.Open();


        if (connecttodevice == true)
        {
            port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            DA.SetDataList(0, myReceivedLines);
        }

推荐答案

需要将SerialPort的使用封装在一个使用声明或实现IDisposable

You need to wrap the use of SerialPort in a using statement or implement IDisposable

// Dispose() calls Dispose(true)
public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

// The bulk of the clean-up code is implemented in Dispose(bool)
protected virtual void Dispose(bool disposing)
{
    if (disposing)
    {
        // free managed resources
        if (_serialPort != null)
        {
            _serialPort.Dispose();
            _serialPort = null;
        }
    }
    // free native resources if there are any.
}

这篇关于拒绝访问端口“COM5"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)