-1

I have an Arduino Uno WiFi Rev2 which I am trying to read data over the serial port from (using C#). The Arduino program periodically emits data to the serial port (for testing purposes). Whenever the Arduino writes to the serial, it also toggles the onboard LED to prove it is still running.

Everything seems to run fine. The LED periodically blinks and thus proves the loop runs as expected. Using the Serial Monitor on the Arduino IDE also correctly echoes the data that the Arduino emits.

Here is the program:

int keepAlive = 500;
unsigned long lastKeepAlive = 0;
char ledState = 0;

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (millis() > lastKeepAlive + keepAlive) {
    lastKeepAlive = millis();
    Serial.println("Alive");
    if (ledState == 0) ledState = 1; else ledState = 0;
    digitalWrite(LED_BUILTIN, ledState);
  }
  delay(5);
}

Another reason why I know it works is because the TX LED on the Arduino blinks whenever it writes on the port.

However, once I close the IDE, the Arduino "stops" writing to the serial port. Maybe because it doesn't have to - there is no connection. Fine with me. Also: the TX-LED stops blinking while the onboard-LED keeps toggling on and off. So the loop still runs fine.

Now I'd like to read out the serial data via C#. For that I've written a simple program like so:

var port = new SerialPort("COM6", 115200, Parity.None, 8, StopBits.One);
port.Open();
port.Write("123");
while (true)
{
  Console.WriteLine(port.BytesToRead);
  await Task.Delay(1000);
}

I expect this program to tell me how many bytes are waiting to be read. But it keeps printing 0 endlessly. Also: the TX-LED does not light up on the Arduino. On the other hand, note that little port.Write in the program - it's a test to verify the communication between the Arduino and C#. In fact: I can briefly see the RX-LED on the Arduino light up when my program sends the 123. I reckon the port and it's settings are correct then.

When I close my program, and the open up the IDE again, the Arduino starts to emit serial data again, the TX-LED starts blinking again and data arrives in the Serial Monitor.

All the while the onboard-LED keeps toggling on and off merrily.

What am I doing wrong? Why is the Arduino talking to the IDE but not to my program? Do I have to initiate some kind of handshake?

3
  • 1
    something wrong with the C# program ... that is off topic here
    – jsotola
    Jul 4, 2023 at 22:17
  • the arduino cannot choose which program receives data from the serial port on your computer ... find a C# tutorial about receiving data from the serial port
    – jsotola
    Jul 4, 2023 at 23:15
  • @jsotola Did you notice the statement "the TX-LED stops blinking while the onboard-LED keeps toggling on and off"? To me, the problem might not be the PC program. Since I don't have hardware available, I cannot reproduce, but it seems that some kind of hardware handshake is blocking. Jul 5, 2023 at 8:01

1 Answer 1

0

I have solved the issue and @jsotola was correct in pointing out the problem lay with the C# program.

What mislead me was the fact that the TX and RX leds apparently only blink when data is read from the respective buffer, not when it is written to. In other words: the Arduino kept happily writing to the serial port (as expected) but the TX led did not light up because no one was reading what it wrote. I dind't know that.

For anyone else having this issue: I had to set DtrEnable to true. This is still a little confusing to me though. I am not new to programming in C# and with serial ports at all. Yet, I have never had to do this. Also, according to documentation this flag is used when using XOn/XOff or RTS handshake. None of which I am using.

I also tried with PuTTY. I did not configure anything aside from the port name and the baud rate and it worked right away. Investigating, I found that PuTTY apparently defaults to XOn/XOff.

So I'm guessing it really is XOn/XOff then ...

I updated my program like this:

var port = new SerialPort("COM6", 115200, Parity.None, 8, StopBits.One);
port.DtrEnable = true;
port.Open();
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.