0

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.

Alaa M.
  • 4,961
  • 10
  • 54
  • 95
  • try and comment all `cmp_Comport` beside the first two and see on which one the problem happens – styx Dec 23 '18 at 15:04
  • @styx - the same problem still exists after commenting everything except the first two – Alaa M. Dec 23 '18 at 15:06
  • try and move the intinalzation to the contructor like this cmp_Comport = new SerialPort("COM6", 9600, ....) – styx Dec 23 '18 at 15:07
  • @styx - still same problem... – Alaa M. Dec 23 '18 at 15:11
  • do you use usb adapter ? does it happen on other computers? can you add the whole SerialPort initalztion process? – styx Dec 23 '18 at 15:13
  • I don't use an adapter. Just a direct USB type C. I just figured that it happens only on some of the boards (custom boards with STM32F7 MCU). But again, programs like Hercules open ports successfully on the boards that I'm not able to open the ports at. – Alaa M. Dec 26 '18 at 06:38
  • What additional initialization are you referring to? – Alaa M. Dec 26 '18 at 06:47
  • 1. try to add `cmp_Comport.DtrEnable = true;` 2. I meant if there are more relevant code that you can add(for example the declaration of the `cmp_Comport`(`SerialPort cmp_Comport = ...` ) – styx Dec 26 '18 at 07:44
  • Same problem with `DtrEnable = true`. Declaration is just `SerialPort cmp_Comport = new SerialPort();` – Alaa M. Dec 26 '18 at 12:48
  • Where are you putting this code? You should not open the port until the form and everything is initialized. It's possible you are opening the port too early. – Baddack Dec 26 '18 at 16:34
  • I open it on a button click so the GUI is surely ready... – Alaa M. Dec 27 '18 at 16:48
  • What version of .NET are you using? – Baddack Dec 27 '18 at 18:10

1 Answers1

5

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);
                }
            }
        }
    }
}
Vurdalakov
  • 99
  • 1
  • 6
  • Fixing the default STM32 generated code in my firmware fixed this for me as described in this answer: https://stackoverflow.com/a/26925578/47078 – Harvey Jun 23 '22 at 14:41