gpt4 book ai didi

c# - SerialPort.Open() --IOException -- "The parameter is incorrect."

转载 作者:太空狗 更新时间:2023-10-29 20:35:11 26 4
gpt4 key购买 nike

我编写了以下代码来在 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 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:

最佳答案

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

关于c# - SerialPort.Open() --IOException -- "The parameter is incorrect.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5565007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com