In modem terminology, the computers are Data Terminal Equipment or DTE this terminology arose at a time when the device connected to a modem was usually a terminal, and modems are Data C
Trang 1Serial communications
In this chapter:
• Terminology
• Asynchronous and
synchronous
communication
• Ser ial por ts
In this chapter:
• Terminology
• Asynchronous and
synchronous
communication
• Ser ial por ts
UNIX has always had a high level of support for serial lines, but their purpose has changed dramatically In the early 70s, the standard ‘‘terminal’’ was a Teletype KSR35, a 10-character-per-second serial printer with keyboard Early UNIX serial line support was geared towards supporting these devices, either directly connected, or via a modem Even in the early 80s, when 4.2BSD introduced network support, things didn’t change much: the network support used different hardware By this time, the Teletypes had been
replaced with glass ttys, in other words serial terminals with a monitor instead of a
printer The speeds had gone up from the 110 bps of the Teletype to 9600 bps, but the underlying principles hadn’t changed
It wasn’t until the last 10 years that the glass ttys were replaced by display boards directly connected to the system bus, or by other machines connected by Ethernet The role of the serial port has changed completely: nowadays, they’re used mainly for mice and dialup Internet connections
This change in use has invalidated a few basic concepts Only a few years ago, the standard ‘‘high-speed’’ modem was a V.22bis 2400 bps modem, even then too slow for an Internet connection The standard data communication line was 56 kb/s, and it was invariably a leased line As a result, the Internet grew up assuming that connections were leased lines, and therefore permanently connected Even today, the Internet protocols do not deal well with dialup access
On the other hand, UUCP did use dialup access As a result, provisions for dialup access
in UNIX tend to be derived from UUCP This doesn’t make for smooth integration.
Trang 2The Complete FreeBSD 326
In this chapter, we’ll look at the way FreeBSD handles serial communications, at how modems work, and how the two fit together
Terminology
Any serial connection has two ends, which may be computers, terminals, printers or
modems In modem terminology, the computers are Data Terminal Equipment or DTE
(this terminology arose at a time when the device connected to a modem was usually a
terminal), and modems are Data Communication Equipment or DCE You’ll also sometimes hear the name dataset for a modem.
Asynchronous and synchronous commu-nication
There are two different ways to transmit serial data, called synchronous and
asyn-chronous communication They grew up in different worlds:
Asynchronous communication
Asynchronous communication predates computers It was originally developed to run
teletypewriters, electrical typewriters that were run off a serial data stream, the
best-known of which were made by the Teletype corporation These machines were frequently used to provide a remote transcript of what somebody was typing miles away,
so they would typically print one character at a time, stop, and wait for the next In the early days of UNIX, the standard terminal was a Teletype model KSR35, commonly just
called teletype or tty (pronounced ‘‘titty’’).
Here’s a picture of a typical byte encoding:
StartParity
1
D7
1
D6
1
D5
0
D4
1
D3
0
D2
1
D1
1
D0
0
Stop Stop Bit 1 Bit 2
Figure 19-1: Asynchronous byte encoding
This figure shows an encoding for the letterj, in binary01101011 We’ll see a number
of things about it:
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 3• Before the character starts, the line is idle, which shows as a high level: this indicates
to the teletype that the line is still connected
• First comes a start bit In the olden days, this bit started the motor of the teletype
mechanism Now it signals that data is coming
• Next comes a parity bit To detect any transmission errors, this character is encoded with even parity The parity bit is set to1if the character contains an odd number of bits, and to0otherwise, which ensures that the character, including the parity bit, always has an even number of bits If a single bit is corrupted during transmission, the character will arrive with odd parity, and the receiver will detect an error
• Next come the bits of the character, last bit first We represent1with a low lev el and
0with a high level
• Finally come one or two stop bits The stop bits were originally intended to give the
teletype time to stop the motor, but they are now largely superfluous You needed two stop bits for a teletype, but nowadays you should always use one
• This example also shows something else of importance: there are a number of ways
to encode the character How many bits? How many stop bits? Odd parity? Even parity? No parity? Mark parity (always a 1 bit)? Space parity (always a 0 bit)?
How much time from one bit to the next (what bit rate)? They’re all set with the stty program (see man page stty(1)), but if you set them wrongly, you’ll run into trouble.
• The encoding isn’t very efficient For every character you send, you also send a start bit and a stop bit Most communications no longer use the parity bit, but this still means that you have a 25% overhead on communication: for every 8 bits, you send
10, and you could send up to 12, as in this example We’ll see that synchronous communication doesn’t hav e this problem Users of synchronous communication
protocols often refer to asynchronous communication as start-stop communication.
Synchronous communication
By contrast with asynchronous communication, synchronous communication comes from the mainframe world, and it assumes that data does not come one byte at a time Instead,
it transmits data in blocks Each block is preceded by one or two SYN characters that tell
the receiver that data is coming, and that enable it to determine the correct orientation of the bits in the data
All modern modems use synchronous communication on the phone line, because it is more efficient, and it’s the basis of protocols such as SNA and X.25, but you will almost never see any other use of it in UNIX systems
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 4Asynchronous and synchronous communication 328
Serial ports
Nowadays, all PCs come equipped with two serial ports, which are calledCOM1:and
COM2: in the DOS world UNIX names are different, and FreeBSD calls these same devices sio0 and sio1 It’s possible to connect up to four direct serial ports on a standard PC, but due to the design of the board, each one requires a separate IRQ line If you put two serial ports on the same interrupt line, neither of them will work
The first two devices, sio0 and sio1, normally use the default IRQs 4 and 3 By default, however, PC manufacturers put COM3: and COM4: also at IRQs 4 and 3 How can this work? It can’t, if you also have COM1: and COM2: enabled at those IRQs However,
DOS tends to do only one thing at a time, so you can use different ports at different times
on the same IRQ, as long as the interrupts aren’t enabled on more than one of the ports at
a time This restriction is unacceptable for UNIX, so we have to put them somewhere else The only unused interrupt available to 8-bit boards is IRQ 5, originally intended for
a second parallel printer port
There’s a very good chance that IRQ 5 will already be occupied What can you do? If one of the boards has a 16-bit or better interface, you can check if one of the interrupts 10
to 15 is available All EISA and PCI boards fit into this category, and so do ISA boards with two connectors to the motherboard Unfortunately, a lot of ISA serial cards only have an 8-bit interface The only alternative is an intelligent serial board that only occupies a single interrupt In this case, you will probably have to build a custom kernel
See the man page sio(4).
Connecting to the port
Theoretically, a serial line can consist of only three wires: a Receive Data line, often abbreviated to RxD, a Tr ansmit Data line (TxD), and a Signal Ground line (SG) In fact,
it is possible to get a link to work like this, but there are a number of problems:
• How do we know when the other end is able to accept data? It may be busy processing data it has already received
• How do we know when it’s even switched on?
• In the case of a modem, how do we know when it is connected to the modem at the other end?
We solve these questions, and more, by the use of additional lines The most common
standard is RS-232, also known as EIA-232, a standard for DCE to DTE connection In Europe, it is sometimes confused with the CCITT V.24 standard, though V.24 does not in
fact correspond exactly to RS-232 Most external modems display some of these signals
on LED, but modem manufacturers love to create alternative abbreviations for signal names Here are the signals that RS-232 defines, with some of the more common abbreviations that you may see on external modems
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 5Table 19-1: RS-232 signals and modem LEDs
PG 1 Protective ground Used for electrical grounding only
modem
RTS 4 Request to send Indicates that the device has data to
output
CTS 5 Clear to send Indicates that the device can receive input
ON
Data set ready Indicates that the modem is powered on and has passed self-test On some modems, PW indicates that power is on, andMRindicates that it is operative
SG 7 Signal ground Return for the other signals
with another modem
computer is ready to talk to the modem
RI 22 AA Ring indicator Raised by a modem to indicate that an
incoming call is ringing TheAAindicator on a modem will usually flash when the incoming call is ringing
AA ‘‘Auto Answer.’’ Indicates that the modem will answer an
incoming call
HS ‘‘High Speed.’’ Indicates that the modem is running at a
higher speed than its minimum Individual modems interpret this differently, but you can assume that something
is wrong if your modem has this indicator and it’s off during transmission
MNP Indicates that error correction is active
OH ‘‘Off hook.’’ Indicates that the modem has some
connection with the phone line
PW Indicates that modem power is on May or may not imply
DSR
The line DCD tells the DTE that the modem has established a connection We’ll look at
how to use this information on page 335
In addition to these signals, synchronous modems supply clocks on pins 17 and 19 For more details about RS-232, see RS-232 Made easy by Martin Seyer.
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 6Ser ial por ts 330
When can I send data?
There are two ways to determine if the other end is prepared to accept data: hardware
handshaking and software handshaking Both are also referred to as flow control In
each case, the handshaking is symmetrical We’ll look at it from the point of view of the DTE, because this is the more common viewpoint
In hardware handshaking, the DCE raises CTS (Clear to Send) when it’s ready to accept
input The DTE only transmits data when CTS is asserted from the other end You’ll
often see that the DTE asserts RTS (Request to send) when it wants to send data This is
a throwback to the days of half-duplex modems, which could only transmit in one direction at a time: RTS was needed to switch the modem into send mode.
Software handshaking is also called X-on/X-off The DCE sends a character (X-off, which
corresponds to S) when the buffer is full, and another (X-on, corresponding to
Ctrl-Q) when there is space in the buffer again You can also use this method on a terminal to
temporarily stop the display of a lot of data, and then restart it It’s no longer a good choice for modems
For hardware handshake to work, your modem must be configured correctly, and you must have the correct cables If it isn’t, the symptoms will be very slow response when transferring large quantities of data: at a higher level, TCP can recover from these overruns, but it takes at least a second to do so every time We’ll see how to check that your modem has the correct kind of flow control on page 333
Modems
A modem is a device that transfers digital data into a form suitable for transmission over a
transmission line, which is usually a telephone line Telephone lines are limited to a frequency of about 3.6 kHz, and this limited the speed of older modems to about 1200 bits per second Modern modems use many sophisticated techniques to increase the speed way beyond this Current modems transmit at 56 kilobits per second
Let’s consider the modem connection in the reference network on page 294, which is repeated in figure 19-2 As we can see, there are three connections:
• The connection from the routergwto the local modem, connected at 57,600 bits per second
• The connection between the modems, at 56,000 bits per second
• The connection from the ISP’s modem to his router, at 115,200 bits per second You’ll also note another value specified here: the connection between the modems is
2,400 baud Isn’t a baud the same thing as a bit per second? No, not always The term
baud is a representation of the frequency of data on a serial line On the connections
between the systems and the modem, which handle raw digital data, it corresponds to the bit rate On the modem line, it doesn’t Here, it indicates that 2,400 units of data are sent per second
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 7Unfortunately, many people use the term baud where bit rate should be used This didn’t
make any difference in the old days with simple modems where the bit rate and baud rate were the same, but nowadays it’s confusing
•
•
gw
Router
•
• modem
•
• modem
•
•
ISP router
Local modem connection, 57,600 bps
PPP link, 56,000 bps, 2400 baud
ISP modem connection, 115,200 bps
DTE
DCE
DCE
DTE
Figure 19-2: Network modem connection
Modem speeds
Tw o factors determine the data transmission speed of a modem: the protocol and the use
of data compression Table 19-2 on page 332 gives an overview of modem protocols and
their speeds
Currently, the most popular modem protocol is V.90 V.90 pushes modem technology to the limit, and it only works when the other end of the link is a digital (ISDN) connection You can’t get a 56 kb/s connection with any kind of analogue modem at the other end
As a result, they’re really only suitable for connection to a large ISP site In addition, the actual speed of the connection depends greatly on the telephone line quality, so the difference between a V.90 and a V.34bis modem may not be as much as it appears
Data compression
In addition, you usually have a choice of data compression: V.42bis or MNP-5 The
choice depends on what the modem at the other end of the line does You can set most modems to negotiate either protocol These protocols include related error correction
standards, called V.42 or MNP2-4 respectively If you believe the sales claims, these
compression protocols will give you up to 100% increase in data throughput Consider this the upper limit; a lot of data is binary, and when ftp’ing a typical gzipped tar archive, you will probably get almost no speed improvement
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 8Modems 332 Data compression has one negative side: it increases the data rate, but it also increases
latency, the time it takes for data to get from the local DTE to the remote DTE The data
doesn’t take as long on the line, but it spends more time in the modems being compressed and uncompressed If you’re running a protocol like PPP that supplies optional compression in the software, you may find it advantageous to turn off compression
We’ll look at that again in Chapter 20, Configuring PPP.
Table 19-2: Modem protocols and speeds
Protocol Speed (bps)
Bell 203 300
V.21 300
Bell 212 1200
V.22 1200
V.22bis 2400
V.32 9600
V.32bis 14400
V.34 28800
V.34bis 33600
V.90 56000
The link speed
The standard PC serial hardware can run at speeds that are a fraction of 115,200 bps (in other words, 115200 divided by a small integer) This gives the following combinations:
Table 19-3: Serial line speeds
Divisor Speed (bps)
1 115200
2 57600
3 38400
4 28800
5 23040
6 19200
You’ll notice that it can’t run at 33600 or 56000 bps Also, looking at the example above, you’ll note that all three links run at different speeds How can that work? Only a few
years ago, it wouldn’t, but modern modems can buffer data For example, the ISP can
send data to the modem far faster than the modem can send it to the other modem It stores the data in internal memory until it can be transmitted This can also happen at the other end If you misconfigure your line so that the local link runs at 9600 bps, things will still work, but of course the total speed is the speed of the slowest link, in this case
9600 bps
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 9This flexibility brings a problem with it: the modem can’t know in advance how fast the connection to the computer is It needs a way to find out The modem solves the question of local line speed by a trick: all commands start withAT or at(you’re not allowed to mix cases, likeaTorAt) It can recognize these characters even if they arrive
at the wrong speed, and thus it can establish the speed of the connection
Dialing out
Nowadays, all modems are capable of dialing That wasn’t always the case, and in some
old documentation you may find references to an Auto-Call Unit or ACU, which is simply
the dialler part of a modem connected via a separate port Typically, one ACU could serve multiple modems
Nearly every modern modem uses a command set designed by Hayes Corporation, which
is thus called the Hayes Command Set We’ll look at it in the following section It is also sometimes called the AT command set, because nearly all the commands start with the
sequence AT The CCITT also created an autodial recommendation, V.25, which was
occasionally implemented, but now appears to be dead
Modem commands
Modern modems store their state in a number of registers, called S registers The register
use varies somewhat from manufacturer to manufacturer, but most modems have a number in common They each store a one-byte value, ranging between 0 and 255 Here’s a list of the more important ones for a Rockwell V.34 chip set The name of the chip set is not the same as the name of the modem You’ll note that one of the commands enables you to find out the chip set version, as we’ll see in the example that follows
Table 19-4: Selected S registers
Register number Purpose
S0 Number of rings until auto-answer 0 disables auto-answer Set to 0
for no automatic answer, or 1 for auto-answer
S2 The escape character, which lets you return from online mode to
command mode Normally, this character is a + To return to command mode, wait a second after any previous input, enter+++, and wait a second, after which the modem should reply withOK
S6 The time, in seconds, to wait before blind dialing If you have set your
modem to not wait for a dial tone (maybe because it doesn’t understand the dial tone), it will wait this long and then try to dial anyway
modems.mm,v v4.10 (2003/04/02 03:11:02)
Trang 10Modem commands 334
Register number Purpose
S7 The number of seconds to wait after dialing before DCD must be
asserted (before a connection is established) If this is set too short, you will not be able to establish a connection If it’s too long, you will waste time when there is no answer or the line is busy
S11 The duration of DTMF (dialing) tones If these are set incorrectly, the
telephone exchange may not understand the number you dial
TheATcommand set tells the modem to do something specific Here are some of the more important ones
Table 19-5: Selected AT commands
Command Meaning
A/ Redial the last number
ATA Answer an incoming call manually This is an alternative to auto-answer
by settingS0
ATDnumber Dial number This command has a large number of options, but if your
modem is set up correctly, you probably won’t need any of them
ATEnumber Enable command echo if number is 1, disable it if number is 0 The
setting of this parameter can be important for some chat scripts, which may not respond correctly otherwise
ATH0 Disconnect the line
ATInumber Display modem identification The values of number vary from one
modem to the next See the examples below
ATLnumber Set the speaker volume number ranges from 0 to 3 0 means ‘‘speaker
off,’’ 3 is the loudest
ATMnumber Determine when the speaker is on 0 means ‘‘always off,’’ 1 means
‘‘speaker on until connect,’’ 2 means ‘‘speaker always on,’’ and 3 means
‘‘speaker off during dialing and receiving.’’
ATO0 Go back online from command mode You don’t need this command
when dialing: the modem automatically goes online when the connection
is established
ATP Select pulse dial If your exchange doesn’t understand DTMF (tone)
dialing, you should set this mode Never use it if your exchange
understands DTMF: pulse dialing (also known as steam dialing) is much
slower
ATQnumber If number is 0, suppress result codes (likeOKafter every command) If
number is 1, enable them This value can be of importance for chat
scripts
modems.mm,v v4.10 (2003/04/02 03:11:02)