SerialPort.Open()--IOException-“参数不正确." [英] SerialPort.Open() --IOException -- "The parameter is incorrect."

查看:55
本文介绍了SerialPort.Open()--IOException-“参数不正确."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了以下代码,以在MainForm加载时配置串行端口.第一次运行时,打开端口时会给出 IOException ,说明该参数不正确.但是,当我重新启动该应用程序时,它运行良好.只有在启动计算机后第一次运行该应用程序时,该异常才会出现,然后在下次重新启动计算机之前,它可以正常工作.

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

}

产品详细信息:

System.IO.IOException未通过用户代码处理

System.IO.IOException was unhandled by user code

Message =参数不正确.\ r \ n"Source =系统"

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

StackTrace:在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(字符串portName,Int32 baudRate,奇偶校验,Int32 dataBits,StopBits stopBits,Int32 readTimeout,Int32 writeTimeout,握手握手,布尔dtrEnable,布尔rtsEnable,布尔dropNull,字节parityReplace在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(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)InnerException:

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

根据您的异常的堆栈跟踪建议

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

这篇关于SerialPort.Open()--IOException-“参数不正确."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆