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

发布于 2024-10-30 17:19:41 字数 2313 浏览 1 评论 0原文

我编写了以下代码来在加载 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(布尔值) 在System.IO.Ports.SerialStream..ctor(字符串端口名称,Int32 baudRate,奇偶校验,Int32 dataBits,StopBits stopBits,Int32 readTimeout,Int32 writeTimeout,握手握手,布尔dtrEnable,布尔rtsEnable,布尔discardNull,字节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(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) 内部异常:

I have written the following code to configure the serial port when the MainForm loads. On first run it gives IOException when port is opened, stating that the parameter is incorrect. But when I re-start the application it works fine. The exception only comes when the application is run first time after starting the computer, and then it works fine till 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 was unhandled by user code

Message="The parameter is incorrect.\r\n"
Source="System"

StackTrace:
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.InternalResources.WinIOError()
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()
at JKamdar.Main.Main_Load(Object sender, EventArgs e) in D:\Project\JKamdar\JKamdar\Main.cs:line 264
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

横笛休吹塞上声 2024-11-06 17:19:41

请尝试使用

根据异常的堆栈跟踪建议的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()

Please try using this.serialPort1.RtsEnable = true

Suggested based on the stack trace of your exception

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()
温折酒 2024-11-06 17:19:41

测试下面的代码是否报错

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

Test if following code gives error

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();
黎夕旧梦 2024-11-06 17:19:41

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

Checking that com port "COM3" is opened could solve the problem I think. If it is open, you should close it first then reopen again. Opening an opened port could give some errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文