Книга: Linux Network Administrator Guide, Second Edition

The stty Command

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

speed 19200 baud; rows 0; columns 0; line = 0;
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

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

Flags Description
N Set the line speed to N bits per second.
crtsdts Enable/Disable hardware handshaking.
ixon Enable/Disable XON/XOFF flow control.
clocal 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.
parodd Enable odd parity. Disabling this flag enables even parity.
parenb Enable parity checking. When this flag is negated, no parity is used.
cstopb Enable use of two stop bits per character. When this flag is negated, one stop bit per character is used.
echo 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

Оглавление книги

Оглавление статьи/книги

Генерация: 1.155. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз