System.IO.Ports in C# Cannot Communicate with Arduino Uno (CH340) – But Arduino IDE & Processing Work Fine

Hi all,

I'm running into a serial communication issue that seems specific to .NET’s System.IO.Ports implementation. Here's everything relevant:

Operating System: Windows 11 Pro
OS Version: 10.0.26100 N/A Build 26100
Visual Studio:
Microsoft Visual Studio Community 2022 (64-bit) - Version 17.13.0
Arduino IDE:
Version 2.3.6
.NET SDKs Installed:
8.0.405
.NET Runtimes Installed:
Microsoft.AspNetCore.App 8.0.13
Microsoft.NETCore.App 8.0.13
Microsoft.WindowsDesktop.App 8.0.13
Board: Arduino Uno clone with CH340 USB-to-Serial chip
Detected as: USB-SERIAL CH340 (COM3)

Uploading and serial communication via Arduino IDE works fine
Processing IDE communicates with the board without issue

COM port is confirmed as COM3 and visible in Device Manager

Using this very basic System.IO.Ports.SerialPort example in a C# console app:

using System;
using System.IO.Ports;

class Program
{
    static void Main()
    {
        SerialPort port = new SerialPort("COM3", 9600);
        try
        {
            port.Open();
            Console.WriteLine("Connected to Arduino.");
            port.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
           Console.ReadKey();
    }
}

I consistently get this error:

Error: A device attached to the system is not functioning.

The port opens fine in other software, but System.IO.Ports always fails, even when the IDE is closed, and no other process is using COM3.

I tried toggling DtrEnable and RtsEnable as elsewhere in the forum mentioned
Ran the app as Administrator

As a temporary workaround, I’ve implemented a TCP bridge using Processing:
Processing reads/writes to the Arduino over serial
C# connects to Processing via TCP to send/receive messages

Arduino Sketch for Testing:

void setup() {
  Serial.begin(9600);
}

void loop() {
  
}

Is there a known fix or alternate C# serial library that bypasses System.IO.Ports entirely?
Would flashing the latest bootloader make a difference seeing that its about 15 years old already.

Any help would be appreciated
Thanks,
Arnold

What happens if you specify the port as "\\.\COM3" instead of "COM3"?
Not familiar with C#, but wondering whether it needs the full path specified, which is what you usually have to do when referring to ports > COM9. Not usually necessary for port numbers in single digits though.

I'm not 100% sure but I seem to think that the following line opened the serial port

SerialPort port = new SerialPort("COM3", 9600);

which would mean you are trying to open the serial port twice, just to check this out try commenting out the port.Open(); line.

This might not be the problem but does not take a second to prove.

This is known working code that communicates with an arduino. You may have to clean it up a bit since it was embedded in a routine to automatically determine which port is correct. But I remember having a similar problem and this fixed it. Don't remember exactly what change fixed the problem. name is something like "COM3"

            port = new SerialPort(name, 115200);
                    try
                    {
                        port.Handshake = Handshake.None;
                        port.DataBits = 8;
                        port.StopBits = StopBits.One;
                        port.Parity = Parity.None;
                        port.Encoding = new ASCIIEncoding();
                        port.RtsEnable = true;
                        port.DtrEnable = true;

                        port.Open();
                   }

HTH