0
votes

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

The parameter is incorrect

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

Here's my code:

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:

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.

1
try and comment all cmp_Comport beside the first two and see on which one the problem happensstyx
@styx - the same problem still exists after commenting everything except the first twoAlaa M.
try and move the intinalzation to the contructor like this cmp_Comport = new SerialPort("COM6", 9600, ....)styx
@styx - still same problem...Alaa M.
do you use usb adapter ? does it happen on other computers? can you add the whole SerialPort initalztion process?styx

1 Answers

4
votes

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 class does not support this case, but there are other implementations that you can use.

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