Using Serial Device Files

Traditional RS-232 devices use device filenames of the form /dev/ttySn, where n is a number from 0 up. To communicate with a serial device, you (or a program you use) send characters to this device file. For instance, the following command sends the string ATDT555-4321 to a modem attached to /dev/ttyS0, causing the modem to dial 555-4321.

$ echo "ATDT555-4321" > /dev/ttyS0

Note In the past, the /dev/cuan devices were used as a variant method of accessing serial ports. Use of /dev/cuan is discouraged today, however, and support for these devices may disappear in the future, although it's still present up to at least the 2.4.18 kernel.

Because the serial device interprets the characters, there is no need for cross-device standardization of commands sent over the RS-232 serial port; modems accept different commands than do printers, which use different commands than mice, and so on. There are, however, some Linux commands that can affect how the serial interface works. Of particular importance is the setserial command, which sets the RS-232 port's parameters, such as its speed. A simplified syntax for this command is as follows:

The -a and -b options cause the program to display information on the hardware's settings, such as the interrupt request (IRQ) used by the port. -a results in a multiline display, and -b produces a less complete single-line output. You can pass a wide assortment of parameters to the program in order to change how it configures the port. These are the most important parameters:

port portnum This option sets the I/O port number to portnum. The I/O port is a hexadecimal number, such as 0x03f8, which indicates where in memory the input to and output from the serial port occur. Unless you're using very unusual hardware, you shouldn't need to adjust this setting.

irq irqnum You can tell Linux to use an unusual IRQ number with this parameter. As with the port parameter, chances are you won't need to use this option.

uart uarttype RS-232 serial ports are controlled by universal asynchronous receiver/transmitters (UARTs), which come in several varieties. Linux usually detects the UART type correctly at bootup, but if it gets this detail wrong, you can correct the matter by using the uart parameter. (An incorrect port type is likely to produce unreliable serial port operation.) Most modern motherboards use 16550A UARTs, so uarttype would be 16550A for these boards. Very old hardware may use earlier UARTs, such as the 16450; and some devices use more recent ones, like the 16650.

baud_base speed This parameter sets the speed of the serial port. The usual value for speed is 115200, which is the fastest speed in bps that most serial ports support.

These options and parameters just scratch the surface of setserial's capabilities. Read its man page for more information on esoteric features you can control with the program. This utility is most useful when you use simple programs and daemons with the serial port. More sophisticated programs and daemons can set the serial port's speed and other features themselves.

Unlike the RS-232 serial port, there isn't a single device file for each USB port on your computer. Instead, Linux maps each device to its own device file. The reason for this arrangement is simple: Using a hub, it's possible to connect more than one USB device to each USB port. Suppose a USB port hosts a printer, a mouse, and a modem. Each of those devices acquires its own entry in /dev or /dev/usb, so you can address each device appropriately. Precisely how you handle these devices varies from one device and program to another. For instance, you would normally reference a USB mouse in your XFree86 configuration file, as described in Chapter 13, whereas you'd reference a printer's file in/etc/printcap, as described in Chapter 10. Table 11.1 summarizes some of the more common USB device files, including their major and minor numbers, which define the interface between the device file and the kernel's drivers. It's important to realize that the device filenames are largely arbitrary; you can access a device under any filename you like, so long as that filename identifies a device file created via mknod with the appropriate type, major number, and minor number, as described shortly. Some USB devices, such as disk devices and speakers, are treated as if they were non-USB devices, such as SCSI disks and ordinary sound cards, respectively. Others use new major and minor numbers, and usually new device filenames.

Table 11.1: Methods of Accessing USB Devices in the 2.4.x Kernel

Device

Device Filename

Type

Major Number

Minor Number

Disk devices

/dev/sdn

Block

8

0-240

Modem

/dev/usb/acm/n

Character

166

0 up

Mouse

/dev/usb/mousen or /dev/input/mousen

Character

180 or 13, respectively

16 and up or 32 and up, respectively

Printer

/dev/usb/lpn

Character

180

0 and up

Scanner

/dev/usb/scannern

Character

180

48-63

Speakers

Normal audio devices, such as/dev/audio and /dev/mixer

Character

Varies

Varies

USB-to-parallel adapter

/dev/usb/lpn

Character

180

0 and up

USB-to-RS-232 adapter

/dev/usb/ttyUSBn

Character

188

0 and up

The /dev/usb directory is often implemented using a special USB device filesystem, which attempts to create device files dynamically for supported devices. If your computer doesn't use this filesystem, you may need to create a device file to access a USB device. You may do so with the mknod command, which uses the following syntax:

mknod filename type major minor

In this command, filename is the device filename, type is either b for block devices or c for character devices, and major and minor are the major and minor numbers listed in Table 11.1. These are sometimes documented in files in the /usr/src/linux/Documentation/usb directory, but documentation on this point is still spotty in 2002.

Was this article helpful?

0 0
Computer Hard Drive Data Recovery

Computer Hard Drive Data Recovery

Learn How To Recover Your Hard Drive Data After A Computer Failure.

Get My Free Ebook


Post a comment