1

You initialize the serial communication via USB with

Serial.begin(baudrate);

where baudrate is a long specifying the number of symbols you want to send over the connection per second. You can put pretty much any number in there, but not every number makes sense.

One thing that I found odd though is that I can't seem to actually use a different baudrate than 9600. The sketch compiles and uploads just fine, but if I check the actual speed of the serial port via

setserial -ag port

where port is the port of my Teensy 4.0 board (that command is something you can use in the terminal of Linux) or with

Serial.baud();

directly in the sketch it always says 9600. I suspect that this is caused by something inside the code behind Serial.begin() since I can manually set the baudrate via the terminal in Linux using

stty speed baudrate < port > port

where (again) baudrate is the speed I want to use and port is the port of my board (checking it with the first terminal command: it works). But when restarting the program on the board the baudrate reverts to 9600.

Does anyone know why this is and how to fix this?

LukasFun
  • 315
  • 1
  • 4
  • 15

2 Answers2

3

The whole concept of Baud Rate with USB communication is completely meaningless. There is no such thing as "baud rate" over USB.

What there is, and what you are confusing with "baud rate" is a configuration item which the host can send to the device which is a "I would like you to communicate with other devices at this speed" configuration item.

This is intended for use with USB to UART adaptors where it is used to configure the baud rate of a physical UART interface for downstream communication.

When you set the baud rate in your code it is ignored, since it has no meaning.

When you set the baud rate on your computer it sends it as this configuration value, which you can then retrieve, should you have an interest in that value, using the Serial.baud() function.

Majenko
  • 103,876
  • 5
  • 75
  • 133
  • Of course there is a baudrate over USB. There is one for every communication. Do you mean to say that the Arduino IDE cannot change it? – LukasFun Nov 21 '19 at 16:38
  • @LukasFun No, there is no baud rate. There is a data rate, but that is largely unrelated to the actual rate of communication between the host and any one device, since it is a packet-based protocol. – Majenko Nov 21 '19 at 16:39
  • So... how do you control the data rate of the USB connection then, I guess? And why would that rate not matter? – LukasFun Nov 21 '19 at 16:41
  • You don't control the data rate. The data rate is defined by the class of the interface and device. USB 1 ("Full Speed") is 12Mbps. USB 2 ("High Speed") is 480Mbps. USB 3 ("Super Speed") is 5Gbps. The chosen speed is the highest that both the device (Arduino) and host (computer) can work together at. – Majenko Nov 21 '19 at 16:43
  • That data rate is divided amongst all devices on the bus. – Majenko Nov 21 '19 at 16:44
  • Oh, and your device never sends data. It enqueues data, and it's the responsibility of the host to come and collect that data. – Majenko Nov 21 '19 at 16:46
  • So, that means, that on USB native Arduino's the baudrate for communication over USB does not have any effect? – chrisl Nov 21 '19 at 16:48
  • Correct. The parameter given to `Serial.begin(...)` is completely ignored. – Majenko Nov 21 '19 at 17:00
  • 1
    Sorry for coming back at this again. While the communication rate of the USB port per se may be fixed, this does not mean that the serial port is actually doing what I want (communicate with maximum speed). That's because the serial port used for this communication is only emulated via the USB port. And the connection of that _emulated_ port should be customizable. It is, in fact, as I specified in my question but is reset every time the controller runs a sketch. Is there any way to change the speed of the connection? I actually tested the speed and it was about 60000 Bits/s. Quite slow. – LukasFun Nov 28 '19 at 16:07
  • @LukasFun The port actually runs at the highest speed it can. It's not a fixed speed, but sends data at as fast a rate as the USB channel allows. The speed "resets" because every time the sketch runs the CDC/ACM port gets destroyed and recreated. It's effectively a completely new device, so it gets the system default settings. But since that "baud rate" has no meaning, changing it is irrelevant. – Majenko Nov 28 '19 at 16:13
  • The "baud rate" is sometimes used to make the device do different things - for instance changing the baud rate to 1200 can cause the Arduino to reset into the bootloader for programming. I use 300 baud on a device I sell to enter a special lowlevel programming protocol mode. – Majenko Nov 28 '19 at 16:14
  • The speed you obtain depends on many factors - what else is on the same USB bus, how powerful the target device is (how fast it can process the USB packets as they arrive), etc. – Majenko Nov 28 '19 at 16:15
  • To put it another way: forget that it's an Arduino you're using and imagine it's an FT232 adaptor. When you set the baud rate you aren't setting the baud rate of the USB connection. You're informing the FT232 of the baud rate you would like *it* to communicate with whatever it is connected to at. – Majenko Nov 28 '19 at 16:40
  • are you thinking of this purely in terms of USB? surely the serial (ie: RS232) communication itself that the USB generates must be at some particular baud rate to be able to communicate correctly with devices. the serial communication cannot be faster than the device expects or it will not be received properly. – Dave Cousineau Jul 28 '22 at 23:17
0

The problem may be in flow control, as LukasFun suggested. Obviously, a low speed RS232 device will not be able to handle a continuous data stream at 1.5 Mbps

A quick look at this paper may help. https://www.usconverters.com/index.php?main_page=page&id=49&chapter=0

Rob
  • 1