SerialPort.Open产生IOException

时间:2019-07-18 01:56:37

标签: c# serial-port

系统:Windows 10

串行端口设置:

enter image description here

enter image description here

设备发送和接收字符串。

我有一个非常简单的程序:

static void Main(string[] args)
{
    string[] ports = SerialPort.GetPortNames();
    for (int i = 0; i < ports.Length; i++)
    {
        Console.WriteLine("[" + i.ToString() + "] " + ports[i].ToString());
    }

    SerialPort port = new SerialPort(ports[0], 9600, Parity.None, 8, StopBits.One);
    port.Handshake = Handshake.None;
    port.ReadTimeout = 500;
    port.WriteTimeout = 500;

    /* already tried:
        port.RtsEnable = true/false;
        port.DiscardNull = true/false;
        port.DtrEnable = true/false;
        port.Handshake = Handshake.RequestToSend/RequestToSendXOnXOff/XOnXOff;
        port.BaudRate = 230400;
        Console.WriteLine(port.Read());
        */

    try
    {
        if (port.IsOpen)
        {
            Console.WriteLine("Port opened -> closing");
            port.Close();
        }

        port.Open();  // <<<----- ERROR

        if (!port.IsOpen)
            Console.WriteLine("Can't open port");
        else
            port.Write("My test message");
    }
    catch (Exception e)
    {
        Console.WriteLine("ERROR:" + e.ToString());
    }

    if (port != null && port.IsOpen)
    {
        Console.WriteLine("Closing port");
        port.Close();
    }

    Console.ReadKey();
}

以管理员身份启动程序。 输出:

enter image description here

好的。我打开了串行端口终端(白蚁3.4),我的com端口工作得很好。发送和接收字符串消息。 enter image description here

白蚁设置:

enter image description here

请帮助我修复它! 谢谢。

0 个答案:

没有答案