я написал следующий код для настройки последовательного порта при загрузке MainForm. При первом запуске выдает IOException
при открытии порта, указывая, что параметр неверен. Но когда я перезапускаю приложение, оно работает нормально. Исключение возникает только тогда, когда приложение запускается в первый раз после запуска компьютера, а затем оно работает нормально до следующего перезапуска компьютера.
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);
}
Сведения об исполнении:
System.IO.IOException не было обработано кодом пользователя
] Message = "Неверный параметр. \ R \ n" Source = "System"
StackTrace: в System.IO.Ports.InternalResources.WinIOError (код ошибки Int32, строка str) в System.IO.Ports.InternalResources.WinIOError () в System.IO.Ports.SerialStream.set_RtsEnable (логическое значение) в System.IO.Ports.SerialStream..ctor (String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, рукопожатие рукопожатия, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNullReplace, Byte в 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 (логическое значение fIgnoreVisible) в System.Windows.Forms.Control.CreateControl () в System.Windows.Forms.Control.WmShowWindow (Сообщение & m) в System.Windows.Forms.Control.WndProc (сообщение & m) в System.Windows.Forms.ScrollableControl.WndProc (сообщение и м) в System.Windows.Forms.ContainerControl.WndProc (сообщение & m) в System.Windows.Forms.Form.WmShowWindow (Сообщение & m) в System.Windows.Forms.Form.WndProc (Сообщение & m) в System.Windows.Forms.Control.ControlNativeWindow.OnMessage (сообщение & m) в System.Windows.Forms.Control.ControlNativeWindow.WndProc (сообщение & m) в System.Windows.Forms.NativeWindow.Callback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException: