3.3. Using the Configuration Utilities
Now let's spend some
time looking at the two most useful serial device configuration
utilities: setserial and
stty.
3.3.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
rc.serial on some distributions, though yours
may very. 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 3-1. For information on the remainder of the
parameters, you should refer to the setserial
manpage.
your serial ports at boot time might look something like that shown
in Example 3-1. Most Linux distributions will
include something slightly more sophisticated than this one.
Example 3-1. rc.serial setserial commands
# /etc/rc.serial - serial line configuration script.The -bg /dev/ttyS* argument in
#
# 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 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 3-2.
Example 3-2. Output of setserial -bg /dev/ttyS command
/dev/ttyS0 at 0x03f8 (irq = 4) is a 16550A
/dev/ttyS1 at 0x02f8 (irq = 3) is a 16550A
3.3.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 that you can
configure. We'll cover the most important of these
in a moment. You can find the rest described in the
stty manpage.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 in this chapter. 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 such as those shown in Example 3-3.
Example 3-3. rc.serial stty commands
#The stty
stty crtscts < /dev/ttyS0
stty crtscts < /dev/ttyS1
stty crtscts < /dev/ttyS2
stty crtscts < /dev/ttyS3
#
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 3-4.
Example 3-4. rc.serial stty commands using modern syntax
#We mentioned that the
stty crtscts -F /dev/ttyS0
stty crtscts -F /dev/ttyS1
stty crtscts -F /dev/ttyS2
stty crtscts -F /dev/ttyS3
#
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 3-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 3-5. Output of stty -a command
speed 19200 baud; rows 0; columns 0; line = 0;A description of the most important of these flags is given in Table 3-2. Each of these flags is enabled by supplying
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon
-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
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
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