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;
port.ReadTimeout = 1000;
port.WriteTimeout = 500;
try
{
port.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
button1.Text = port.IsOpen.ToString();
}
}
}
source
share