17.4.8 Line Speed

The terminal line speed tells the computer how fast to read and write data on the terminal.

If the terminal is connected to a real serial line, the terminal speed you specify actually controls the line—if it doesn’t match the terminal’s own idea of the speed, communication does not work. Real serial ports accept only certain standard speeds. Also, particular hardware may not support even all the standard speeds. Specifying a speed of zero hangs up a dialup connection and turns off modem control signals.

If the terminal is not a real serial line (for example, if it is a network connection), then the line speed won’t really affect data transmission speed, but some programs will use it to determine the amount of padding needed. It’s best to specify a line speed value that matches the actual speed of the actual terminal, but you can safely experiment with different values to vary the amount of padding.

There are actually two line speeds for each terminal, one for input and one for output. You can set them independently, but most often terminals use the same speed for both directions.

The speed values are stored in the struct termios structure, but don’t try to access them in the struct termios structure directly. Instead, you should use the following functions to read and store them:

Function: speed_t cfgetospeed (const struct termios *termios-p)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the output line speed stored in the structure *termios-p.

Function: speed_t cfgetispeed (const struct termios *termios-p)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the input line speed stored in the structure *termios-p.

Function: int cfsetospeed (struct termios *termios-p, speed_t speed)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function stores speed in *termios-p as the output speed. The normal return value is 0; a value of -1 indicates an error. If speed is not a speed, cfsetospeed returns -1.

Function: int cfsetispeed (struct termios *termios-p, speed_t speed)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function stores speed in *termios-p as the input speed. The normal return value is 0; a value of -1 indicates an error. If speed is not a speed, cfsetospeed returns -1.

Function: int cfsetspeed (struct termios *termios-p, speed_t speed)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function stores speed in *termios-p as both the input and output speeds. The normal return value is 0; a value of -1 indicates an error. If speed is not a speed, cfsetspeed returns -1. This function is an extension in 4.4 BSD.

Data Type: speed_t

The speed_t type is an unsigned integer data type used to represent line speeds.

Function: speed_t cfgetospeed (const struct termios *termios-p)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the output line speed stored in the structure *termios-p.

Portability note: In the GNU C Library, the functions above accept speeds measured in bits per second as input, and return speed values measured in bits per second. Other libraries require speeds to be indicated by special codes. For POSIX.1 portability, you must use one of the following symbols to represent the speed; their precise numeric values are system-dependent, but each name has a fixed meaning: B110 stands for 110 bps, B300 for 300 bps, and so on. There is no portable way to represent any speed but these, but these are the only speeds that typical serial lines can support.

B0  B50  B75  B110  B134  B150  B200  B300  B600  B1200
B1800  B2400  B4800  B7200  B9600  B14400  B19200
B28800  B33600  B38400  B57600  B76800  B115200
B153600  B230400  B307200  B460800  B500000
B576000  B614400  B921600  B1000000  B1152000
B1500000  B2000000  B2500000  B3000000
B3500000  B4000000  B5000000  B10000000

BSD defines two additional speed symbols as aliases: EXTA is an alias for B19200 and EXTB is an alias for B38400. These aliases are obsolete.

Function: baud_t cfgetibaud (const struct termios *termios-p)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function returns the input line baud rate stored in the structure *termios-p.

Function: int cfsetobaud (struct termios *termios-p, baud_t baud)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function stores baud in *termios-p as the output baud rate. The normal return value is 0; a value of -1 indicates an error. If baud is not a valid baud rate, cfsetobaud returns -1.

Function: int cfsetibaud (struct termios *termios-p, baud_t baud)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function stores baud in *termios-p as the input baud rate. The normal return value is 0; a value of -1 indicates an error. If baud is not a valid baud rate, cfsetobaud returns -1.

Function: int cfsetbaud (struct termios *termios-p, baud_t baud)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This function stores baud in *termios-p as both the input and output baud rates. The normal return value is 0; a value of -1 indicates an error. If baud is not a valid baud rate, cfsetbaud returns -1.

Data Type: baud_t

The baud_t type is a numeric data type used to represent line baud rates. It will always represent the actual numeric value representing the baud rate, unlike speed_t. In the current version of the GNU C Library this is the same type as speed_t, but this may not be the case in future versions, or on other implementations; it may not even necessarily be an integer type.

The functions cfsetospeed, cfsetispeed, cfsetobaud and cfsetibaud report errors only for speed values that the system simply cannot handle. If you specify a speed value that is basically acceptable, then those functions will succeed. But they do not check that a particular hardware device can actually support the specified speeds—in fact, they don’t know which device you plan to set the speed for. If you use tcsetattr to set the speed of a particular device to a value that it cannot handle, either tcsetattr returns -1, or the value is adjusted to the closest supported value, depending on the policy of the kernel driver.