C# program can’t receive any data from Arduino serialusb class

I am new to arduino. I started with a simple sketch to allow PC to control the LEDs and echo the command on xiao arduino model. When running the sketch, if I use teraterm or putty, i can read echo from xiao and control the LED (write) successfully. but when i use visual studio c# to write a simple console program, i can control the LED (write), but i can never receive anything from xiao(write). the same c# console can receive from non-arduino serial devices

I tried it on multiple configuration and result is the same.

Can any one see if this true with any arduino board or show me how to fix the problem?

Thanks for any pointer!

[Arduino Sketch]

const int LedPin = 13;
int ledState = 0;
String s = "";

void setup()
{
    pinMode(LedPin, OUTPUT);
    SerialUSB.begin(9600);
    while (!SerialUSB)
        delay(10);
}

void loop()
{

    if (SerialUSB.available() > 0)
    {
        s = SerialUSB.readString();
        SerialUSB.println("test=" + s);
        if (ledState == 1)
            ledState = 0;
        else
            ledState = 1;
        digitalWrite(LedPin, ledState);
    }
    delay(10);
}

[VS2017 C# console program]

using System;
using System.IO.Ports;
namespace ConsoleApp1
{
    class Program
    {
        static SerialPort _serialPort;
        static byte [] message = new byte [6000];
        public static void Main()
        {
            int i;
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM6";//Set your board COM
            _serialPort.BaudRate = 9600;
            _serialPort.Open();
            while (true)
            {
                if ((i=_serialPort.BytesToRead)>0)
                {
                    _serialPort.Read(message, 0, i);
                    Console.WriteLine("Read=" + i.ToString());
                }
                if (Console.KeyAvailable)
                {
                    string c = Console.ReadLine();
                    _serialPort.Write(c);
                    if (c == "x")
                    {
                        _serialPort.Close();
                        return;
                    }
                }
            }
        }
    }
}

1 Like

What you've provided makes it clear the Arduino is working just fine, and communicating with the PC just fine. You're problem is in the c# program, which is not something you're likely to find much help with in an Arduino forum. I'd suggest you find a c# forum.

if the c# program can't work with other serial devices, i will totally agree with you. The problem is only arduino fails, all other devices i can get my hand on work...

The fact that it DOES work properly with TeraTerm and PuTTY is all the proof you need that the Arduino is fine, and properly interfaces to the PC, USB drivers, COM port drivers, etc. The problem lies with the c# interface to the COM device. Everything from the Windows COM device driver to the Arduino, and back, obviously works just fine. No doubt some subtle quirk of the c# SerialPort object. I used Arduinos with c# applications years ago, and the c# SerialPort object was notoriously finicky and quirky, and I suspect it still is.

The problem is NOT the Arduino.

OK, thanks

Also, keep in mind, most Arduino boards reset twice when first connected to the USB port. Your c# code allows exactly ONE attempt to connect, and then it gives up. You really should make multiple attempts to open the Serial Port device. The first is likely to fail, but subsequent ones should succeed. Where USB is involved, NEVER assume everything will work perfectly on the first try.

the c# console program has no problem connecting to arduino, it can send data to it fine, just could never receive anything. i tried multiple times, all the same

try adding:

   _serialPort.RtsEnable = true;
   _serialPort.DtrEnable = true;

to your C# Serial port initialization.

4 Likes

I seem to recall setting appropriate Rx and Tx timeouts was also critical.

this solves the problem, thanks!

Why does SerialUSB in arduino side watch these two controls? and how?

BTW, it seems I just need to set anyone of the two to be true to make it work. If both are false, which is actually the setting in TeraTerm and PuTTY, arduino won't send data to PC.

This makes no sense to me, feels like a bug in arduino side

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.