【问题标题】:SerialPort.Open() --IOException -- "The parameter is incorrect."SerialPort.Open() --IOException -- "参数不正确。"
【发布时间】:2016-07-08 17:34:27
【问题描述】:

我编写了以下代码来配置 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 错误代码,字符串 str) 在 System.IO.Ports.InternalResources.WinIOError() 在 System.IO.Ports.SerialStream.set_RtsEnable(布尔值) 在 System.IO.Ports.SerialStream..ctor (字符串端口名称,Int32 波特率,奇偶校验,Int32 数据位,StopBits stopBits,Int32 读取超时,Int32 写入超时,握手握手,布尔 dtrEnable,布尔 rtsEnable,布尔丢弃空,字节奇偶校验替换) 在 System.IO.Ports.SerialPort.Open() 在 D:\Project\JKamdar\JKamdar\Main.cs:line 264 中的 JKamdar.Main.Main_Load(Object sender, EventArgs e) 在 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(消息和 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(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 内部异常:

【问题讨论】:

  • 能否获取完整的异常信息和堆栈跟踪信息?
  • 通过设备管理器检查波特率可能是个问题(只是猜测)
  • @V4Vendetta:最大端口速度 460800
  • @Niraj:什么操作系统?你能在其他操作系统上检查一下,看看哪些受到影响吗?
  • 设置 this.serialPort1.RtsEnable = true 并试一试

标签: c# .net serial-port


【解决方案1】:

请尝试使用 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()

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      测试以下代码是否报错

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

      【讨论】:

        猜你喜欢
        • 2019-05-23
        • 1970-01-01
        • 2011-06-23
        • 2015-05-27
        • 2012-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多