在c#中打开串口时出错

发布于 2024-10-10 17:47:32 字数 1450 浏览 1 评论 0原文

因此,当我尝试使用 SerialPort.Open() 方法在 ac# 应用程序中打开 COM1 时,收到以下异常:

“ArgumentException: 给定的端口名称不以 COM/com 开头或无法解析为有效的串行端口但是

,如果我在设备管理器中禁用 Com1 端口,然后再启用,则一切正常。从那时起,就没有问题了。我可以运行应用程序并打开端口,不会失败。但如果我重置电脑,我会遇到同样的问题,直到我禁用然后启用。

当我启动计算机时,Com1 似乎没有打开。当 Com1 打开时,我尝试使用我的应用程序打开,但没有收到 ArgumentException。相反,我收到一个异常,即对该端口的访问被拒绝。

我只在 Windows 7 机器上尝试过此操作。我用的是VS2010。我尝试过.net 3.5 & 4.0。

因此,正如我所说,一旦我重置端口,该应用程序就可以正常工作。有什么想法吗?

代码(这是一个简单的测试应用程序):

public partial class Form1 : Form
{
    SerialPort port = new SerialPort();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] ports = SerialPort.GetPortNames();

        foreach (string element in ports)
        {
            textBox1.Text = element + "\r\n";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        port.PortName = "COM1";
        port.BaudRate = 9600;
        port.Parity = Parity.None;
        port.DataBits = 8;
        port.StopBits = StopBits.One;
        port.Handshake = Handshake.None; // Handshake.RequestToSend;
        port.ReadTimeout = 1000;
        port.WriteTimeout = 500;
        try
        {
            port.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        button1.Text = port.IsOpen.ToString();
    }
}

}

So I am receiving the following exception when I attempt to open COM1 in a c# application, using the SerialPort.Open() method:

"ArgumentException: The given port name does not start with COM/com or does not resolve to a valid serial port"

However, if I disable the Com1 port in Device Manager, then enable, everything works fine. From then on, no problems. I can run the app and open the port without fail. But if I reset the PC, I experience the same issue, until I disable then enable.

Com1 does not appear to be open when I start the computer. When Com1 is open, and I try to open with my app, I do not get an ArgumentException. Rather, i get an exception that access to that port is denied.

I have only attempted this with Windows 7 machines. I am using VS2010. I tried .net 3.5 & 4.0.

So, as I've said, the app works fine ONCE I reset the port. Any thoughts?

Code (This is a simple test app):

public partial class Form1 : Form
{
    SerialPort port = new SerialPort();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] ports = SerialPort.GetPortNames();

        foreach (string element in ports)
        {
            textBox1.Text = element + "\r\n";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        port.PortName = "COM1";
        port.BaudRate = 9600;
        port.Parity = Parity.None;
        port.DataBits = 8;
        port.StopBits = StopBits.One;
        port.Handshake = Handshake.None; // Handshake.RequestToSend;
        port.ReadTimeout = 1000;
        port.WriteTimeout = 500;
        try
        {
            port.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        button1.Text = port.IsOpen.ToString();
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2024-10-17 17:47:32

我想通了。

由于某种原因,Adobe PDF 被分配给 COM1。如果我将 Adob​​e 迁移到 COM2,那么一切都很好。我想由于 Adob​​e 没有主动使用该端口,这就是为什么我没有收到“访问被拒绝”的消息。

我根本不知道为什么 Adob​​e PDF 需要串行端口。

感谢您的意见/投票。

I figured it out.

Adobe PDF, for some reason, is assigned to COM1. If I move Adobe to COM2, then all is well. I suppose since Adobe is not actively using the port, that is why I don't get "access denied."

I have no idea why Adobe PDF needs a serial port at all.

THanks for the views/votes.

送你一个梦 2024-10-17 17:47:32

我禁用了comport,然后再次启用!

然后将其重命名为例如COM6!一些COM6。一些步骤是:

控制面板=>设备管理器=>串行端口(COM ... =>高级设置=>

从组合框中更改端口名称,

并删除我的打印机设备中的硝基PDF

I disabled comport and then enabled again!!

And then renamed it to for example COM6! some COM6. Some of steps is :

Control Panel=>Device Manager=>Serial Ports(COM ... =>Advanced setting =>

Change port name from combo-box,

And delete nitro-PDF in my printers device

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文