Error opening serial port in C #

So, I get the following exception when I try to open COM1 in a C # application using the SerialPort.Open () method:

"ArgumentException: this port name does not start with COM / com or does not allow a valid serial port

However, if I disable the Com1 port in the device manager, turn it on, everything will be fine. Since then no problem. I can launch the application and open the port without fail. But if I reset the PC, I have the same problem until I turn off and then turn it on.

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

I just tried to do this with Windows 7 machines. I am using VS2010. I tried .net 3.5 and 4.0.

So, as I said, the application works fine, I reset the port. Any thoughts?

Code (this is a simple test application):

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();
    }
}

}

+3
source share
2 answers

I get it.

Adobe PDF is assigned to COM1 for any reason. If I transfer Adobe to COM2, then everything will be fine. I suppose since Adobe is not actively using the port, so I do not get "access denied".

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

Marriage for views / votes.

+1
source

I turned off comport and turned on again !!

And then renamed it, for example, to COM6! some com6. Some steps:

= > = > (COM... = > = >

,

-PDF

+1

Source: https://habr.com/ru/post/1783785/


All Articles