我已经编写了以下代码,以在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);

}

产品详情:

最佳答案

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

09-26 22:33