Using the Serial Port on Teensy boards from Arduino code

This tutorial shows how to use the serial port on the Teensy boards to communicate with the Arduino code running on it. We will create a basic Teensy project with Visual Studio and modify it to input and output data via the USB serial port provided by the Teensy Arduino core.

Before you begin, follow our basic Teensy tutorial to get started with Teensy and VisualGDB.

  1. Start Visual Studio and open the VisualGDB Arduino Project Wizard:
  2. On the first page of the wizard select Create a new “Blinking LED” project:
  3. On the next page select your Teensy board. If the Teensy targets do not appear, follow our basic Teensy tutorial to import the Teensy files into VisualGDB:
  4. As the Teensy boards to not support JTAG/SWD debugging, select “Debug Methods -> Built-in GDB Simulator” on the Debug Method page. This will allow continuing the wizard, although the simulator won’t be able to emulate the Teensy board:
  5. Press “Finish” to create the project. Once it is created and loaded, open VisualGDB Project Properties and locate the “USB Type” setting:The Teensy board is capable of emulating numerous different USB peripherals and their combinations. In this tutorial we will only use the virtual serial port, so ensure “Serial” is selected as the USB type.
  6. VisualGDB will automatically scan the code of the Teensy core and configure IntelliSense accordingly. E.g. it will suggest the available serial ports and their methods:
  7. We will now use the low-level write() method of the Serial object to output some text. Replace the loop() function with the code below:
    void loop()
        Serial.write("On\r\n", 4);
        digitalWrite(LED_BUILTIN, HIGH);
        Serial.write("Off\r\n", 5);
        digitalWrite(LED_BUILTIN, LOW);

  8. Build the code and program it into the Teensy board by pressing the programming button (see the basic Teensy tutorial for more details). Once the programming is complete, the board will get recognized as a USB-based COM port. Open VisualGDB Project Properties and select the port in the “COM port for uploading the sketch” field:
  9. Close VisualGDB Project Properties, right-click in Solution Explorer and select “Open Serial Terminal”:
  10. VisualGDB will open a terminal window showing the output from the loop() function:
  11. Now we will demonstrate some more high-level serial port API:
    1. The readStringUntil() method will be used to read the input line-by-line.
    2. The printf() method will be used to write formatted text output

  12. Replace the contents of the loop() function with the code below:
            Serial.print("Write something:\r\n");
            auto line = Serial.readStringUntil('\n');
            Serial.printf("You wrote \"%s\"\r\n", line.c_str());

    Then program the board and reconnect the serial terminal:You will observe that the printf() method works as expected, however the readStringUntil() method times out.

  13. The lack of a hardware debugging interface makes it harder to understand what is going on, so we will rely on VisualGDB’s code jumps to quickly navigate the function call chains:
  14. Go to the timedRead() function and note code that checks the _timeout variable:
  15. Hover the mouse over the _timeout word and select “Code/Data Relations”. VisualGDB will immediately show that the timeout is set by the setTimeout() method and read by 4 other methods:
  16. Add a call to setTimeout() before calling readStringUntil(). The example will now work as expected, waiting for you to enter the entire line of text: