24

I regularly use cat to view debugging information in the console from my FPGA development board over the serial connection, but I never have had to tell linux what the baud rate is. How does cat know what the baud rate of the serial connection is?

  • You did not set up the port e.g. with minicom before? It doesn't work here. Only after I set up the serial port parameters I can use cat. – Marco Apr 19 '13 at 9:11
  • It doesn't set or knows the baudrate, it just reads from the device. – Ulrich Dangel Apr 19 '13 at 9:12
  • @Marco, I don't know if Debian has some default baud rate setting, but I haven't set it anywhere. – stanri Apr 19 '13 at 9:18
33

The stty utility sets or reports on terminal I/O characteristics for the device that is its standard input. These characteristics are used when establishing a connection over that particular medium. cat doesn't know the baud rate as such, it rather prints on the screen information received from the particular connection.

As an example stty -F /dev/ttyACM0 gives the current baud rate for the ttyACM0 device.

  • 1
    but how did stty know about the baud rate then? This answer only defers somehow the question, if the baud rate can be autodetected or was set at some point (i.e. via stty) – humanityANDpeace Apr 3 '18 at 19:51
  • @humanityANDpeace I assume the default baud rate was the one I happened to be using. I later on did need to change it via stty when I changed the baud rate on the device. – stanri Apr 6 '18 at 8:16
9

cat just uses whatever settings the port is already configured for. With this little C snippet you can see the baud rate currently set for a particular serial port:

get-baud-rate.c

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

int main() {
  struct termios tios;
  tcgetattr(0, &tios);
  speed_t ispeed = cfgetispeed(&tios);
  speed_t ospeed = cfgetospeed(&tios);
  printf("baud rate in: 0%o\n", ispeed);
  printf("baud rate out: 0%o\n", ospeed);
  return 0;
}

Run it:

./get-baud-rate < /dev/ttyS0 # or whatever your serial port is

The numbers you get can be looked up in /usr/include/asm-generic/termios.h, where there are #defines such as B9600 etc. Note that the numbers in the header file and in the get-baud-rate output are in octal.

Maybe you can experiment and see what these numbers are like on a fresh boot and whether they change later.

  • 2
    I just found the stty command which does just this. For example, stty -F /dev/ttyACM0 gives me the current baud rate, which is correct for my device. – stanri Apr 19 '13 at 9:33
  • Of course that's a much better idea. – clacke Apr 19 '13 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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