Test if SerialUSB1 is connected

profor

Active member
Hello all,

i am doing this test in my code

Code:
  if  (Serial)  {
//  print info menu after connecting the USB serial
    if  (!bitRead(dbg.status_flags,  FLAG_DBG_SERIAL_CONNECTED)) {
      if  (dbg.wrk_wait < PRESET_DBG_WAIT)  {
        dbg.wrk_wait++;
      } else  {
        bitSet(dbg.status_flags,  FLAG_DBG_SERIAL_CONNECTED);
        dbg.wrk_wait = 0;
        for (uint8_t  q = 0;  q < PRESET_STAGES_SIZE; q++)  {
          dbg.stages[q] = 0;
        }
      
        dbg.stages[IDX_DBG_STAGE] = STAGE_DBG_TOP;
        bitClear(dbg.flags,  FLAG_DBG_MENU_PRINTED);
      }
    }

That works like charm:)

But such test is not possible to do on SerialUSB1 if i select Two serials.
It is not a big deal, but i do sniff on CAN-BUS and that i want to print out on SerialUSB1 keeping Serial free to control the sniff.
If the SerialUSB1 is not connected in windows, when there is output something in my code, it drops the Serial and restarts. Not sure now, if Teensy reboots, did not check yet.
All is good if prior the sniff i connect to the SerialUSB1 port:)
From the code above it is obvious what i want to do with the SerialUSB1, right?

Thanks a lot.
Teensy4 / Teensyduino 1.56
 
Hello,

This is a tricky thing to do. Here is how you can use if(Serial) in the first place:
Code:
        operator bool() { return usb_configuration && (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) &&
		((uint32_t)(systick_millis_count - usb_cdc_line_rtsdtr_millis) >= 15);

So the gist is you need to test (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR), which I believe could tell you whether SerialUSB1 port is open. This variable usb_cdc2_line_rtsdtr is in usb_serial2.c and not properly exposed in usb_serial.h

Here is what usb_serial.h exposes to the rest of the code:

extern volatile uint8_t usb_cdc_line_rtsdtr;

You could add this line to your main code:

extern volatile uint8_t usb_cdc2_line_rtsdtr;

Then use (usb_cdc2line_rtsdtr & USB_SERIAL_DTR) to determine whether this port is open. Good luck!
 
If I have some time I can test also. Most of my teensy software has been heavily modified for my work so I have to find a mint copy first :)
 
After a bit more reading, it seems that there is this already in usb_serial.h so you can just use if(SerialUSB1) like if(Serial).

Code:
        operator bool() { return usb_configuration && (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) &&
                ((uint32_t)(systick_millis_count - usb_cdc2_line_rtsdtr_millis) >= 15);
I tested on my T40 and it works, as long as the terminal program toggles DTR to low. One of my terminal programs does the toggle and the other has to be done manually with a DTR button. Pressing RTS in that terminal didn't trigger the if().

Code:
void setup() {
  // put your setup code here, to run once:
  while (!Serial){}
  Serial.println("Serial started");
  while (!SerialUSB1){}
  Serial.println("SerialUSB1 started");
  
}

void loop() {
  // put your main code here, to run repeatedly:

}
 
It seems like the first code You posted is available in usb_serial.h on line 239.

Code:
        operator bool() { return usb_configuration && (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) &&
                ((uint32_t)(systick_millis_count - usb_cdc2_line_rtsdtr_millis) >= 15);
        }


Having it like this
Code:
extern volatile uint8_t usb_cdc2_line_rtsdtr;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  if  (usb_cdc2line_rtsdtr & USB_SERIAL_DTR)  {

  } else  {
   
  }
}

gives me

Code:
'usb_cdc2line_rtsdtr' was not declared in this scope
 
After a bit more reading, it seems that there is this already in usb_serial.h so you can just use if(SerialUSB1) like if(Serial).

Code:
        operator bool() { return usb_configuration && (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) &&
                ((uint32_t)(systick_millis_count - usb_cdc2_line_rtsdtr_millis) >= 15);
I tested on my T40 and it works, as long as the terminal program toggles DTR to low. One of my terminal programs does the toggle and the other has to be done manually with a DTR button. Pressing RTS in that terminal didn't trigger the if().

Code:
void setup() {
  // put your setup code here, to run once:
  while (!Serial){}
  Serial.println("Serial started");
  while (!SerialUSB1){}
  Serial.println("SerialUSB1 started");
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

OK, will try.
 
It seems like the first code You posted is available in usb_serial.h on line 239.

Code:
        operator bool() { return usb_configuration && (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) &&
                ((uint32_t)(systick_millis_count - usb_cdc2_line_rtsdtr_millis) >= 15);
        }


Having it like this
Code:
extern volatile uint8_t usb_cdc2_line_rtsdtr;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  if  (usb_cdc2line_rtsdtr & USB_SERIAL_DTR)  {

  } else  {
   
  }
}

gives me

Code:
'usb_cdc2line_rtsdtr' was not declared in this scope

There is a missing understore: usb_cdc2_line_rtsdtr instead of usb_cdc2line_rtsdtr. With the underscore, it works like the (SerialUSB1).
 
Back
Top