Is there any way to check which baud rates are supported on a serial device?

Is there any way to check which baud rates are supported for a serial device on Linux?

I've poked around the /sys/class/tty/ttyS0 directory, but I can't see anything in there that lists this type of information.


You seem to be asking two different questions.

Is there any way to check which baud rates are supported on a serial device?

The answer would depend on (1) the capabilities of the hardware, i.e. the UART/USART/SCC, and the range of divisors that the device driver can use in the baud rate generator; consult the device data sheet; (2) the frequency of the clock/oscillator connected to the serial port device; consult the board documentation.

Is there any way to check which baud rates are supported on Linux?

The one of the defined baud rates in include/asm-generic/termbits.h for the c_cflag member of the terminal control structure is the typical method that the serial port (i.e. UART/USART) device driver receives for the baud rate configuration value.

#define  B0     0000000         /* hang up */
#define  B50    0000001
#define  B75    0000002
#define  B110   0000003
#define  B134   0000004
#define  B150   0000005
#define  B200   0000006
#define  B300   0000007
#define  B600   0000010
#define  B1200  0000011
#define  B1800  0000012
#define  B2400  0000013
#define  B4800  0000014
#define  B9600  0000015
#define  B19200 0000016
#define  B38400 0000017

#define    BOTHER 0010000
#define    B57600 0010001
#define   B115200 0010002
#define   B230400 0010003
#define   B460800 0010004
#define   B500000 0010005
#define   B576000 0010006
#define   B921600 0010007
#define  B1000000 0010010
#define  B1152000 0010011
#define  B1500000 0010012
#define  B2000000 0010013
#define  B2500000 0010014
#define  B3000000 0010015
#define  B3500000 0010016
#define  B4000000 0010017

Serial port drivers typically do not have any means of reporting/advertising which of these baud rates are actually supported/configurable/implemented. There is a capabilities value for attributes like FIFO and sleeping but not for baud rates. A driver could define an ioctl() call to configure (nonstandard) baud rates, although that would make programs using it non-portable.


Little bash script:

From sawdust's answer, there is my solution:

for bauds in $(
    sed -r 's/^#define\s+B([1-9][0-9]+)\s+.*/\1/p;d' < \
        /usr/include/asm-generic/termbits.h ) ;do
    echo $bauds
    stty -F /dev/ttyS0 $bauds && echo Ok.
done  2>&1 |
    pr -at2

Will render on my host:

50                              Ok.
75                              Ok.
110                             Ok.
134                             Ok.
150                             Ok.
200                             Ok.
300                             Ok.
600                             Ok.
1200                                    Ok.
1800                                    Ok.
2400                                    Ok.
4800                                    Ok.
9600                                    Ok.
19200                                   Ok.
38400                                   Ok.
57600                                   Ok.
115200                                  Ok.
230400                                  Ok.
460800                                  Ok.
500000                                  Ok.
576000                                  Ok.
921600                                  Ok.
1000000                                 Ok.
1152000                                 Ok.
1500000                                 Ok.
2000000                                 stty: /dev/ttyS0: unable to perform
2500000                                 stty: /dev/ttyS0: unable to perform
3000000                                 stty: /dev/ttyS0: unable to perform
3500000                                 stty: /dev/ttyS0: unable to perform
4000000                                 stty: /dev/ttyS0: unable to perform

That is, but this won't mean it will work!

You have to test them with your cable and your device...


You can check the device baud rate using the "stty" command on the console:

$ stty < /dev/tty..      (where tty... is the device file you are listening)  

output:

speed 9600 baud; line = 0;
-brkint -imaxbel

You can also change the baud rate with the following command:

$ sudo stty -F /dev/tty... 9600    (or whatever baud rate number)