开发者

Error opening serial port in c#

开发者 https://www.devze.com 2023-02-03 06:41 出处:网络
So I am receiving the following exception when I attempt to open COM1 in a c# application, using the SerialPort.Open() method:

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.RequestToS开发者_JAVA技巧end;
        port.ReadTimeout = 1000;
        port.WriteTimeout = 500;
        try
        {
            port.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        button1.Text = port.IsOpen.ToString();
    }
}

}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消