Opening serial com port in C# made Serial status not ready in Leonard

Hello,

One problem has been annoying me as follows.

The following is a simple Leonard code doing the two jobs,

  1. Periodically sending "test" message to UART.
  2. Periodically toggling D0 digital port.

I monitored D0 pin by Oscilloscope and it started toggling when I download the code into Leonard.
Also, if I connecetd TeraTerm(terminal software) to Leonard via virtual COM,
it displayed "test" repeatedly. Until here, those were as expected.

Then, I closed teraterm and tried to connect C# UART program to Leonard via virtual COM.
Once I opened the COM port in my C#, D0 started to toggle more quickly and it means serial port is not available. (See the code below. Delay is larger.)

Then, I closed C# and connected TeraTerm back. It showed slower toggling again and "test" message is displayed correctly.

I compared serial com port setting between TeraTerm and C# but I did not see any difference.
DataBit=8bit, no parity, stopbit1, No handshake.

In C#, COM port status was "open" and no error when I run it.

Q1. Any idea why Serial status becomes not ready when C# tried to open serial com port?

Then, although Serial status was not ready, I tried to force to send text data to Leonard.
Leonard could catch the text message even with Serial not ready. However, sending text from Leonard to PC was still not success. I think it is because Serial status is not ready.
This behavior increased my doubt.

=== Code in Leonard =============================
int Data = 1;
void setup() {
pinMode(0, OUTPUT);
digitalWrite(0, Data);

Serial.begin(9600);
}

void loop() {
if(Serial) {
Serial.print("test\n");
delay(200);
} else {
delay(1);
}

Data = !Data;
digitalWrite(0, Data);
}

Yoshi_K:
Q1. Any idea why Serial status becomes not ready when C# tried to open serial com port?

That sounds like a problem with your C# code and this is an Arduino Forum.

But maybe if you post the C# code some one will be able to help.

And please use the code button </> so your code looks like this and is easy to copy to a text editor

Do you have an Uno or Mega that you can try? Their serial connection is more predictable than the Leonardo's

...R

Did you try to add the line while (!Serial) ;to setup?

Opening the serial connection resets the attached Arduinos (normally),
the Leonardo seems to need some extra time.

One thread that discusses some aspects Micro and while (!Serial)

Whandall:
Opening the serial connection resets the attached Arduinos (normally),

Not with a Leonardo. This can be a problem if you need it to reset but I don't think that is an issue with the OP's program.

To force a Leonardo to reset you need to open the serial port at 1200 baud and then close it again. That will cause the Leonardo to disappear from the serial connection until it restarts and re-establishes its connection. Then the PC can open the serial port at the "proper" baud rate.

...R

Hello Everyone,

After some investigation, I found the cause.
Virtual COM of Leonard seemed requiring that either of Dts or Rts (or both) is enabled as follows.

== C# on Windows7 ===============
SerialPort sp;
sp.DtrEnable = true;
sp.RtsEnable = true;
== end of C# on Windows7 ===========

Without those, "Serial" in Leonard code always return FALSE.

I checked some example codes on the Internet before but all of those did not show those parameters
have to be enabled and hence I did not notice.

I hope this helps anyone who will see same problem in the future.

Regards
Yoshi

Virtual COM of Leonard seemed requiring that either of Dts or Rts (or both) is enabled as follows.

Does "Leonard" stand for "Leonardo"?

Such a case I have to protest. Although I'm not italian (I wouldn't care), to say "Leonard" instead of Leonardo is an insult for latins. It is like saying "BalonpiƩ" instead of "football (soccer; another insult in itself)" in an english forum; like saying "The Elisean Champs" instead of "Les Champs ElyseƩs" in a french one; like saying "going to sleep" in an Spanish forum instead of "me voy a echar una siesta".

Is it clear?

:grin:

After some investigation, I found the cause.
Virtual COM of Leonard seemed requiring that either of Dts or Rts (or both) is enabled as follows.

It is the DtrEnable that needs to be set.