Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArduinoUniqueID example tries to send data before serial comms established #19

Open
ghost opened this issue Apr 13, 2022 · 1 comment
Open

Comments

@ghost
Copy link

ghost commented Apr 13, 2022

Tested on an Arduino Micro the ArduinoUniqueID example tries to send serial data before comms are established and ends up sending nothing! Adding <while (!SerialUSB);> just after serial.begin fixed this issue for me :)
This issue also occurred for the ArduinoUniqueID8 example but the ArduinoUniqueIDSerialUSB example already had it and works all good!

@GralfR
Copy link

GralfR commented Aug 28, 2023

This is the typical behaviour of the native-USB-Arduino. The AVR-Arduino using USB-to-serial are reset as soon as the USB-connection establishes. That's why it seams the Arduino is sending data "at the right time". In fact it just boots up, sends serial data even if nobody is listening and is being reset at the moment the USB-connection is established.
If you want this same behaviour on native USB-Arduino, then the while (!Serial); is not a good solution. It will freeze up the Arduino if no USB-connection is available. So, the Arduino depends on a USB-connection to run. You can check this with a simple flashing LED-sketch, which does not blink until a USB-connection is established.

I just wrote a snippet that emulates the oldfashioned way of e.g. UNO R3 (reset on connection) on a modern native-USB-device like UNO R4. Instead of waiting for connection by while (!Serial); the device checks if (Serial) inside loop and as soon as USB connects, you can do what ever you want (like print a welcome-message to serial device or even reboot). Until then the device was working and not asleep. It's also good to remember an established connection to not to get stuck in reboot-loop.

static bool connected=false;
void setup() {
  Serial.begin(9600);
}
void loop() {
  if (Serial && !connected) {
    connected = true;
    Serial.println("Welcome"); //or whatever; UNO R3 did a reset at this stage
  }
  if (!Serial && connected) {
    connected=false; //recognise the lost connection to again print welcome on new connection
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant