Arduino Leonardo - Serial Port

Hello!

I spend hours of searching & reading similar posts.

Im using an Arduino Leonard as a HID (Keyboard) with an NFC-Reader which types in login details.

Everything works fine, but im not able to use the serial port.

Without the following code the serial connection (Used for output debug messages & sending commands to change the login) details) the serial connection NEVER works.

  while (!Serial) {
    ; 
  }

If i use the code, the arduino stucks in the while loop UNTIL i manually open the Arduino IDE and click on the serial icon, then it works to the point when i close the IDE / replug the arduino.

I have tested using Keyboard.begin(); before & after the serial begin.

Im not having any problems letting it act as a keyboard, that works fine.

You cannot use the USB connection as a keyboard and as a serial port at the same time.

The code you posted is the normal code for using the USB connection as a serial port. I don't understand why you would expect it to work without that?

Please post your complete program.

...R

Just use serial1 for your debug but remember you will need a separate USB-serial adapter. Yes, you will have to use 2 USB ports on your PC.

More details about this here.

Robin2:
You cannot use the USB connection as a keyboard and as a serial port at the same time.

The code you posted is the normal code for using the USB connection as a serial port. I don't understand why you would expect it to work without that?

Please post your complete program.

...R

I meand, it only works with

 while (!Serial) {
    ; 
  }

I know that what i posted above in my last post is the default code for a serial connection.

So you are basically confirming me, that using HID (Keyboard) & Serial connection over one usb connection is not working?

Why both is working while i have the IDE (with serial monitor) open then?
They keyboard commands are recived & at the same time my serial monitor recives data.

I've read different things about, it's possible & it's not possible and im not sure what the case is now.
Thats one of the reasons why i started this thread here.

When you have

 while (!Serial) {
    ; 
  }

not in the setup and only trigger that peace of code, for example, when a button is pressed or a specified tag is scanned, in the loop void, then keyboard & serial are working at the same time.

 if(id == valid_id) {
      Serial.println("ID: " + id);
      Serial.println("Access granted");
      Keyboard.write('!');
      delay(2000);
    } else if(id == program_id) {
      Serial.begin(9600);
      while(!Serial) {
        ;
      }
    } else {
         //access denied
    }

That basically proofs, that it's possible right?

Just the

while(!Serial) {
        ;
      }

in the setup, blocks everything, until a serial connection is opened, so that cannot be implemented as default, because the reader would not start reading without a open serial connection. Does (!Serial), wait for a open serial connection, or does it wait for the serial thing to initialize?

Does (!Serial), wait for a open serial connection, or does it wait for the serial thing to initialize?

It's been years since I have looked at the implementation in detail, but from what I remember, Leo'ish boards have a habit of having potential 'garbage' in the serial input buffer. The code you reference simply clears the buffer to an empty state before the remainder of the sketch moves forward.

Ray

Janomine:
Why both is working while i have the IDE (with serial monitor) open then?
They keyboard commands are recived & at the same time my serial monitor recives data.

I have no idea and you have not posted the complete program.

...R

I asked a simple question & said that "You cannot use the USB connection as a keyboard and as a serial port at the same time." WHy should i post the rest of the source which is just about NFC & does not have anything todo with the serial problem? That does not make any sense...

I've also edited my latest post.

Again, if i have the IDE serial monitor open, serial communication & keyboard.write works fine at the same time.

Without the serial monitor open the serial connection will not work and

while(!Serial) {
        ;
      }

stucks forever until i open a serial connection, like the IDE serial monitor. That means i cannot initialize the serial connection on the setup, when i want use the leonardo normally. I have to trigger the serial while in the loop void manually after the setup, if i want to use it.

That does simple means, that while(!Serial) waits for a serial connection to be opened by the pc?

Janomine:
WHy should i post the rest of the source

Perhaps because you would like help solving your problem?

How many problems have you successfully solved when you have only been allowed to see part of the problem?

You don't have to read too many Threads to discover that the problem is very often in the place where it is not suspected.

I can't know whether the rest of the code is relevant without seeing it.

...R

The basic while(!Serial) waits forever. This is useful for a device that must wait for the serial monitor and has no meaningful function without it. For most of my projects, where the serial monitor may or may not be connected, I add a timeout:

while(!Serial && millis()<5000) {
  //wait up to 5 sec to see if the serial monitor connects to the USB serial
}

MorganS:
The basic while(!Serial) waits forever. This is useful for a device that must wait for the serial monitor and has no meaningful function without it. For most of my projects, where the serial monitor may or may not be connected, I add a timeout:

while(!Serial && millis()<5000) {

//wait up to 5 sec to see if the serial monitor connects to the USB serial
}

Great, thank you! So basically, while(!Serial) waits for a serial connection to be opened to the arduino right?

Janomine:
while(!Serial) waits for a serial connection to be opened to the arduino right?

Your description is not sufficiently precise to be able to say that you are right.
while (!Serial) waits to see if the Leonardo has created a serial port within itself. It will work even if the Arduino is not connected to anything (assuming it gets power from somewhere).

Seems I was all wrong - apologies.

See below.

...R

Robin2:
Your description is not sufficiently precise to be able to say that you are right.

while (!Serial) waits to see if the Leonardo has created a serial port within itself. It will work even if the Arduino is not connected to anything (assuming it gets power from somewhere).

...R

No. On the USB Arduinos and the Teensys, Serial doesn't return true unless there is an active serial port created on the computer end. It will wait forever if there is no USB cable plugged in or you never open the serial monitor.

However it is less useful than you might think as it doesn't go back to false when the computer's serial port is closed or unplugged.

MorganS:
No. On the USB Arduinos and the Teensys, Serial doesn't return true unless there is an active serial port created on the computer end. It will wait forever if there is no USB cable plugged in or you never open the serial monitor.

I was not aware of that. I ran a test and you are correct (not that I really doubted you).

void setup() {
	Serial1.begin(9600);
	Serial1.println("Starting LeonardoSerialTest.ino");

	while (!Serial);
	Serial.begin(9600);
	Serial.println("Serial has started");
	Serial1.println("Serial has started");
}

void loop() {
}

When I ran the Leonardo from an FTDI cable using Serial1 for output I got the first message but not the final one - indicating that it was waiting.

Plugging in the USB cable had no effect - it continued waiting.

When I opened the serial port for the USB cable it stopped waiting and printed the other messages.

...R