Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Teensy 4.0 Detect USB presence?

  1. #1

    Teensy 4.0 Detect USB presence?

    I have a section of code that outputs data to the USB serial, but this runs in the loop.
    I want to optimize the code to prevent it from trying to spit that out to USB isn't connected.
    All that concatenation forming work could be avoided in my loop.

    IS there a flag to check if USB serial is connected?

  2. #2
    Senior Member
    Join Date
    Oct 2019
    Location
    Calgary
    Posts
    125
    Something like
    Code:
    if (Serial) { 
       Serial.print(...); 
    }
    should do the trick, I think.

  3. #3
    That was the first thing i tested.

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,317
    You can easily ignore lack of USB connection
    Yes, it fills-up a buffer, but when this buffer is full all further data are dropped.
    You can see that by writing data to USB, disconnecting USB from PC and after some time reconnecting.
    At that point you see that first the last data before disconnecting are arriving followed by new data

    One possibility would be to using Serial.availableForWrite() and stop using it when it returns zero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •