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

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

问题描述

我正在尝试在 C# 中打开一个 COM 端口,但我得到一个带有错误消息的 IO 异常:

I'm trying to open a COM port in C# but I'm getting an IO Exception with the error message:

参数不正确

我看到了这个帖子:SerialPort.Open() --IOException — 参数不正确."
它描述了同样的问题,但是将 RtsEnable 设置为 true 并没有解决我的问题(没有任何改变).

I saw this post: SerialPort.Open() --IOException — "The parameter is incorrect."
which describes the same problem, but setting RtsEnable to true did not resolve my problem (nothing changed).

这是我的代码:

cmp_Comport.PortName = "COM6";
cmp_Comport.BaudRate = 9600;
cmp_Comport.Parity = Parity.None;
cmp_Comport.StopBits = StopBits.One;
cmp_Comport.DataBits = 8;
cmp_Comport.Handshake = Handshake.None;
cmp_Comport.RtsEnable = true;
cmp_Comport.DataReceived += new SerialDataReceivedEventHandler(CMP_DadaReceived);
cmp_Comport.Open(); // ==> Causes exception

这是完整的异常堆栈跟踪:

Here's the full exception stack trace:

在 System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
在 System.IO.Ports.InternalResources.WinIOError()
在 System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
在 System.IO.Ports.SerialStream..ctor(字符串端口名,Int32 波特率,奇偶校验,Int32 数据位,StopBits stopBits,Int32 读取超时,Int32 写入超时,握手握手,布尔 dtrEnable,布尔 rtsEnable,布尔丢弃空,字节奇偶校验替换)
在 System.IO.Ports.SerialPort.Open()
在 C:...MyProjectComport.cs:line 83 中的 MyProject.Comport.CMP_Open(Int32 ind, String& error)

at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.InternalResources.WinIOError()
at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
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 MyProject.Comport.CMP_Open(Int32 ind, String& error) in C:...MyProjectComport.cs:line 83

请注意,在其他软件中,例如Hercules,同一个端口打开就好了.

Note that in another software, e.g. Hercules, the same port opens just fine.

推荐答案

此异常通常发生在没有底层物理 RS​​232 实现的虚拟(例如 USB)COM 端口上.此类端口不管理状态位,因为 SerialPort.Open() 方法在尝试设置通信参数时引发 IOException 错误 87参数不正确"串口.

This exception often occurs with virtual (e.g. USB) COM ports that do not have an underlying physical RS232 implementation. Such ports do not manage state bits and because of that SerialPort.Open() method raises IOException with error 87 "The parameter is incorrect" when it tries to set communication parameters for the serial port.

System.IO.Ports.SerialPort 类不支持这种情况,但您可以使用其他实现.

System.IO.Ports.SerialPort class does not support this case, but there are other implementations that you can use.

例如,使用 SerialPortStream 库(也可在 NuGet) 您可以使用 SerialPortStream.OpenDirect() 方法打开串行 COM 端口而无需设置通信参数:p>

For example, with SerialPortStream library (also available in NuGet) you can open serial COM port without setting communication parameters using SerialPortStream.OpenDirect() method:

namespace Vurdalakov
{
    using System;
    using RJCP.IO.Ports;

    class Program
    {
        static void Main(String[] args)
        {
            using (var serialPort = new SerialPortStream("COM1"))
            {
                serialPort.OpenDirect();

                while (serialPort.IsOpen)
                {
                    var ch = (Char)serialPort.ReadChar();
                    Console.Write(ch);
                }
            }
        }
    }
}

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

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