-
Notifications
You must be signed in to change notification settings - Fork 5
Serial
- Setup
- Usage
- How to Communicate with a PC Application
- Misc notes on ARM Teensy USB Serial when it is compiled in:
The easiest way to communicate with a PC is using the virtual serial port provided by the integrated USB controller. To enable it you first need to select Serial in the Tools|USB Type menue entry.
Note: The Teensies ship with the virtual serial port disabled.
After enabling the virtual serial port you can use the Serial object to read from and write to the port. The following example shows how to print the current value of millis() every 100ms.
void setup()
{
// no baudrate setting requried for USB serial
}
void loop()
{
Serial.println(millis());
delay(100);
}
If you now open the Serial Monitor you should see something like this:
Please note that there is no need to set a baudrate. Communication over the virtual serial port is always done with full USB speed which is (net rates, actual tranmission rates will be smaller)
- 12 MBit/s for the T3.X boards, and
- 480 MBit/s for the T4.X boards.
Since Teensies boot up very fast, they will be running your code before the PC has even detected the Teensy and loaded the corresponding USB drivers. Thus, if you don't wait until the PC is ready your code will send out data before the PC can receive it. In the code above this doesn't reall matter but if you need to avoid this you should check if the USB host already established a connection before you start sending data.
void setup()
{
while(!Serial){} //wait until the connection to the PC is established
Serial.println("Start");
}
void loop()
{
}
This code works nicely but if you do not connect the Teensy to an USB Host the while(!Serial){}
will wait forever and the rest of your code will never start, which can be very confusing. To allow for start even if no PC is connected you can add a timeout the connection checking:
void setup()
{
while(!Serial && millis() < 1000){} //wait (at most 1000 ms) until the connection to the PC is established
Serial.println("Start");
}
void loop()
{
}
TBD
TBD
TBD
TBD
TBD
- Teensy uses native USB hardware - not a converter chip
- No exposed pins are used or shared for USB
- Teensy is ready to use in less than 300 ms - but waits until then to enter setup()
- Using "Serial.begin(115200);" is optional and USB has begun before setup() entry
- As indicated the Host for Teensy device may never connect, or take some time after setup() entry
Teensy is a PJRC trademark. Notes here are for reference and will typically refer to the ARM variants unless noted.