Practical Tips on Serial Communications

In many cases getting serial output from a computer or controller is a no-brainer. You don't need to know how it works--just hook up to a designated port or pin and there you are. But when you're building from scratch or troubleshooting a poorly documented system it can be valuable to understand some under-the-hood details. That's where this app note comes in.

RS-232 and Variations

RS-232 is a standard that describes a common method of serial signalling using a voltage that switches between a positive level for logic "0" and a negative level for logic "1". Per the standard,

Many products fudge on the standard. For example, a system without a negative power supply may define it this way:

Serial outputs generated by the BASIC Stamp and non-UART-equipped microcontrollers often use this approach, since it saves the cost of a serial line-driver IC.

Serial inputs on our products work fine with either real RS-232 (+/-15V), or its logic-level counterpart (0-5V). Our products' approximate serial logic thresholds are:

RS-232 signal
Figure 1. Simulated RS-232 signal and
serial logic thresholds for our products.

Tips to Remember

Just knowing a little about the signalling voltages gives you a few strong hints for troubleshooting and design:

Serial Mechanics and Timing

Let's walk through the transmission of one byte of data. Per figure 1, the serial line starts off low, in its idle or stop-bit state. It can stay in this state forever--the receiving device understands this to mean that no data is available. When there is data to send, the following sequence of events occurs (from the perspective of the sender):

Notes:
1. A bit time is defined as 1/bps. For example, at 9600 bps the bit time is 1/9600 = 104 microseconds.
2. The "significance" of a bit or digit is its relative value. When we write numbers, the most-significant bit/digit is the one furthest to the left.

Figure 2 shows how this might look on an oscilloscope.

start and stop bits
Figure 2. Serial data is framed
by a start bit and a stop bit.

It should be obvious that timing is very important to proper reception of this kind of serial data (called asynchronous serial data because there's no separate clock signal to help the receiver sort out the bits--just the relative timing of the start and stop bits). Under ideal conditions, no more than a few percent timing error is tolerable. For consistently reliable communication, the goal should be to keep timing errors to less than one percent.

Tips to Remember

Here are a couple of practical hints regarding serial timing:

Notes:
1. UART (universal asynchronous receive/transmit chip), a hardware device for serial communication.
2. Bit-banging means generating serial output in software as opposed to with a hardware UART. Timing accuracy of bit-bang output is only as good as the program that generates it, the accuracy of the system clock, and in interrupt-equipped systems, the degree to which other interrupts affect timing. Some compilers generate truly bad bit-bang serial timing, based on the assumption that the receiver is always going to be a PC (with its generous error tolerance).

Testing Serial Timing

If you have a decent oscilloscope, you can easily perform a sanity check on serial timing. Set up the computer that you wish to test to continuously transmit the space character (ASCII 32; 0x20 hex). View the output on the oscilloscope (triggered on the rising edge). You'll see a rectangle with a notch near the middle. Adjust the scope so that you can accurately measure the time-width of the notch. It should be exactly one-bit-time wide. If it's off by more than a percent, that's a potential problem.

Baud Rate Bit Time (us) Max Error (us)
1200 833.33 8
2400 416.67 4
4800 208.33 2
9600 104.17 1

Note: Some will refute this 1% rule as excessively strict, and cite anecdotal evidence of systems with worse timing that work just fine. After all, errors in asynchronous communication are not inevitable until the timing error reaches 5%. (Really, that number should immediately be cut to 2.5% so that sender and receiver get equal shares of the error budget.) The 1% rule just says that if your serial timing is within 1%, you won't have problems; if it's greater, you may.

Timing accuracy of 1% is pretty easy to achieve in a properly designed system, so strict or not, it's a reasonable guideline.

Keep Shopping
 
Checkout Now

If placed now, this order will ship (checking...).

Updating Cart