Serial Communication (Computer Hardware)
What is Serial Communication?
Serial is a very common protocol for device communication that comes standard on just about every PC. Most computers include two RS-232 standard based serial ports. Serial is also a common communication protocol that is used by many devices for instrumentation. Furthermore, serial communication can be used for data acquisition in conjunction with a remote sampling device.
The concept of serial communication is simple. The serial port sends and receives bytes of information one bit at a time. Although this is slower than parallel communication, which allows the transmission of an entire byte at once, it is simpler and can be used over longer distances. For example, the IEEE 488 specifications for parallel communication state that the cabling between equipment can be no more than 20 meters total, with no more than 2 meters between any two devices; serial, however, can extend as much as 1200 meters.
How Serial Communication Works?
Serial communication uses a transmitter to send data, one bit at a time, over a single communication line to a receiver. (Computer serial ports transmit and receive data bit by bit.) You can use this method when data transfer rates are low or you must transfer data over long distances. Serial communication is popular because most computers have one or more serial ports, so no extra hardware is needed other than a cable to connect the instrument to the computer or two computers together.
Typically, serial is used to transmit ASCII data. Communication is completed using 3 transmission lines: (1) Ground, (2) Transmit, and (3) Receive. Since serial is asynchronous, the port is able to transmit data on one line while receiving data on another. Other lines are available for handshaking, but are not required. The important serial characteristics are baud rate, data bits, stop bits, and parity. For two ports to communicate, these parameters must match on each.
During transmission, the serial device driver program on the computer CPU takes each byte from the main computer memory (typically 8 KB in size) and places it onto the data bus along with the serial port I/O address. The byte then travels through the data bus and is stored in the serial port hardware buffer. When the serial port is ready to transmit data, it fetches the data from this hardware buffer, places it in its shift register, and transmits each bit over the communication line.
The shift register is one byte long. From the shift register, the serial port transmits data bit by bit. When the last bit in the shift register is transmitted, the serial port must request the next byte from the CPU via an interrupt request (IRQ). However, the CPU is usually busy when the serial port needs the byte, so the serial port must interrupt the CPU. The CPU may not respond to this interrupt immediately, which may lead to a delay in bit transmission. To solve this problem, the serial port has a hardware buffer; while the serial port transmits bits from its shift register, the hardware buffer actually sends the IRQ to the CPU to request the next byte. The advantage of this process is that when the serial port takes the final bit from its shift register and is ready to transmit the next byte, it does not need to send an IRQ to the CPU. Rather, the next byte is readily available in the buffer. This buffer is a FIFO buffer and is called a Universal Asynchronous Receiver Transmitter (UART). Early UART versions had a 1-byte buffer, but recent versions such as 16550 and 16750 have up to a 64-byte buffer. The larger buffer means interrupt requests occur less often, and the CPU can respond to IRQs more efficiently and devote more time to other tasks.
Receiving data is similar to transmission. The serial port receives the data bit by bit and places it in the shift register. When a byte is received, it is transmitted into the UART. With modern UARTs and their larger buffers, the serial port continues placing the bytes. When the buffer holds 1, 4 , 8, or 14 bytes, the port sends an IRQ to the CPU to pick up the bytes. The port does not wait until the buffer contains 16 bytes, because the CPU may not respond immediately due to the interrupt request process overload involved (due to identifying the type of request, I/O address, etc.). While the CPU responds to the IRQ, if more than 2 bytes are received, the new byte may overwrite the byte in the buffer. This creates a Hardware Overrun Error.
UART (Universal Asynchronous Receiver Transmitter)
A UART is a 40-pin serial chip on the PC motherboard. During transmission, the UART converts the bytes from the PC parallel bus to the serial bit stream. During receiving, the UART builds the serial bits into a parallel byte and either sends the parallel byte to the CPU or places it in its buffer. The UART does not do anything with the data; it just receives it and sends it. Early UARTs had 1-byte buffers; therefore, after receiving or building every byte, they needed to send an IRQ to either send or pick up the next byte. This process is acceptable with low transfer speeds. But at high speeds, the CPU must service the request so often that it either cannot give sufficient time to other processes or cannot service the IRQ in a timely manner. In the latter case, the new byte may come before the old byte has been received, thus causing a Hardware Overrun Error. Modern UARTs such as the 16550 and 16750 (also called FIFO UARTs) have up to a 64-byte buffer. They also have adjustable trigger levels of 1, 4, 8, or 14, meaning they can send an IRQ after collecting 1, 4, 8, or 14 bytes.
In serial communication, data bits received at the serial port are bundled into a byte and transmitted into the serial port hardware buffer. From the buffer, the byte is sent into the CPU. If a new byte arrives before the byte in the buffer is moved into the CPU, a Hardware Overrun Error occurs.
A Hardware Overrun Error may happen for several reasons:
1. The serial port hardware buffer is not large enough. For example, early UART chips can store only 1 byte. After each byte is received, the serial port sends an IRQ to the CPU to pick up the next byte. Therefore, the CPU must respond very quickly, or a new byte may arrive and cause an overrun error. The new UART chips have up to a 64-byte buffer (16 bytes in the 16550A and 64 bytes in the 16750), so the buffer can store more bytes before the serial port sends an IRQ to the CPU. Therefore, the CPU has sufficient time to respond to the interrupt request and act on it. These new UARTs also have adjustable trigger levels of 1, 4, 8, and 14, meaning they can send interrupt requests to the CPU on receiving 1, 4, 8, or 14 bytes.
2. Some other process might have disabled the interrupt.
3. In Windows, the CPU must take care of many processes and may not respond to fast IRQs from the serial port. Also, if there are high-priority IRQs from other processes, the CPU may ignore the serial port IRQ.
To prevent hardware overrun errors:
1. Try hardware handshaking.
2. See if the UART is an older un-buffered version or a new buffered UART (such as a 16550A or 16750). You should use a buffered UART for the reasons discussed above.
3. Change the Receive (Rx Trigger) buffer to 8, 4, or 1 (1 is a last resort).
Serial communication requires that you specify the following four parameters to match between computer and serial peripheral:
1. The baud rate of the transmission
2. The number of data bits encoding a character
3. The sense of the optional parity bit
4. The number of stop bits
Each transmitted character is packaged in a character frame that consists of a single start bit followed by the data bits, the optional parity bit, and the stop bit or bits.
Baud rate is a measure of how fast data are moving between instruments that use serial communication. RS-232 uses only two voltage states, called MARK and SPACE. In such a two-state coding scheme, the baud rate is identical to the maximum number of bits of information, including control bits that are transmitted per second.
Baud rate: a speed measurement for communication. It indicates the number of bit transfers per second. For example, 300 baud is 300 bits per second. When we refer to a clock cycle we mean the baud rate. For example, if the protocol calls for a 4800 baud rate, then the clock is running at 4800Hz. This means that the serial port is sampling the data line at 4800Hz. Common baud rates for telephone lines are 14400, 28800, and 33600. Baud rates greater than these are possible, but these rates reduce the distance by which devices can be separated. These high baud rates are used for device communication where the devices are located together, as is typically the case with GPIB devices.
Data bits: a measurement of the actual data bits in a transmission. When the computer sends a packet of information, the amount of actual data may not be a full 8 bits. Standard values for the data packets are 5, 7, and 8 bits. Which setting you choose depends on what information you are transferring. For example, standard ASCII has values from 0 to 127 (7 bits). Extended ASCII uses 0 to 255 (8 bits). If the data being transferred is simple text (standard ASCII), then sending 7 bits of data per packet is sufficient for communication. A packet refers to a single byte transfer, including start/stop bits, data bits, and parity. Since the number of actual bits depends on the protocol selected, the term packet is used to cover all instances.
Stop bits: used to signal the end of communication for a single packet. Typical values are 1, 1.5, and 2 bits. Since the data is clocked across the lines and each device has its own clock, it is possible for the two devices to become slightly out of sync. Therefore, the stop bits not only indicate the end of transmission but also give the computers some room for error in the clock speeds. The more bits that are used for stop bits, the greater the lenience in synchronizing the different clocks, but the slower the data transmission rate.
Parity: a simple form of error checking that is used in serial communication. There are four types of parity: even, odd, marked, and spaced. Of course, the option of using no parity is also available. For even and odd parity, the serial port will set the parity bit (the last bit after the data bits) to a value to ensure that the transmission has an even or odd number of logic high bits. For example, if the data was 011, then for even parity, the parity bit would be 0 to keep the number of logic high bits even. If the parity was odd, then the parity bit would be 1, resulting in 3 logic high bits. Marked and spaced parity does not actually check the data bits, but simply sets the parity bit high for marked parity or low for spaced parity. This allows the receiving device to know the state of a bit so as to enable the device to determine if noise is corrupting the data or if the transmitting and receiving devices' clocks are out of sync.
What is handshaking?
The method used by RS-232 for communication allows for a simple connection of three lines: Tx, Rx, and Ground. However, for the data to be transmitted, both sides have to be clocking the data at the same baud rate. Even though this method is sufficient for most applications, it is limited in being able to respond to problems such as the receiver getting overloaded. This is where serial handshaking can help. In this section we will discuss three of the most popular forms of handshaking with RS-232: Software Handshaking, Hardware Handshaking, and Xmodem.
Software Handshaking: The first form of handshaking we will discuss is software handshaking. This style uses actual data bytes as control characters, similar to the way GPIB uses command strings. The lines necessary are still the simple three line set of Tx, Rx, and ground since the control characters are sent over the transmission line like regular data. The function SetXMode allows the user to enable or disable the use of two control characters, XON and XOFF. These characters are sent by the receiver of the data to pause the transmitter during communication.
As an example, assume that the transmitter begins to transmit data at a high baud rate. During the transmission, the receiver finds that the input buffer is becoming full due to the CPU being busy with other duties. To temporarily pause the transmission, the receiver sends XOFF, typically decimal 19 or hex 13, until the input buffer has been emptied. Once the receiver is ready for more data it sends XON, typically decimal 17 or hex 11, to resume communication. LabWindows will send XOFF when its input buffer becomes half full. In addition, in case the XOFF transmission was corrupted, LabWindows will also transmit XOFF when the buffer has reached 75% and 90% capacity. Obviously, the transmitter must also be following this protocol for it to succeed.
The biggest drawback to this method is also the most important fact to keep in mind: decimal 17 and 19 are now off limits for data values. In ASCII transmissions this typically does not matter since these values are non-character values; however, if the data is being transmitted via binary, it is very likely that these values could be transmitted as data and the transmission would fail.
Hardware Handshaking: The second method of handshaking is to use actual hardware lines. Like the Tx and Rx lines, the RTS/CTS and DTR/DSR lines work together with one being the output and the other the input. The first set of lines are RTS (Request to Send) and CTS (Clear to Send). When a receiver is ready for data, it will assert the RTS line indicating it is ready to receive data. This is then read by the sender at the CTS input, indicating it is clear to send the data. The next set of lines are DTR (Data Terminal Ready) and DSR (Data Set Ready). These lines are used mainly for modem communication. They allow the serial port and the modem to communicate their status. For example, when the modem is ready for data to be sent from the PC, it will assert the DTR line indicating that a connection has been made across the phone line. This is read in through the DSR line and the PC can begin to send data. The general rule of thumb is that the DTR/DSR lines are used to indicate that the system is ready for communication where the RTS/CT
S lines are used for individual packets of data.
In LabWindows, the function SetCTSMode enables or disables the use of hardware handshaking. If the CTS mode is enabled, LabWindows employs the following rules:
When the PC sends data:
The RS-232 library must detect that its CTS line is high before sending data.
When the PC receives data:
If the port is opened and the input queue has room for data, the library raises RTS and DTR.
If the port's input queue is 90% full, the library lowers RTS and leaves DTR high.
If the port's input queue is nearly empty, the library raises RTS and leaves DTR high.
If the port is closed, the library lowers RTS and DTR.
XModem Handshaking: The last mode discussed here is the XModem file transfer protocol. This protocol is very common in modem communication. Although it is often used for modem communication, the XModem protocol can be used directly between other devices if they both follow the protocol. In LabWindows, the actual implementation of XModem is hidden from the user. As long as the PC is connected to another device using XModem protocol, the LabWindows' XModem functions can be used to transfer files from one site to another. The functions are XModemConfig, XModemSend, and XModemReceive.
XModem uses a protocol based on the following parameters: start_of_data, end_of_trans, neg_ack, ack, wait_delay, start_delay, max_tries, packet_size. These parameters need to be agreed upon by both sides, and standard XModem has a standard definition of these; however, they can be modified through the XModemConfig function in LabWindows to meet any requirement. The way that these parameters are used in XModem is by having the neg_ack character sent by the receiver. This tells the sender that it is ready to receive data. It will try again with start_delay time in-between each try until either it reaches max_tries or receives start_of_data from the sender. If it reaches max_tries it will inform the user that it was unable to communicate with the sender. If it does receive start_of_data from the sender, it will read the packet of information that follows. This packet contains the packet number, the complement of the packet number as an error check, the actual data packet of packet_size bytes, and a checksum on t
he data for more error checking. After reading the data, the receiver will call wait_delay and then send ack back to the sender. If the sender does not receive ack, it will re-send the data packet max_tries or until it receives ack. If it never receives the ack, it informs the user that it has failed to transfer the file.
Since the data must be sent in packets of packet_size bytes, when the last packet is sent, if there is not enough data to fill the packet, the data packet is padded with ASCII NUL (0) bytes. This can cause the received file to be larger than the original. It is also important to remember not to use XON/XOFF with the XModem protocol since the packet number from the XModem transfer is very likely to increment to the XON/OFF control character values, which would cause a breakdown in communication.
Serial Standards
There are many different recommended standards of serial port communication, including the following most common types. (RS = Recommended Standard)
1. RS-232 (ANSI/EIA-232 Standard)
The RS-232 is a standard developed by the Electronic Industries Association (EIA) and other interested parties, specifying the serial interface between Data Terminal Equipment (DTE) and Data Communications Equipment (DCE). The RS-232 standard includes electrical signal characteristics (voltage levels), interface mechanical characteristics (connectors), functional description of interchange circuits (the function of each electrical signal), and some recipes for common kinds of terminal-to-modem connections. The most frequently encountered revision of this standard is called RS-232C. Parts of this standard have been adopted (with various degrees of fidelity) for use in serial communications between computers and printers, mouse, modems, and other equipment, as well as industrial instrumentation. Because of improvements in line drivers and cables, applications often increase the performance of RS-232 beyond the distance and speed listed in the standard. RS-232 is limited to point-to-point connections between PC serial ports and devices. RS-232 hardware can be used for serial communication up to distances of 50 feet. The serial ports on standard IBM-compatible personal computers follow RS-232.
Devices that use serial cables for their communication are split into two categories. These are DCE and DTE. DCE are devices such as a modem, TA adapter, plotter, and so on, while DTE is a computer or terminal. RS-232 serial ports come in two sizes, the D-Type 25-pin connector and the D-Type 9-pin connector (All types known as DB connector. 15, 37 and 50-pin sizes available). Both of these connectors are male on the back of the PC. Thus, you require a female connector on the device.
A DB-9 connector is commonly used for the 1st serial port (COM1) on a PC, which is typically connected to the mouse. A DB-25 connector is used for the 2nd serial port (COM2), often connected to a modem, as well as the parallel port (see printer cable). DB-25s are also used in a wide variety of communications devices. A high-density DB-15 connector is used for the VGA port on a PC, which has 15 pins in the same shell as the 9 pins in the DB-9 connector. DB-15 connector also used as Game port on PC and Thick Ethernet.
Variations of the RS 232 standard
There are number of different specifications and standards that relate to RS 232. A description of some of the RS 232 standards and the various names and references used is given below:
· EIA/TIA-232: This reference to the RS 232 standard includes the names of the first and current sponsoring organisations, the Electronic Industries Alliance (EIA) the Telecommunications Industry Alliance (TIA).
· RS-232C: This was the designation given to the release of RS 232 standard updated in 1969 to incorporate many of the device characteristics.
· RS-232D: This was the release of the RS 232 standard that occurred in 1986. It was revised to incorporate various timing elements and to ensure that the RS 232 standard harmonised with the CCITT standard V.24.
· RS-232F: This version of the RS 232 standard was released in 1997 to accommodate further revisions to the standard. It is also known as TIA-232-F.
· V24: The International Telecommunications Union (ITU) / CCITT (International Telegraph and Telephone Consultative Committee) of the ITU developed a standard known as ITU v.24, often just written as V24. This standard is compatible with RS232, and its aim was to enable manufacturers to conform to global standards and thereby allow products that would work in all countries around the world. It is entitled "List of definitions for interchange circuits between data terminal equipment (DTE) and data circuit-terminating equipment (DCE)."
· V28: V.28 is an ITU standard defining the electrical characteristics for unbalanced double current interchange circuits, i.e. a list of definitions for interchange circuits between data terminal equipment (DTE) and data circuit-terminating equipment (DCE).
· V10: V.10 is an ITU standard or recommendation for unbalanced data communications circuits for data rates up to 100 kbps that was first released in 1976. It can inter-work with V.28 provided that the signals do not exceed 12 volts. Using a 37 pin ISO 4902 connector it is actually compatible with RS423.
2. RS-449, RS-422, RS-423
The RS-449, RS-422, and RS-423 are additional EIA serial communication standards related to RS-232. RS-449 was issued in 1975 and was supposed to supersede RS-232, but few manufacturers have embraced the newer standard. RS-449 contains two sub specifications called RS-422 and RS-423. While RS-232 modulates a signal with respect to a common ground, or single-ended transmission, RS-422 modulates two signals against each other, or differential transmission. The RS-232C receiver senses whether the received signal is sufficiently negative with respect to ground to be a logical 1, whereas the RS-422 receiver senses which line is more negative than the other. This makes RS-422 more immune to noise and interference and more versatile over longer distances. The Macintosh serial ports follow RS-422, which can be converted to RS-423 by proper wiring of an external cable. RS-423 can then communicate with most RS-232 devices over distances of 15 m or so.
RS-422 (EIA RS-422-A Standard) is the serial connection used on Apple Macintosh computers. RS-422 uses a differential electrical signal, as opposed to unbalanced signals referenced to ground with the RS-232. Differential transmission, which uses two lines each for transmit and receive signals, results in greater noise immunity and longer distances as compared to the RS-232. The greater noise immunity and distance are big advantages in industrial environments.
RS-485 (EIA-485 Standard) is an improvement over RS-422, because it increases the number of devices from 10 to 32 and defines the electrical characteristics necessary to ensure adequate signal voltages under maximum load. With this enhanced multidrop capability, you can create networks of devices connected to a single RS-485 serial port. The noise immunity and multidrop capability make RS-485 the serial connection of choice in industrial applications requiring many distributed devices networked to a PC or other controller for data collection, HMI, or other operations. RS-485 is a superset of RS-422; thus, all RS-422 devices may be controlled by RS-485. RS-485 hardware may be used for serial communication for up to 4000 feet of cable
National Instruments serial interfaces give you connectivity to the many serial devices available for industrial, manufacturing, and laboratory data acquisition. Using a National Instruments RS-485 product, one can connect up to 31 multidropped devices to a single port or use several ports to communicate with a number of point-to-point instruments. All these serial interfaces use high performance UARTs.
These serial interfaces have been designed for communication using asynchronous serial protocols. Protocols such as OptoMux and ModBus can be run using these serial interfaces, however these protocols are not directly supported through National Instruments Serial Software.
The National Instruments ENET-232 and ENET-485 serial device servers provide a mechanism to transparently control serial devices using your local Intranet or even the Internet. Applications written for a plug-in or local serial port now can use the ENET-232 and the ENET-485 to connect to remote serial ports with absolutely no change required in the application software (under Windows). These device servers connect either high-speed (100 Mb/s) or low-speed (10 Mb/s) Ethernet networks to asynchronous serial ports for communication with serial devices. Since these devices are controlled using the Ethernet network, the distance limits imposed by the RS-232 and 485 standard do not apply and the ports can be as far away from the controlling machine as the network allows. The distance requirement between the ENET-232/485 port and the instruments port do still apply.
All serial interfaces include an enhanced serial driver for full interrupt-driven, buffered I/O and reliable interrupt sharing between ports. The National Instruments serial driver maintains complete software compatibility with the standard Windows serial driver, so existing applications can seamlessly use the ports on any National Instruments serial interface without a special function library. You can program all interfaces with LabVIEW, Measurement Studio, Lookout, C/C++, Visual basic, or any application package written to the Microsoft serial driver interface.
Modern Serial Implementations
USB or the Universal Serial Bus Interface is now well established as an interface for computer communications. In many areas it has completely overtaken RS232 and the parallel or Centronics interface for printers, and it is also widely used for memory sticks, computer mice, keyboards and for many other functions. One of the advantages of USB is its flexibility: another is the speed that USB provides.
USB provides a sufficiently fast serial data transfer mechanism for data communications, however it is also possible to obtain power through the connector and this has further added to the popularity of USB as many small computer peripherals may be powered via this. From memory and disk drives to other applications such as small fans and coffee cup warmers, the USB port on computers can be used for a variety of tasks. To download USB Specifications: http://www.usb.org/developers/docs/.
FireWire is another implimentation by Apple and it's also known as IEEE1394.
List of Serial Communicative Devices:
Following is a list of devices connects to PC using serial port or use serial communication.
1. Serial Mouse (9 pin-DB-9 connector) (COM1)
2. Serial Keyboard
3. VGA Monitor (15 pin-DB-25 connector)
4. Serial Printer
5. Serial Scanner
6. Modem (DB-25 connector) (COM2)
7. Plotter
8. TA Adaptor
9. Game port (DB-15)
10. Thick Ethernet (DB-15)
11. All USB devices
12. FireWire Devices (Ex: Portable FireWire Hard Drive etc…)