2

I would like my software running on Leonardo to behave differently depending on the baudrate of the USB Serial set by the PC. For example, I want to make a smart USB to UART converter which performs some processing of the data it transmits. It would be much more elegant to set the UART baudrate to whatever value the PC is requesting on USB side, rather than hardcoding a value.

AFAIK 32u4 chip is used as a USB to UART converter on Mega, where it supports different baudrates, so this should be doable.

I also have a Teensy 3.2 board, so if anyone knows how to detect the baudrate on this board I'm also interested.

6

From looking at the source it appears that on 32u4 based boards Serial includes extra methods to access the settings from the USB host:

see: https://github.com/arduino/ArduinoCore-avr/blob/b7c607663fecc232e598f2c0acf419ceb0b7078c/cores/arduino/USBAPI.h#L129

From USBAPI.h:

// These return the settings specified by the USB host for the
// serial port. These aren't really used, but are offered here
// in case a sketch wants to act on these settings.
uint32_t baud();
uint8_t stopbits();
uint8_t paritytype();
uint8_t numbits();
bool dtr();
bool rts();
  • 1
    I tried it out. On Leonardo, Serial.baud() works right after the serial has started, inside setup. On Teensy, Serial.baud() seems to return 0 inside setup, so I wait inside loop until a non-zero value is returned. – Dmitry Grigoryev Aug 30 '18 at 7:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.