4.5. Using the Configuration Utilities

Now let's spend some time looking at the two most useful serial device configuration utilities: setserial and stty.

4.5.1. The setserial Command

The kernel will make its best effort to correctly determine how your serial hardware is configured, but the variations on serial device configuration makes this determination difficult to achieve 100 percent reliably in practice. A good example of where this is a problem is the internal modems we talked about earlier. The UART they use has a 16-byte FIFO buffer, but it looks like a 16450 UART to the kernel device driver: unless we specifically tell the driver that this port is a 16550 device, the kernel will not make use of the extended buffer. Yet another example is that of the dumb 4-port cards that allow sharing of a single IRQ among a number of serial devices. We may have to specifically tell the kernel which IRQ port it's supposed to use, and that IRQs may be shared.

setserial was created to configure the serial driver at runtime. The setserial command is most commonly executed at boot time from a script called 0setserial on some distributions, and rc.serial on others. This script is charged with the responsibility of initializing the serial driver to accommodate any nonstandard or unusual serial hardware in the machine.

The general syntax for the setserial command is:
setserial device [parameters]
in which the device is one of the serial devices, such as ttyS0.

The setserial command has a large number of parameters. The most common of these are described in Table 4-1. For information on the remainder of the parameters, you should refer to the setserial manual page.

Table 4-1. setserial Command-Line Parameters

port port_number

Specify the I/O port address of the serial device. Port numbers should be specified in hexadecimal notation, e.g., 0x2f8.

irq num

Specify the interrupt request line the serial device is using.

uart uart_type

Specify the UART type of the serial device. Common values are 16450, 16550, etc. Setting this value to none will disable this serial device.


Specifying this parameter instructs the kernel serial driver that this port is one port of an AST Fourport card.


Program the UART to use a speed of 57.6 kbps when a process requests 38.4 kbps.


Program the UART to use a speed of 115 kbps when a process requests 38.4 kbps.


Program the UART to use the default speed of 38.4 kbps when requested. This parameter is used to reverse the effect of a spd_hi or spd_vhi performed on the specified serial device.


This parameter will cause the kernel to attempt to automatically determine the IRQ of the specified device. This attempt may not be completely reliable, so it is probably better to think of this as a request for the kernel to guess the IRQ. If you know the IRQ of the device, you should specify that it use the irq parameter instead.


This parameter must be specified in conjunction with the port parameter. When this parameter is supplied, setserial instructs the kernel to attempt to automatically determine the UART type located at the supplied port address. If the auto_irq parameter is also supplied, the kernel attempts to automatically determine the IRQ, too.


This parameter instructs the kernel not to bother performing the UART type test during auto-configuration. This is necessary when the UART is incorrectly detected by the kernel.

A typical and simple rc file to configure your serial ports at boot time might look something like that shown in Example 4-1. Most Linux distributions will include something slightly more sophisticated than this one.

Example 4-1. Example rc.serial setserial Commands

# /etc/rc.serial - serial line configuration script.
# Configure serial devices
/sbin/setserial /dev/ttyS0 auto_irq skip_test autoconfig
/sbin/setserial /dev/ttyS1 auto_irq skip_test autoconfig
/sbin/setserial /dev/ttyS2 auto_irq skip_test autoconfig
/sbin/setserial /dev/ttyS3 auto_irq skip_test autoconfig
# Display serial device configuration
/sbin/setserial -bg /dev/ttyS*

The -bg /dev/ttyS* argument in the last command will print a neatly formatted summary of the hardware configuration of all active serial devices. The output will look like that shown in Example 4-2.

Example 4-2. Output of setserial -bg /dev/ttyS Command

/dev/ttyS0 at 0x03f8 (irq = 4) is a 16550A

4.5.2. The stty Command

The name stty probably means “set tty,” but the stty command can also be used to display a terminal's configuration. Perhaps even more so than setserial, the stty command provides a bewildering number of characteristics you can configure. We'll cover the most important of these in a moment. You can find the rest described in the stty manual page.

The stty command is most commonly used to configure terminal parameters, such as whether characters will be echoed or what key should generate a break signal. We explained earlier that serial devices are tty devices and the stty command is therefore equally applicable to them.

One of the more important uses of the stty for serial devices is to enable hardware handshaking on the device. We talked briefly about hardware handshaking earlier. The default configuration for serial devices is for hardware handshaking to be disabled. This setting allows “three wire” serial cables to work; they don't support the necessary signals for hardware handshaking, and if it were enabled by default, they'd be unable to transmit any characters to change it.

Surprisingly, some serial communications programs don't enable hardware handshaking, so if your modem supports hardware handshaking, you should configure the modem to use it (check your modem manual for what command to use), and also configure your serial device to use it. The stty command has a crtscts flag that enables hardware handshaking on a device; you'll need to use this. The command is probably best issued from the rc.serial file (or equivalent) at boot time using commands like those shown in Example 4-3.

Example 4-3. Example rc.serial stty Commands

stty crtscts < /dev/ttyS0
stty crtscts < /dev/ttyS1
stty crtscts < /dev/ttyS2
stty crtscts < /dev/ttyS3

The stty command works on the current terminal by default, but by using the input redirection (“<”) feature of the shell, we can have stty manipulate any tty device. It's a common mistake to forget whether you are supposed to use “<” or “>”; modern versions of the stty command have a much cleaner syntax for doing this. To use the new syntax, we'd rewrite our sample configuration to look like that shown in Example 4-4.

Example 4-4. Example rc.serial stty Commands Using Modern Syntax

stty crtscts -F /dev/ttyS0
stty crtscts -F /dev/ttyS1
stty crtscts -F /dev/ttyS2
stty crtscts -F /dev/ttyS3

We mentioned that the stty command can be used to display the terminal configuration parameters of a tty device. To display all of the active settings on a tty device, use:

$ stty -a -F /dev/ttyS1

The output of this command, shown in Example 4-5, gives you the status of all flags for that device; a flag shown with a preceding minus, as in –crtscts, means that the flag has been turned off.

Example 4-5. Output of stty -a Command

intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; 
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
        -ixoff -iuclc -ixany -imaxbel
-opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0
         bs0 vt0 ff0
-isig -icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop
         -echoprt echoctl echoke

A description of the most important of these flags is given in Table 4-2. Each of these flags is enabled by supplying it to stty and disabled by supplying it to stty with the – character in front of it. Thus, to disable hardware handshaking on the ttyS0 device, you would use:
$ stty -crtscts -F /dev/ttyS0

Table 4-2. stty Flags Most Relevant to Configuring Serial Devices


Set the line speed to N bits per second.


Enable/Disable hardware handshaking.


Enable/Disable XON/XOFF flow control.


Enable/Disable modem control signals such as DTR/DTS and DCD. This is necessary if you are using a “three wire” serial cable because it does not supply these signals.

cs5 cs6 cs7 cs8

Set number of data bits to 5, 6, 7, or 8, respectively.


Enable odd parity. Disabling this flag enables even parity.


Enable parity checking. When this flag is negated, no parity is used.


Enable use of two stop bits per character. When this flag is negated, one stop bit per character is used.


Enable/Disable echoing of received characters back to sender.

The next example combines some of these flags and sets the ttyS0 device to 19,200 bps, 8 data bits, no parity, and hardware handshaking with echo disabled:
$ stty 19200 cs8 -parenb crtscts -echo -F /dev/ttyS0