global-variablestermiosbaud-rate

What is the baudrate limit in the termios.h?


This is a quick snippet of code from a serial program I've been working with to interface with a microcontroller. The code has been verified to work but I want to add the global define to make the code more modular. The excerpt shown works, until I replace the 'B1000000' in the 'cfsetispeed' with the global 'BAUDRATE'.

// Globals
struct termios tty;
char BAUDRATE = B1000000;     // 1,000,000

// All of the other details omitted ( int main (), etc. )
cfsetospeed (&tty, BAUDRATE);
cfsetispeed (&tty, B1000000);

So two questions come to mind:

1) I read that Termios only allows for select baudrates, the max listed is 230,400. How is it that 1,000,000 is allowed?

2) Why would cfsetispeed( ) not allow a global char definition as an argument?


Solution

    1. Termios accepts baudrates as a bit flag but there are other baud rates available to the speed_t structure in termbits.h (linked code for kernel v5.3.11) that are not listed on the man7 page or linux.die.net that range from 460800 to 4000000.

    EDIT: The previous link provided died and I managed to find the equivalent in the newer version so here is the excerpt in case it dies again:

    #define CBAUDEX 0010000
    #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
    
    1. cfsetispeed accepts baud rates as being of type speed_t. speed_t is typedef'ed to be an object of type "unsigned int" in termbits.h, which is larger (32-bits vs 8-bits) than the char you are passing in.