SerialPort.Open () --IOException - "The parameter is invalid."

I wrote the following code to configure the serial port when loading MainForm. At first start, it gives IOExceptionwhen the port is open, indicating that the parameter is incorrect. But when I restart the application, it works fine. An exception occurs only when the application is launched for the first time after starting the computer, and then it works normally until the next restart of the computer.

private void Main_Load(object sender, EventArgs e)
{
    this.serialPort1.PortName = "COM3";
    this.serialPort1.BaudRate = 9600;
    this.serialPort1.DataBits = 8;
    this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);

    this.serialPort1.Open(); //Exception comes here
    this.serialPort1.WriteLine("AT#cid=1" + System.Environment.NewLine);

}

Exeption Details:

System.IO.IOException failed to execute user code

Message = "Invalid parameter. \ R \ n" Source = "System"

StackTrace:             System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)             System.IO.Ports.InternalResources.WinIOError()             System.IO.Ports.SerialStream.set_RtsEnable ( )             System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, , Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, handshake, dtrEnable, rtsEnable, discardNull, parityReplace)             System.IO.Ports.SerialPort.Open()             JKamdar.Main.Main_Load ( , EventArgs e) D:\Project\JKamdar\JKamdar\Main.cs: 264             System.Windows.Forms.Form.OnLoad(EventArgs e)             System.Windows.Forms.Form.OnCreateControl()             System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)             System.Windows.Forms.Control.CreateControl()             System.Windows.Forms.Control.WmShowWindow(Message & m)             System.Windows.Forms.Control.WndProc(Message & m)             System.Windows.Forms.ScrollableControl.WndProc(Message & m)             System.Windows.Forms.ContainerControl.WndProc(Message & m)             System.Windows.Forms.Form.WmShowWindow(Message & m)             System.Windows.Forms.Form.WndProc(Message & m)             System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message & m)             System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & m)             System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)       InnerException:

+5
3

this.serialPort1.RtsEnable = true

at System.IO.Ports.SerialStream.set_RtsEnable(Boolean value)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
+3

, COM- COM3 , , . , , . .

0

,

SerialPort port = new SerialPort(    "COM3", 9600, Parity.None, 8, StopBits.One);

  // Open the port for communications
  port.Open();

  // Write a string
  port.Write("Hello World");

  // Write a set of bytes
  port.Write(new byte[] {0x0A, 0xE2, 0xFF}, 0, 3);

  // Close the port
  port.Close();
0

All Articles