Arduino UNO serial requires COM port "connect" for Tx, Rx LED to start working.

According to "Serial" documentation there is no way to detect if a serial device is hooked up or not.

The "if (Serial)" is said to return true at all times.

Normally during development the Tx, Rx LEDs on the board are handy to indicate the UNO is sending or receiving data. If the UNO is sending out log data or sign of life on the Tx you can see it flashing.

One problem is the onboard USB hardware is not initiated only by supplying +5V power. The USB initiates when an application opens the USB associated virtual port the first time.

After the USB port has opened once the Arduino USB port LEDs start working.

After the one time connect the USB virtual port can be closed and the application using the port can be closed. Arduino Tx LED will still flash when Arduino sends something.

Would it be possible to know if the USB device has initiated so Arduino code does not waste time on sending data. Alternatively it knows the activities on the serial device can not be seen by anyone not even on the Tx LED. I was thinking of using the onboard LED as an alternative when Tx is found dead.

I would like the onboard UART to intiate at power up and Tx LED working all the time or as an alternative at least know UART is not initiated and LEDS not in operation.

I am using Window 7 to connect to the virtual serial port.

flodis:
Would it be possible to know if the USB device has initiated so Arduino code does not waste time on sending data.

I'm not sure this question is as clear as you may think it is.

An obvious option is for the PC to send a message to the Arduino.

But then there is the question of whether the Arduino resets when the PC program opens the Serial port? It will with an Uno or Mega and it won't with a Leonardo.

I write my PC programs so they listen for a message from the Arduino that is sent from setup() immediately after a restart. See for example this Python - Arduino demo.

It would be easy to extend that concept to require the Arduino to expect a response from the PC, and if it did not get it then it would know that no one is listening.

On the other hand it might just be simpler to let the Arduino send data regardless.

A flashing LED on the Arduino can be very useful to let people know it is alive.

...R

Yes I was thinking some handshake would sort out port activity. Like "hello", "goobye" if someone is listening or not. Not so great to have to implement in all sketches and all port connects.

With unitiated serial the LEDS are dead and you can not see port activity as you did when someone had opened the port just once. Just turning on the PC powering the USB will not make the LEDS come live. Port has to be opened at least once.

Also noticed when the virtual USB port is closed from PC-side and you continue sendning data from the UNO - after some time the Tx LED becomes fully turned on and will only go off when virtual port is re-opened.

Could it be buffer full or serial error on the Arduino side is indicated that way or Windows has some timeout on port resources? Virtual driver maybe enters some state where Adruino can no longer send data over USB is indicated this way?

Just wanted to know how it works since Tx LED continuosly on could make you think you are sendning all the time or something bad has happened.

I think the fully turned on LED is not so nice since classic serial ports without hanshaking do not care if someone is listenning or not. If USB is down I think Tx LED should flash anyway and data be wasted. There is no data buffered on the PC side while port is closed anyway. As you open the virtual port buffers allways start empty.

The idea to flash the Tx LED at all times would indicate Arduino is trying to send something. If serial device is not open or not initiated should not be a worry for Arduino to indicate. But that depends on what the purpose of the Tx LED is?

The positive way to see it, as it works now, is:

Tx LED flashing:
"Data is sent and USB driver has been initiated as port has been successfully opened at least once".

The Tx LED goes yellow:
"The USB port has been successfully initiated by being opened once but now it is not working anymore. Driver may be down, buffer full or some other errorr".

No Tx LED flashing when data sent:
"The virtual USB port has never ben opened OR your code is not sendning anything. Open and close the virtual port to be sure LEDS are working".

flodis:
Just wanted to know how it works since Tx LED continuosly on could make you think you are sendning all the time or something bad has happened.

Just ignore it?
I have an Atmega 328 that runs continuously for days or weeks at a time happily sending data into the ether.

...R