SerialPort.Open() --IOException --“参数不正确。”
我编写了以下代码来在加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请尝试使用
根据异常的堆栈跟踪建议的this.serialPort1.RtsEnable = true
Please try using this.serialPort1.RtsEnable = true
Suggested based on the stack trace of your exception
测试下面的代码是否报错
Test if following code gives error
检查 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.