Serial Port

From OSDev Wiki
Jump to: navigation, search

Contents

Introduction

A Serial Port is the generic term for a port on a computer that can be connected to other devices. This may be a proprietary connector with a few wires in it; an AppleBus/AppleTalk connector to connect a printer, modem or another computer; or even an Ethernet port. But by far the most common usage is for an RS-232 port with a 9-pin D-connector (DE-9) for asynchronous communications (see Communications). All these ports have one thing in common: the data is sent over a small number of wires (usually one in each direction) in serial fashion, rather than all at once in parallel.

But since "Serial Port" is usually used to refer to the COM Port that is found on the PC (to distinguish it from the rarely-seen-nowadays "Parallel Port"), this page will concentrate on that. Refer to the specific pages for other port types. This page will cover the physical port itself, its signals and how they're commonly used, and the most common protocol used on this port.

Physical Port

The original IBM PC computer came with two communication ports: a parallel port for connection to a printer, and a serial port to connect to a modem for external communications. Of course, over time those ports were used for other purposes, but that was their original intended usage.

Confusingly, both ports used the then already established standard connector called the DB-25.

  • The D-series range has (usually) two columns of pins offset from each other to provide both ease of construction, and a definite polarity to the plug to prevent it from being plugged in upside down. Anecdotally, it was so-named because the metal shielding shroud protecting the pins looked like the letter 'D'.
  • The letter after the 'D' indicates the layout of the pins; and often the number of them, making the suffix number redundant. For example, the first in the series is the DA-15, followed by the DB-25 and DC-37. The later DE-9 was adopted for COM ports on the IBM PC-AT, since only 9 of the 25 defined serial port pins were used.

To prevent plugging a device into the wrong port, the DB-25s used were different genders: the parallel port was female, needing a male printer cable, while the serial port was male, needing a female modem cable.

DB-25

IBM used the DB-25 connector to match other computing products that it was selling. The existing layout allowed for multiple serial ports within the one connector - the so-called Primary and Secondary ports. It also had clock pins for high-speed Synchronous ports as well. But for the IBM PC, only the Asynchronous Primary port pins were used, which meant only 9 of the 25 pins were needed.

DE-9

In the later IBM PC "Advanced Technology" (AT), they used the smaller DE-9 connector for more flexible port layout on the rear of the PC.

Signals

The standard signals on the Serial Port were designed to talk to an external serial communications device called a MoDem (Modulator/Demodulator), to allow the computer to be connected over long-distance wires - telephone, ISDN or other long haul methods. The signalling conventions for modems had been well established, and required multiple control signals as well as the simple data in and data out wires.

The naming conventions identified the two endpoints of the cable as Data Terminal Equipment (DTE) and Data Communications Equipment (DCE) for computer and modem respectively. The signal names don't usually refer to these, but their definitions do: for example, Transmit Data (TX or TD) is defined to be "Data sent from the DTE to the DCE" - which means the TX pin on the modem is an input pin!

Electrical

The signals used for the normal IBM PC COM port are defined by the RS-232 standard - but there are other standards such as RS-422 and RS-485. These are better for longer cables, or in (electrically) noisier environments; places that electrical interference can change the signal being communicated.

Each standard defines the voltage levels required to receive a HIGH ('1') or LOW ('0') signal. Obviously, the transmitter needs to produce at least this much - but voltage drop over the cable means that it should probably produce more. So the standards also define a maximum voltage too.

TTL

This term is often used to indicate the signal levels coming straight out of the chip, whether that's the PC's 8250 or 16450 UART, or a microcontroller. It stands for Transistor/Transistor Logic, and historically means 0V and 5V for LOW and HIGH respectively. These days, the chips usually have different Logic standards with different voltage levels, so "TTL" is a misnomer. But the concept is the same: Minimum for LOW, Maximum for HIGH. Only, these signals aren't "driven" very hard, so can't be transmitted over much distance - well under a metre. But that's fine for two chips or boards that are next to each other.

RS-232

RS-232 defines all of the signals used for serial communications, as well as specifying the voltages that the receiver needs to detect to interpret the values of HIGH and LOW. It also demands that the signals be "driven" hard enough to cover tens of metres, which means that both sides can be at opposite ends of a room.

Each of the signals use the same scheme: a LOW is signalled with a voltage between +3V and +25V, while a HIGH is between -3V and -25V, relative to the Signal Ground (GND). Note that a disconnected cable would produce 0V, which is neither HIGH nor LOW. This "dead band" is deliberate, to reduce the effect of electrical noise.

RS-422

RS-422 defines the same set of signals as RS-232, but defines that each signal be sent over a pair of wires, known as a "balanced" circuit. Rather than each signal being referenced to a common ground, each signal is referenced between its two wires. The signal names are modified to suffix 'A' or 'B", or occasionally '+' and '-', to each of the signals' wires. LOW is indicated by 'B' being at least 0.2V lower than 'A', while HIGH is indicated by 'B' being at least 0.2V higher than 'A'. The maximum difference between the two must be no more than 12V.

This "common mode rejection" system takes advantage of the phenomenon that external electrical interference on the cable raises or lowers the voltage on both wires by similar amounts, which doesn't alter their relative voltage difference. That means that cables can be hundreds of metres long for even high data rates, without the signal deteriorating.

RS-485

RS-485 defines the same set of signals as RS-422 and RS-232, and also defines the same voltages and balanced wiring as RS-422. But it also defines that the transmitter must be able to be turned off. This means that multiple transmitters, and all the receivers, can be connected to the same cable, and only the one currently transmitting is actually driving the signal.

This has the benefits of the cable length of RS-422, but allows a single cable run rather than individual runs for each pair of equipment. The drawback is that only one transmitter can be using the cable at any one time.

Pinout

These pinouts are identical for both the computer (DTE) and modem (DCE), thus only a straight-through cable is required - but remember that the names are computer-centric.

Pin name Abbreviation DB-25 pin DE-9 pin Original function
Shield 1 - Tied to the cable shield on ONE end, to shield the whole cable
Ground GND 7 5 Signal ground reference
Transmit Data TD 2 3 Data from the DTE to the DCE
Receive Data RD 3 2 Data from the DCE to the DTE
Data Set Ready DSR 6 6 Set HIGH by the DCE to indicate it has power
Ring Indicator RI 22 9 Pulsed by the DCE to indicate an incoming call
Data Terminal Ready DTR 20 4 Set HIGH by the DTE to request link establishment
Data Carrier Detect DCD 8 1 Set HIGH by the DCE to indicate it has a connection to the remote modem
Request To Send RTS 4 7 Set HIGH by the DTE to indicate it has data to send
Clear To Send CTS 5 8 Set HIGH by the DCE to allow the DTE to send

Usage

The original usage of these signals were for very simple electrical "slave" DCEs to be connected to the barely more intelligent "master" DTEs, so to establish a communications link required a sequence of steps. As modems became more sophisticated, a lot of the functions became commands over the data link versus dedicated signals - and yet the signals remained. Still later, the Serial Port was used for communicating with local devices rather than modems, and the line between DTE and DCE blurred so that no true "master" was obvious - and yet the signals remained. Indeed, their functions were redefined to better suit the requirements of the local environment, so that the signals became flow controllers rather than requesters/granters.

Original

The original sequence of steps to communicate with a modem were as follows:

  1. The DCE raises DSR to tell the DTE that it is able to be used.
  2. Link establishment:
  3. EITHER

    1. The DTE raises DTR to tell the DCE to establish a link;

    OR

    1. The DCE pulses RI to indicate an incoming call;
    2. The DTE raises DTR to tell the DCE to accept the incoming call.

    At this point, a link has been established, but no data can be transmitted or received.

    Before data can be transmitted:

  4. The DTE raises RTS to request transmission
  5. The local DCE starts transmitting a carrier to the remote DCE
  6. The remote DCE raises DCD to its DTE, to indicate the carrier has been detected
  7. The local DCE raises CTS to indicate that the link is ready for transmission.

Note that, depending on whether the modem is half-duplex or full-duplex, the RTS/CTS/DCD signalling may not be able to be performed in both directions simultaneously. For example, if the modems were connected to a radio transmitter/receiver pair that were operating on the same frequency, the fact that DCD was raised meant that the frequency was already in use, and raising RTS would have no effect.

Original Null Modem

To remove the modem from between two computers, a full "null modem" cable was required to be wired, to emulate what an instant-response modem would look like: for example, raising RTS would instantly raise CTS in response, while simultaneously telling the other computer that data was about to come.

 GND -------------- GND
 
 DTR ------\/------ DTR
 DSR ------/\------ DSR
 
 TX  ------\/------  TX
 RX  ------/\------  RX
 
           /------- DCD
          /
 RTS ----/     /--- RTS
 CTS ---/     /---- CTS
             /
 DCD -------/

This is most definitely a full-duplex cable - but is NOT suitable for today's "null modem" cable, detailed below.

Local link

Where the two computers that need to communicate are so close that no modem is required, a direct cable can be used instead. Here, there is no "prepare channel for transmission" requirement: the link is assumed available all the time. So, at minimum, the two grounds need to be connected, and each Transmit Data pin is connected to the other's Receive Data pin.

However, the remote computer is a limiting factor: it may not be ready to receive. Thus the above signal definitions are modified so that the local computer raises RTS to indicate that it is ready to receive rather than send, and that the cable connects RTS to the remote computer's CTS - which still means "Clear To Send".

Today's Null Modem

This, then, is the current definition of a "null modem" cable. It swaps the relevant pins, and one computer can tell the other to pause transmitting while it catches up:

 GND -------------- GND
 
 TX  ------\/------  TX
 RX  ------/\------  RX
 
 RTS ------\/------ RTS
 CTS ------/\------ CTS

Protocol

Serial Communications breaks the data to be transmitted into smaller pieces, which are sent sequentially over the data line(s). For standard Asynchronous Serial Communications, the pieces are each individual bit of the data. Each bit is converted to a voltage level and held on the line for a fixed time period, before the next bit is transmitted. All the bits are sent sequentially (least significant bit first).

Terms

Standard Asynchronous Serial Communications has four main parameters that need to be configured. These completely define how a single character is sent - future characters are then sent in the identical manner:

  • Baud Rate
  • Data Length
  • Parity
  • Stop Bits

Baud Rate

Before any data can be sent, a pre-arranged rate of transitions needs to be defined. To transmit four 0 consecutive bits, there would be no transition between bits, so it is unclear on the receiving side whether that's a fast 16 bits of zero, or a slow single bit of 0. The "baud rate" or "bit rate" is how long a bit is transmitted for before the next bit is started. Thus both sides know the period of time for one bit.

Start Bit

This is the bit that is transmitted before the first data bit is sent, to take the Transmit Data line off Idle and warn the receiver that data is about to arrive. This is not a configurable parameter - only one Start Bit is ever sent.

Data Length

Both sides need to know how many bits are in each character - the data length. If the link is being used to send binary data, then each "character" would be an eight-bit byte. If it's just (English) text data, then seven-bit ASCII would be sufficient. Old-fashioned teletypes didn't use ASCII, they used an older encoding known as "Baudot" - a five-bit-per-character protocol.

Parity

To help detect transmission errors, after transmitting the agreed number of data characters, the transmitter may send an extra bit called a parity bit. If it does, it would be agreed whether this bit indicated an Even Parity or an Odd parity. The transmitter's parity bit would be a '0' or a '1' to make Even or Odd the number of '1' bits already sent in the data. The receiver would look at the received parity bit, and if it didn't match the expected value, it would know that there was a corruption in transit.

This technique is not perfect: a lot of interference with multiple corruptions per character could coincidentally give the same parity result.

Stop Bits

After the Data and any Parity bits, the transmitter needs to let the line state go back to Idle for a defined time before starting the next character. This time period allows the receiver to re-synchronise its clock to the transmitter's clock every character. In older equipment, it also gave time to for the receiver to actually print the received character! This is why some equipment required two Stop Bits between characters: to give the print head time to print the character. As equipment improved, two bits wasn't needed any more - but one bit was still too quick. So "modern" equipment allowed one-and-a-half Stop Bits to be specified, improving the number of characters per second that the equipment could print. (Remember: in serial communications, a bit is a unit of time. Otherwise "half a bit" would be a nonsense term...)

Data Signalling

The standard Asynchronous Serial Protocol has to cover a multitude of considerations:

  • Not be continually transmitting data;
  • Signal that data is about to be sent;
  • Signal the end of data;
  • Account for the fact that the clocks in different devices are not synchronised;
  • Aid in electrical noise reduction or detection.

With the above in mind, actually transmitting the 1s and 0s of the data is done by setting the line HIGH or LOW in whatever electrical standard is being used (RS-232 has HIGH as negative voltages, and LOW as positive voltages).

Thus the default Idle state of the line is HIGH. This indicates that something is connected, but no data is being sent.

Example

For the following example, assume the link has been configured for the following:

  • 9,600 bits per second baud rate;
  • Eight data bits per character;
  • Even parity;
  • Two stop bits

And that the Transmitter wants to send an ASCII capital 'S'. In ASCII binary, that's 0b01010011. Because there's an even number of '1' bits in this character, an Even Parity bit would be '0'.

Transmission

  1. The TD signal is already at Idle HIGH ('1').
  2. The 'S' is loaded into the Transmit register.
  3. The COM port needs to send a single Start bit to alert the Receiver that data is coming.
  4. A Start ('0') bit is sent for one bit time.
  5. By convention, the Least Significant bit ('1') is sent first, for one bit time.
  6. For seven more bit times, the successive bits are sent: '1', '0', '0', '1', '0', '1', '0'.
  7. Now the transmitter may send a parity bit. Since 'Even parity' is defined, the '0' is sent for one bit time.
  8. Finally, the transmitter needs to put the line back to Idle HIGH ('1') for two stop bit times.

Reception

  1. The receiver is receiving an Idle HIGH.
  2. Suddenly, it notes that the line goes LOW. It immediately starts a half bit time delay.
  3. At the end of that, it re-samples the line:
    1. If it was no longer LOW, it was a noise spike, so the receiver goes back to Idle.
    2. Because it is still LOW, the receiver now waits a full bit time.
  4. It samples the least significant bit as a '1', on the (hopefully) middle of the first data bit's time.
  5. It then waits seven further full bit times, sampling the data line each time for the remaining data bits.
  6. It then waits another bit time to sample the parity bit as '0'.
  7. It then waits a final bit time to sample the stop bit as a '1'.
  8. Because the stop bit is correct, and the parity matches the expected value, a "valid" character has been received.
  9. The receiver immediately starts waiting for another start bit.

Remember, at this point the receiver is still only halfway through receiving the first stop bit. But this mechanism allows minor differences between the transmitter's and receiver's clocks to be accommodated, since the receiver resynchronises its clock on the next Start bit.

More sophisticated COM chips may oversample each bit multiple times, choosing the most common state as the bit's state.

The BREAK Signal

If the transmitter obeys the above protocol, and the receiver has the same settings, then the only communications possible between the transmitter and receiver is data. If the transmitter wants to indicate anything extra to the receiver, outside of the data - for example a Terminal Reset or other signal - it would have to break protocol.

The transmitter could break protocol by sending a bad Parity character - if Parity was being sent. But the only sure flag that the receiver is looking for is the Stop Bit. If the transmitter wants to indicate something extra, it can send a BREAK signal. This is marked as a '0' stop bit - the receiver would detect this as a Frame Error in the received character. If the received character was also all '0's, then this would indicate that a BREAK signal had been sent, and the receiver could do the indicated action: clear the screen; go back to communication defaults; whatever.

Personal tools
Namespaces
Variants
Actions
Navigation
About
Toolbox