Defining -DUSB_DUAL_SERIAL in teensy 4.1 build causes upload and monitor to fail

Using the USB_DUAL_SERIAL build flag in teensy41 builds on a Ubuntu 22.04 Linux system causes the Upload and Monitor task to fail.

Uploading .pio/build/ssd1306/firmware.hex
==================== [SUCCESS] Took 2.33 seconds ====================
Error: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'

 *  The terminal process "platformio 'run', '--target', 'upload', '--target', 'monitor', '--environment', 'ssd1306', '--upload-port', '/dev/ttyACM0', '--monitor-port', '/dev/ttyACM0'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

I believe this may be related to the teensy often enumerating the second USB CDC channel (02) before the first one (00). I have come across this before and tried to fix it by overriding the monitor port in platformio.ini to use /dev/serial/by-id/xxxxxxxxx but I did not get consistent behaviour.

I think platformio picks up the first serial device that is enumerated by udev and tries to use that as the monitor device without checking that it is the correct CDC channel, or it does not wait long enough for the device nodes and symlinks to settle down.

Any ideas on how to fix this?

Where do find the code that does this in the PlatformIO source?



It works when “Upload” and “Monitor” tasks are executed separately?

Sorry about the delay in replying. I must have missed a notification.

Once it has hit the problem, you can no longer upload to the board. This is because PlatformIO now thinks that SerialUSB1 (/dev/ttyACM1 on my Linux box) is the the upload port. You have to do a 15 second boot button reset on the teensy to clear this. You then have to come out of Visual Studio and then go back in again to get the upload working. However the monitor still does not work.

I think this is probably a problem in the the Teensy platform package.

I have working on this to try and get TeensyDebug (GDB debugging) working in Platformio.

Here is my PlaformIO.ini

platform = teensy
board = teensy41
framework = arduino
lib_deps =
;upload_protocol = teensy-cli    

;build_type = debug
; See
;build_unflags = -DUSB_SERIAL
;platform_packages =
;	framework-arduinoteensy@
build_flags = -DUSB_DUAL_SERIAL
;monitor_port = /dev/serial/by-id/usb-Teensyduino_Triple_Serial_12205920-if00
;debug_port = /dev/serial/by-id/usb-Teensyduino_Triple_Serial_12205920-if02
;debug_speed = 115200
;debug_tool = custom
;debug_load_mode = manual
;debug_server = 
;debug_init_cmds =
;  shell sleep 5
;  target extended-remote $DEBUG_PORT
;  define pio_reset_run_target
;  interrupt
;  tbreak loop
;  continue
;  end
;  define pio_restart_target
;  echo Restart is undefined for now.
;  end
;debug_init_break =

And here is my test sketch.

//#pragma GCC optimize ("O0")

#include <Arduino.h>
//#include "TeensyDebug.h"

int mark = 0;

void blinker(int count = 1) {
        digitalWrite(LED_BUILTIN, 0);
        for (int i = 0; i < count; i++) {
                digitalWrite(LED_BUILTIN, 1);
                digitalWrite(LED_BUILTIN, 0);

void test_function() {

void setup() {
  while (!Serial) { delay(100); } // wait for Serial Monitor to be ready
  Serial.printf("Serial is ready\n");

  while (!SerialUSB1) { delay(1000); } // wait for Serial Debugger to be ready
  SerialUSB1.printf("SerialUSB1 is ready\n");

  // Launch the GDB debugger stub

void loop() {