old-www/HOWTO/Text-Terminal-HOWTO-23.html

366 lines
20 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.21">
<TITLE> Text-Terminal-HOWTO: Appendix C: Serial Communications on EIA-232 (RS-232)</TITLE>
<LINK HREF="Text-Terminal-HOWTO-24.html" REL=next>
<LINK HREF="Text-Terminal-HOWTO-22.html" REL=previous>
<LINK HREF="Text-Terminal-HOWTO.html#toc23" REL=contents>
</HEAD>
<BODY>
<A HREF="Text-Terminal-HOWTO-24.html">Next</A>
<A HREF="Text-Terminal-HOWTO-22.html">Previous</A>
<A HREF="Text-Terminal-HOWTO.html#toc23">Contents</A>
<HR>
<H2><A NAME="s23">23.</A> <A HREF="Text-Terminal-HOWTO.html#toc23">Appendix C: Serial Communications on EIA-232 (RS-232)</A> </H2>
<H2><A NAME="ss23.1">23.1</A> <A HREF="Text-Terminal-HOWTO.html#toc23.1">Intro to Serial Communication</A>
</H2>
<P> (Much of this section is now found in Serial-HOWTO.) Text
terminals on Unix-like systems (and on PC's) are usually connected to
an asynchronous 232 serial port of a computer. It's usually a
RS-232-C, EIA-232-D, or EIA-232-E. These three are almost the same
thing. The original RS prefix became EIA (Electronics Industries
Association) and later EIA/TIA after EIA merged with TIA
(Telecommunications Industries Association). The EIA-232 spec
provides also for synchronous (sync) communication but the hardware to
support sync is almost always missing on PC's. The RS designation is
obsolete but is still in use. EIA will be used in this article.</P>
<P>The serial port is more than just a physical connector on the back of
a computer or terminal. It includes the associated electronics which
must produce signals conforming to the EIA-232 specification. The
standard connector has 25 pins, most of which are unused. An
alternative connector has only 9 pins. One pin is used to send out
data bytes and another to receive data bytes. Another pin is a common
signal ground. The other "useful" pins are used mainly for signalling
purposes with a steady negative voltage meaning "off" and a steady
positive voltage meaning "on".</P>
<P>The UART (Universal Asynchronous Receiver-Transmitter) chip does most
of the work. Today, the functionality of this chip is usually built
into another chip.</P>
<H2><A NAME="ss23.2">23.2</A> <A HREF="Text-Terminal-HOWTO.html#toc23.2">Voltages</A>
</H2>
<H3>Voltage for a bit</H3>
<P> At the EIA-232 serial port, voltages are bipolar (positive or
negative with respect to ground) and should be about 12 volts in
magnitude (newer ones are 5 volts but the 12 volt example is given
here). For the transmit and receive pins +12 volts is a 0-bit
(sometimes called "space") and -12 volts is a 1-bit (sometimes called
"mark"). This is known as inverted logic since normally a 0-bit is
both false and negative while a one is normally both true and
positive. Although the receive and transmit pins are inverted logic,
other pins (modem control lines) are normal logic with a positive
voltage being true (or "on" or "asserted") and a negative voltage
being false (or "off" or "negated"). Zero voltage has no meaning
(except it usually means that the PC is powered off).</P>
<P>A range of voltages is allowed. The specs say the magnitude of a
transmitted signal should be between 5 and 15 volts but must never
exceed 25 V. Any voltage received under 3 V is undefined (but some
terminals will accept a lower voltage as valid). One sometimes saw
erroneous claims that the voltage is commonly 5 volts (or even 3
volts) but it's usually 11-12 volts on older PCs. If you are using a
EIA-422 port on a Mac computer as an EIA-232 (requires a special
cable) or EIA-423 then the voltage will actually be only 5 V. The
discussion here assumes 12 V. There was much confusion about voltages
on the Internet.</P>
<P>Note that normal computer logic normally is just a few volts (5 volts
was once the standard) so that if you try to use test equipment
designed for testing 3-5 volt computer logic (TTL) on a 12 volt serial
port, it may damage the test equipment.</P>
<H3><A NAME="byte_seq"></A> Voltage sequence for a byte </H3>
<P> The transmit pin (TxD) is held at -12 V (mark) at idle when nothing
is being sent. To start a byte it jumps to +12 V (space) for the
start bit and remains at +12 V for the duration (period) of the start
bit. Next comes the low-order bit of the data byte. If it's a 0-bit
nothing changes and the line remains at +12 V for another bit-period.
Then comes the next bit, etc. Finally, a parity bit may be sent and
then a -12 V (mark) stop bit. The line remains at -12 V (idle) until
the next start bit. Note that there is no return to 0 volts and thus
there is no simple way (except by a synchronizing signal) to tell
where one bit ends and the next one begins for the case where 2
consecutive bits are the same polarity (both zero or both one).</P>
<P>A 2nd stop bit would also be -12 V, just the same as the first stop
bit. Since there is no signal to mark the boundaries between these
bits, the only effect of the 2nd stop bit is that the line must remain
at -12 V idle twice as long. The receiver has no way of detecting the
difference between a 2nd stop bit and a longer idle time between
bytes. Thus communications works OK if one end uses one stop bit and
the other end uses 2 stop bits, but using only one stop bit is
obviously faster. In rare cases 1 1/2 stop bits are used. This means
that the line is kept at -12 V for 1 1/2 time periods (like a stop bit
50% wider than normal).</P>
<H2><A NAME="parity_def"></A> <A NAME="ss23.3">23.3</A> <A HREF="Text-Terminal-HOWTO.html#toc23.3">Parity Explained </A>
</H2>
<P> Characters are normally transmitted with either 7 or 8 bits (of
data). An additional parity bit may (or may not) be appended to this
resulting in a byte length of 7, 8 or 9 bits. Some terminal emulators
and older terminals do not allow 9 bits. Some prohibit 9 bits if 2
stop bits are used (since this would make the total number of bits too
large: 12 bits total).</P>
<P>The parity may be set to odd, even or none (mark and space parity may
be options on some terminals). With odd parity, the parity bit is
selected so that the number of 1-bits in a byte, including the parity
bit, is odd. If a such a byte gets corrupted by a bit being flipped,
the result is an illegal byte of even parity. This error will be
detected and if it's an incoming byte to the terminal an
error-character symbol will appear on the screen. Even parity works
in a similar manner with all legal bytes (including the parity bit)
having an even number of 1-bits. During set-up, the number of bits
per character usually means only the number of data bits per byte (7
for true ASCII and 8 for various ISO character sets).</P>
<P>A "mark" is a 1-bit (or logic 1) and a "space" is a 0-bit (or logic
0). For mark parity, the parity bit is always a one-bit. For space
parity it's always a zero-bit. Mark or space parity only wastes
bandwidth and should be avoided when feasible. "No parity" means that no
parity bit is added. For terminals that don't permit 9 bit bytes,
"no parity" must be selected when using 8 bit character sets since
there is no room for a parity bit.</P>
<H2><A NAME="ss23.4">23.4</A> <A HREF="Text-Terminal-HOWTO.html#toc23.4">Forming a Byte (Framing)</A>
</H2>
<P> In serial transmission of bytes via EIA-232 ports, the low-order
bit is always sent first. Serial ports on PC's use asynchronous
communication where there is a start bit and a stop bit to mark the
beginning and end of a byte. This is called framing and the framed
byte is sometimes called a frame. As a result a total of 9, 10, or 11
bits are sent per byte with 10 being the most common. 8-N-1 means 8
data bits, No parity, 1 stop bit. This adds up to 10 bits total when
one counts the start bit. One stop bit is almost universally used.
At 110 bits/sec (and sometimes at 300 bits/sec) 2 stop bits were once
used but today the 2nd stop bit is used only in very unusual
situations (or by mistake since it seemingly still works OK that way).</P>
<H2><A NAME="ss23.5">23.5</A> <A HREF="Text-Terminal-HOWTO.html#toc23.5">Limitations of EIA-232</A>
</H2>
<H3>Low Speed &amp; Short Distance</H3>
<P> The conventional EIA-232 serial port is inherently low speed and
is severely limited in distance. Ads often read "high speed" but it
can only work at high speed over very short distances such as to a
modem located right next to the computer. All of the wires use a
common ground return so that twisted-pair technology (needed for high
speeds) can't be used without additional hardware. However some
computers have more modern interfaces. See
<A HREF="#non_232">Successors to EIA-232</A>.</P>
<P>It is somewhat tragic that the RS-232 standard from 1969 did not use
twisted pair technology which could operate about a hundred times
faster. Twisted pairs have been used in telephone cables since the
late 1800's. In 1888 (over 110 years ago) the "Cable Conference"
reported its support of twisted-pair (for telephone systems) and
pointed out its advantages. But over 80 years after this approval by
the "Cable Conference", RS-232 failed to utilize it. Since RS-232
was originally designed for connecting a terminal to a low speed modem
located nearby, the need for high speed and longer distance
transmission was apparently not recognized.</P>
<H3><A NAME="non_232"></A> Successors to EIA-232 </H3>
<P> See the Serial-HOWTO section "Other Serial Devices" for a longer
discussion about non-EIA-232 ports. A number of EIA standards have
been established for higher speeds and longer distances using
twisted-pair (balanced) technology. Balanced transmission can
sometimes be a hundred times faster than unbalanced EIA-232. For a
given speed, the distance (maximum cable length) may be many times
longer with twisted pair. Few terminals seem to support them. While
many terminals also support EIA-423 is is almost like EIA-232 but is
only 5 volts and somewhat higher speeds (without using twisted pair).
Twisted pair includes EIA-422, EIA-530-A, HSSI (High Speed Serial
Interface), USB (Universal Serial Bus), and of course ethernet.</P>
<H3>Line Drivers</H3>
<P> For a text terminal, the EIA-232 speeds are fast enough but the
usable cable length is often too short. Balanced technology could
fix this. The common method of obtaining balanced communication with
a text terminal is to install 2@ line drivers in the serial line to
convert unbalanced to balanced (and conversely). They are a
specialty item and are expensive if purchased new.</P>
<H2><A NAME="sync"></A> <A NAME="ss23.6">23.6</A> <A HREF="Text-Terminal-HOWTO.html#toc23.6">Synchronization &amp; Synchronous </A>
</H2>
<H3>How "Asynchronous" is Synchronized</H3>
<P> Per EIA-232 there are only two states of the transmit (or receive)
wire: mark (-12 V) or space (+12 V). There is no state of 0 V. Thus
a sequence of 1-bits is transmitted by just a steady -12 V with no
markers of any kind between bits. For the receiver to detect
individual bits it must always have a clock signal which is in
synchronization with the transmitter clock. Such clocks generate a
"tick" in synchronization with each transmitted (or received) bit.</P>
<P>For asynchronous transmission, synchronization is achieved by framing
each byte with a start bit and a stop bit (done by hardware). The
receiver listens on the line for a start bit and when it detects one
it starts its clock ticking. It uses this clock tick to time the
reading of the next 7, 8 or 9 bits. (It actually is a little more
complex than this since several samples of a bit are often taken and
this requires additional timing ticks.) Then the stop bit is read, the
clock stops and the receiver waits for the next start bit. Thus async
is actually synchronized during the reception of a single byte but
there is no synchronization between one byte and the next byte.</P>
<H3>Defining Asynchronous vs Synchronous</H3>
<P> Asynchronous (async) means "not synchronous". In practice, an
async signal is what the async serial port sends and receives which is
a stream of bytes each delimited by a start and stop bit. Synchronous
(sync) is most everything else. But this doesn't explain the basic
concepts.</P>
<P>In theory, synchronous means that bytes are sent out at a constant
rate one after another in step with a clock signal tick. There is
often a separate wire or channel for sending the clock signal.
Asynchronous bytes may be sent out erratically with various time
intervals between bytes (like someone typing characters at a
keyboard).</P>
<P>There are borderline situations that need to be classified as either
sync or async. The async serial port often sends out bytes in a
steady stream which would make this a synchronous case but since they
still have the start/stop bits (which makes it possible to send them
out erratically) its called async. Another case is where data
bytes (without any start-stop bits) are put into packets with possible
erratic spacing between one packet and the next. This is called sync
since the bytes within each packet must be transmitted synchronously.</P>
<H3>Synchronous Communication</H3>
<P> Did you ever wonder what all the unused pins are for on a 25-pin
connector for the serial port? Most of them are for use in
synchronous communication which is seldom implemented on PC's. There
are pins for sync timing signals as well as for a sync reverse
channel. The EIA-232 spec provides for both sync and async but PC's
use a UART (Universal Asynchronous Receiver/Transmitter) chip such as
a 16450, 16550A, or 16650 and can't deal with sync. For sync one
needs a USART chip or the equivalent where the "S" stands for
Synchronous. Since sync is a niche market, a sync serial port is
likely to be quite expensive.</P>
<P>Besides the sync part of the EIA-232, there are various other EIA
synchronous standards. For EIA-232, 3 pins of the connector are
reserved for clock (or timing) signals. Sometimes it's a modem's task
to generate some timing signals making it impossible to use
synchronous communications without a synchronous modem (or without a
device called a "synchronous modem eliminator" which provides the
timing signals).</P>
<P>Although few serial ports are sync, synchronous communication
does often take place over telephone lines using modems which use
V.42 error correction. This strips off the start/stop bits and puts
the date bytes in packets resulting in synchronous operation over the
phone line.</P>
<H2><A NAME="block"></A> <A NAME="ss23.7">23.7</A> <A HREF="Text-Terminal-HOWTO.html#toc23.7">Block Mode </A>
</H2>
<H3>Introduction to Block Mode</H3>
<P> Block mode is seldom used with Linux and is mainly of historical
interest. In block mode, when one types at a terminal the results are
saved in the terminal memory and are not sent just yet to the host
computer. Such terminals often have built-in editing capabilities.
When the user presses certain keys (such as the send key) what has
been saved in the terminal memory is sent to the host computer. Now
the Linux editors vi and emacs, must react instantly to typing certain
keys so block mode isn't feasible. Such editors and other interactive
programs can't permit the long delay in sending a keystroke to the
computer which is inherent in block mode. So they can't use block
mode.</P>
<P>The old IBM mainframe interface uses block mode (see
<A HREF="Text-Terminal-HOWTO-24.html#ibm_">IBM Terminals </A> so many IBM terminals are block-mode only and
also synchronous (see Section
<A HREF="#sync">Synchronization &amp; Synchronous</A>).</P>
<H3>Types of Block Modes, Forms</H3>
<P> Block mode may itself have various sub-modes such as "page" (a
page at a time) and "line" (a line at a time). Some terminals have
both block transmission modes and conventional character modes and may
be switched from one mode to another. Async terminals which have
block modes include HP2622A, Wyse60, VT130, VT131, VT330, VT340, and
Visual500. Many later model terminals can emulate block mode. But
the Linux console can't. Block modes may include a forms capability
where the host computer sends a form to the terminal. Then the user
fills it out and hits the send key which sends only the data in the
form back to the host computer. The form itself (not the data) is
displayed on the screen in protected fields which don't get
transmitted to the host.</P>
<H3>Efficiency</H3>
<P> Block mode takes load off the host computer, especially if the
host computer's hardware is designed for block modes (as IBM
mainframes were). In character mode every character typed is sent
immediately to the serial port and usually causes an interrupt at the
host computer. The host that receives the byte must stop whatever it
is doing and fetch that character from the port hardware. Even with
UART's that have FIFO hardware buffers, the hardware timeout is
normally only the transmission time of 3 bytes so that an interrupt is
usually issued for every character typed.</P>
<P>In true block mode a long block of characters is received using only
one interrupt. If block mode is used with conventional async FIFO
serial ports, an interrupt is needed only every 14 bytes since they
have 16-byte hardware buffers. Thus much of the load and overhead of
interrupt handling is eliminated and the computer has more time to do
other tasks when block mode is used.</P>
<P>A significant savings for block mode occurs if the terminal is
connected to its host via a network. Without block mode, every
character (byte) typed is sent in its own packet including all the
overhead bytes (40 in a TCP/IP packet as used on the Internet). With
block mode, a large number of characters are sent in a single packet.</P>
<H3>Problems with block mode</H3>
<P>While block mode is more efficient, it is nearly extinct, and for
good reason. Faster and cheaper computers made the higher efficiency
less important. For example, a 56k modem results in hundreds of
interrupts per second (every 14 characters) while typing at a terminal
only causes a few interrupts per second (one for each character
typed). So the number of interrupts caused by typing at a terminal
is not very significant (unless you have hundred of terminals
connected to the same computer).</P>
<P>Another point is that the efficiency is not of much significance where
the user doesn't type in very much. Editors are a primary example of
where the user types in a lot. But if you use block mode for
editing, you must then use the crude editor built into terminal. Modern
editors like vim and emacs are much better but can't use block mode.
Even in the days of mainframes with terminals, block mode wasn't used
much except by IBM. A major reason was that software to utilize it
was not widely available (except for IBM). The terminfo data base
doesn't seem to include it and this would complicate writing software
for it.</P>
<H2><A NAME="RS232_books"></A> <A NAME="ss23.8">23.8</A> <A HREF="Text-Terminal-HOWTO.html#toc23.8">EIA-232 (RS-232) Books </A>
</H2>
<P> (Note: The first book covers much more than just EIA-232.)
<UL>
<LI> Black, Uyless D.: Physical Layer Interfaces &amp; Protocols, IEEE
Computer Society Press, Los Alamitos, CA, 1996.</LI>
<LI> Campbell, Joe: The RS-232 Solution, 2nd ed., Sybex, 1982.</LI>
<LI> Putnam, Byron W.: RS-232 Simplified, Prentice Hall, 1987.</LI>
<LI> Seyer, Martin D.: RS-232 Made Easy, 2nd ed., Prentice Hall,
1991.</LI>
</UL>
</P>
<H2><A NAME="ss23.9">23.9</A> <A HREF="Text-Terminal-HOWTO.html#toc23.9">Serial Software</A>
</H2>
<P> See
<A HREF="ftp://ibiblio.org/pub/Linux/system/serial/">Serial Software</A> for Linux software for the serial ports
including getty and port monitors.</P>
<HR>
<A HREF="Text-Terminal-HOWTO-24.html">Next</A>
<A HREF="Text-Terminal-HOWTO-22.html">Previous</A>
<A HREF="Text-Terminal-HOWTO.html#toc23">Contents</A>
</BODY>
</HTML>