SerialPort.Open()--IOException - “参数不正确。”

时间:2011-04-06 10:39:33

标签: c# .net serial-port

我编写了以下代码来配置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”        源= “系统”

     

堆栈跟踪:              在System.IO.Ports.InternalResources.WinIOError(Int32 errorCode,String str)              在System.IO.Ports.InternalResources.WinIOError()              在System.IO.Ports.SerialStream.set_RtsEnable(布尔值)              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)              在System.IO.Ports.SerialPort.Open()              在D:\ Project \ JKamdar \ JKamdar \ Main.cs中的JKamdar.Main.Main_Load(Object sender,EventArgs e):第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(消息& m)              在System.Windows.Forms.Control.WndProc(消息& m)              在System.Windows.Forms.ScrollableControl.WndProc(消息& m)              在System.Windows.Forms.ContainerControl.WndProc(消息& m)              在System.Windows.Forms.Form.WmShowWindow(消息& m)              在System.Windows.Forms.Form.WndProc(消息& 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:

3 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

检查com端口“COM3”是否打开可以解决我认为的问题。如果它是打开的,您应该先关闭它然后再次重新打开。打开一个打开的端口可能会出错。

答案 2 :(得分: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();