Next: Special Characters, Previous: Local Modes, Up: Terminal Modes

#### 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)

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

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

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

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

> 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)

> 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)

> 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.

The functions `cfsetospeed` and `cfsetispeed` 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, `tcsetattr` returns -1.

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
         B9600  B19200  B38400  B57600  B115200
         B230400  B460800

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.