Debugging Serial (RS232C) Cables |
---|
Links on the page Background Cable Types and Lengths How Serial RS232 Works Sex and Serial Ports The 9 Pins Common Cable Connections Figuring Out a Cable Tricking the Interface |
Background |
Serial interfaces have been in heavy use for more than 30 years. While there are many other options
for interfacing devices available now, it will probably be another 30 years before these interfaces
disappear.
In the beginning, even before computer screens existed, serial interfaces were used to connect keyboards and teletypes to central processing units. Information typed on the keyboard could then be printed on the "teletypes" which later became known as printers. The first computer screens which also used serial interfaces, were commonly called "Glass Teletypes" because they emulated the teletype printers without using paper. Since then, serial interfaces have been used to connect almost every imaginable type of device including:-
Some devices only ever receive data, such as a computer screen, printer or customer display. Other devices both send and receive data, such as modems, computers and terminals. The fact that serial interfaces can work reliably to connect all of these types of devices indicates how flexible this type of interface really is. This flexibility however, has resulted in some of the most serious drawbacks to the serial interface. This is because it has led to a great range of interpretations as to how the RS232 standard should be applied, resulting in differences between manufacturers. While many of these differences no longer a problem because certain conventions have been adopted that is not always the case. The serial interface is clearly bi-directional, or in other words, capable of passing data in both directions. But not every device actually sends data both ways. Often the manufacturer of Customer Pole displays figure that this device will never send data, so why build in the parts of the interface that send data? But the manufacturer of the computer that you are trying to connect to this customer display has implemented communication both ways and the interface on the computer end won't work until it receives signals from the device on other end that everything is OK... Signals that the other device never provides because it knows it will never use them. To make things worse, the technical documentation supplied by the equipment manufacturers is, often as not, just plain wrong and misleading. All this might sound very bad. The good news is that if you understand how serial interfacing actually works then you can make any serial device connect to any other serial device without a great deal of difficulty. And without having any of the technical documentation from the manufacturer. To illustrate how to interface serial devices without any documentation, we are going to interface this old receipt printer so that it works with SELLmatix Point of Sale running on a PC. ![]()
![]() First, a few more details about serial interfaces and how they work. |
Cable Types and Lengths | ||||||||||||||||
The main reason for choosing a device with a serial interface these days, is the length of the cable that can be used.
Parallel ports are much faster for data transfer in a single direction which makes them very good for printers, but parallel cables don't
work much beyond 5 meters in length. USB devices are good for bi-directional data transfer, but after 15 meters, you begin to need repeaters to boost the signal. With serial ports, however, you can go far, far beyond these limitations. But the restriction is communication speed and the correct type cable must be used, and the cable situated correctly. The original RS-232 specification limited cable length to 15.25 meters at a data rate of 19,200 bits per second, or a cable length equal to a capacitance of 2500 pF. At the time, few UART chips (Universal Asynchronous Receiver Transmitter) that control the Serial interface could operate at 19,200bps. Now, however, they can go well beyond. Maximum cable length depends on a range of factors including the performance of the UART chips, the cable capacitance, inductance and screening. These are difficult to quantify, but the main factor is the communication speed. Generally speaking, doubling the data rate halves the recommended maximum length. As a general rule of thumb, the following table shows what can reasonably be expected with good quality cable routed in such a manner as to minimise interference.
In an effort to tweak things and get the best performance, people are sometimes tempted to try things such as increasing the size of the cable to reduce voltage drop. With Serial interfaces however, this is not the problem because the acceptable voltage ranges are very broad. Instead, the limiting factor is the capacitance of the cable, so that in fact, thinner serial cables work better over long distances than thick cables. Serial cables are very vulnerable to interference. It is very important that the correct type of cable be used. The following picture shows the differences between correct serial interface cable on the left, and Unshielded Twisted Pair (UTP) or CAT5 cable that is used for network (LAN) connections on the right.
![]() Notice that the serial cable has strands of wire woven around the inner insulated cores. This shielding is not used for any electrical connection, it is merely to reduce the effect of electromagnetic fields. When making up a serial cable, this shielding is stripped back and cut off. If you see a serial cable where any electrical connection is made using this shielding, that tells you that the person that made the cable did not know what they were doing!. This shielding does not exist on UTP CAT5 network cable. Also notice that the inner wire cores on the serial cable are made up of multiple small strands of wire, while the CAT5 cable has a single core in each of the wires. The CAT5 network cable is also referred to as UTP meaning "Unshielded Twisted Pair". Notice that it consists of 4 pairs that are each twisted together, for a total of 8 wires. Serial cables could have anywhere between 2 and 9 cores, since not all of the wires need to be present in all serial cables. The electrical properties of shielded serial cable and CAT5 network cable are quite different. We continue to be amazed at how many contractors there are who should know better, but think that they can use CAT5 cable for serial connections because there are a similar number of cores. In certain cases, you might get away with this if you are lucky. But if you are not so lucky, it can result in data loss, devices rebooting or locking up for no apparent reason, and even physical damage to the equipment.
Interference Other electrical devices including loudspeakers, refrigeration units, air conditioners and TV sets all produce electromagnetic interference, and as wide a distance as possible should be maintained between these devices and serial cables. Fluorescent lights pose a particular hazard to serial cables. We have seen serial ports at both ends of a serial cable blown where the serial cable was run across a fluorescent at 90 degrees at a distance of one foot. Longer serial cables that avoid these sources of interference are far better then short cables in close proximity. | ||||||||||||||||
How Serial RS232 Works |
Serial interfaces send data from one device to the other using a single wire in the cable.
Usually a byte of data contains 8 bits. A bit is a single on or off, zero or one state. For
every byte of data sent over a serial link, the bits are sent down the wire one after another. Data traveling in the other direction works the same way, but a different wire is used in the cable. Parallel interfaces, on the other hand use 8 wires to transmit data, and the value of the various bits are all sent at the same time. Parallel interfaces are faster than serial, but have very limited capability to send data in the other direction. In a serial interface there is always a Signal Ground connection and voltages on the other pins are compared to the signal ground pin. The devices on each end each set voltages on some pins, and read the voltage from other pins. You don't need to know this, but a logic 0 (low) condition is indicated by a voltage compared to the signal ground of between 3 and 25 volts. Logic 1 (high) is indicated by a voltage of between -3 and -25 volts. Voltages in the range between -3 and +3 volts are a dead area that allows for the absorption of noise and interference. In addition to the transmitted and received data pins and the Signal Ground, there are a couple of other circuits that are usually, but not always used. If you are cabling between a device that does require these other circuits, and a device that does not use the other circuits, then you will need to spoof the circuit and trick the device that does require the circuit into thinking that it is there. These other circuits are:-
With modems which transfer a lot of data in both directions, however, you will need more wires, but even then there are multiple ways of handling the interface, so that you could get away with less than all the circuits if it was absolutely necessary. Serial interfacing involves simply working out which of these circuits are used by each of the devices, and building a cable that makes the correct connections on that circuit. |
Sex and Serial Interfaces |
For reasons that this author has never been able to understand, plugs which have
pins that stick out are referred to as "Male", and plugs that have sockets into which
the pins are inserted when the two plugs are connected, are referred to as "Female".
This is of no significance whatever when it comes to serial interfaces. Adding to the confusion is that fact that there are two common types of serial plugs and sockets used that contain a different number of pins. These are referred to a DB9 and DB25 where the 9 and the 25 indicate how many pins are used in the connectors. In DB9 connectors is it common for all pins to be used. In DB25 connectors most of the pins are not used and not connected.
![]()
DTE/DCE... The Serial Version of Sex Far back in the mists of ancient time, in the great and glorious days of the "glass teletype", most serial cable was flat "ribbon" cable and the plugs were usually crimped onto the cable without using any solder joints. But this meant that Pin 1 at one end, was always Pin 1 at the other end. Since an output at one end of a serial cable is an input to the device on the other end, straight through connections would mean that an output was connected to an output at the other end, and the same with inputs. This obviously wont work, since an output at one end needs to be marched to an input at the other end. Instead of matching inputs to outputs in the cable, it was decided to try and do the matching inside the devices. In other words, one of the devices would have its pins do what is described, and the other device would have its connections do the exact opposite. In this way, straight through ribbon cables could be used. Devices which have their pins do what is described in the interface, are called DTE or Data Terminal Equipment. Devices which do the opposite of what is described in the interface are called DCE or Data Communication Equipment. The original concept was that a DTE device would always connect to a DCE device. But there was never any clear way to define type of device was a DTE and what type of device was DCE. Some make sense. It makes sense to consider a modem as a DCE device and a terminal as a DTE. But what about a printer? Data terminates there, and is not passed on, yet printers are almost always connected to terminals. Almost immediately the differentiation between DTE and DCE became blurred. DTE devices were connected to other DTE devices and the same with DCE devices. In today's world, the terms DTE and DCE rarely make sense. Some devices are DTE and the pins on their interface do what is described in the standard. Others are DCE, and their pins do the opposite. The manuals for the devices always describe the signal names from the point of view of the DTE, but the documentation of DCE devices almost never indicates that the device is DCE, and that the function of these pins is the opposite to what is described in the standard. When designing a cable to connect serial devices, the first thing you need to do is determine whether the device is DTE or DCE. Fortunately, this is very easy, and is described below. But any inference you draw from the plugs being male/female, the type of device or the printed documentation, will very likely be wrong. |
The Nine Pins | ||||||||||||||||||||||||||||||||||||||||||||||||||
The following table shows the pins used in DB9 and DB25 connectors and their
function on DTE and DCE devices.
| ||||||||||||||||||||||||||||||||||||||||||||||||||
Common Cable Configurations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
While there are a lot of different ways that serial cables can be wired up
depending on the devices, there are a few methods that are used more often than others
and in many cases, you can follow these standard wiring diagrams if the type of connection
is one of the more common types.
Computer to Printer To fully implement a DTE to DTE serial cable using wither 9 pin or 25 pin connectors, use the following diagram.
This cable fully implements a serial interface between two DTE devices, and uses 7 connections in the cable. This is because the Signals RI-Ring Indicator and DCD-Data Carrier Detect which apply only to modems are not connected since modems are always DCE devices are these are both outputs from the modem with no corresponding output from the DTE device. This cable will work equally well for Hardware handshaking and for XON/XOFF handshaking. If you will never use XON/XOFF (software) handshaking on this cable, then you can eliminate the first connection shown in this table, since the printer will never transmit data to the computer. If you will never use hardware handshaking DTR/DSR and/or RTS/CTS handshaking, and will always use software handshaking XON/XOFF the you can use the following cable connections.
If you use this type3 of cable connection, many hardware devices still require that their inputs be set correctly, or they will not work. You can fool the hardware by using the device outputs to set the inputs. This means that at each end, you would short out:-
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Figuring Out the Cable | ||||||||||||||||||||||||||||||||
Back to the example of the printer shown earlier in this article. So, you have a device
and you think it uses a serial interface, but you don't know even the model or any of the details.
What do you do? The first thing is to determine if the device is a DTE or a DCE serial device. Once you know this, you can figure out most of the other connections. The RS-232 standard, says that idling tranmitter pins are held with a voltage on the pin. In other words, if the device is turned on but doing nothing, there will be a voltage on the pins it tranmists on and if we can identify these pins we can compare the pin numbers with the cabling chart to determine if the device is a DTE or DCE.
Tester LED's or Light Emitting Diodes light up when voltage passes in one direction, but not the other direction. By wiring up two different colored LED's in opposite directions we can detect a logic 0 and a logic 1 on any of the serial lines. But in this case, we don't need to work out whether a signal is a logic 0 or a logic 1, but only if there is a signal on a particular pin. So it does not matter whether we have different colored LED's, just that they are connected in opposite directions so that one of these LED's will light up if a signal is present. The 470 ohm resistor is simply to protect the LED's because otherwise the LED's would go "pop" and stop working. The voltages that appear on a serial interface are in the range of +25 Volts and -25 volts, and while this is perfectly safe, and you will never damage the hardware even if you short out pins incorrectly, this voltage is too much for the LED's. The connections should be made as follows.
![]()
DTE or DCE The RS232 Serial standard states that idling transmitter pins are kept low. In other words, if a pin on the interface is a transmitting pin, and the device is turned on, there should be a voltage asserted on that pin. Connecting one end of the tester to the Signal Ground pin on the printer, we can then test by touching the other end of the tester on each of the other pins on the printer plug, and see which of the pins lights up one of the LED's as follows:-
![]() Normally on a DTE device, one would expect Pin 2 which is transmitted data to also have a voltage because this is the pin on which a DTE device transmits data. This device, being a printer, is never going to transmit data however, so it is quite likely that Pin 2 is not connected inside the printer. Similarly this printer is never going to assert RTS (same reason... it is never going to send data), so it is not really surprising on an older printer to see these signals unused. What is more interesting, however, is that pressing the "Online" button on the front panel of the printer causes the other LED on Pin 20 of our tester to light up as follows:-
![]() Now to check the computer. Performing the same tests on the computer end which has a 9 pin male connector, shows that pins 3 and 4 both turn on one of the LED's. Pin 3 on a DB9 is TXD (transmitted data by a DTE) and Pin 4 is DTR (Data Terminal Ready for a DTE). So the computer is also a DTE. The preferable way to make up a cable is to connect all the signals correctly, even if they are not required. That way, there is a good chance that the same cable could be used for other connections as well. If one of the devices requires a signal that the other device does not supply, then we can do a slimy hack to fool the interfaces into working. But we will only do this if absolutely necessary. The way to match the signals between two DTE devices where one used a DB9 connector and the other used a DB25 connector would be as follows:-
| ||||||||||||||||||||||||||||||||
Tricking the Interface |
At this point, data should pass over the interface. Whether that data makes sense or not, is another matter.
It is necessary to have the serial ports configured to use the same Baud Rate, Data Bits, Stop Bits and parity. On computers, these parameters are normally set in software. On devices such as printers, they are normally set using dip switches. If you do not have documentation for the dip switch settings, it is usually better to try and use the existing settings if you are using an old device, assuming that it did work previously, and knowing that there will be ways of setting the dip switches so that the device does not work at all. If experimenting blind, then it is important to keep accurate records of what you have tried, otherwise things can go badly wrong. When changing dip switch settings, remember that the device only "reads" these settings when it is powered on. You will need to turn off the device and then turn it back on for the new settings to take effect. In this example, everything worked perfectly with the cable made up. There are some cases where you may need to "spoof" one of the devices that requires an input not required by the other device. Say for example, you have a computer which requires that the DSR input be set in order to work, but the other device does not provide a DTR output. In that case, nothing would happen over the interface. You could fool the computer into thinking that DSR has been set by connecting the DTR and DSR pins on the computer end. The computer would then assert DTR, and when it went to read DSR, it would be reading the output from its own DTR. Another common hack is to connect the RTS, CTS and DCD pins inside the connector. This means that when a device asserts its RTS signal, this signal is taken as input from the other device telling it that the other device can receive data and also has DCD from a modem asserted. Do these trick only if it is absolutely necessary. Many older cables routinely had pins 4 and 5 joined inside the plugs as well as pins 6, 8, and 20. This is not good practice, and should be avoided if possible. |