SerialPort.Open()-参数不正确 [英] SerialPort.Open() - The parameter is incorrect
问题描述
我试图在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,奇偶校验,Int32 dataBits,StopBits stopBits,布尔型discardNull)
在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()处
在MyProject.Comport.CMP_Open(Int32 ind,String& error)中的C:... \ MyProject \ Comport.cs:line 83
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:...\MyProject\Comport.cs:line 83
请注意,在其他软件中,例如大力神,同一端口打开就可以了.
Note that in another software, e.g. Hercules, the same port opens just fine.
推荐答案
此异常通常发生在没有基础物理RS232实现的虚拟(例如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 库(在
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屋!