remove

02.01.2024

Byte-Sized Serial Communications: RS-232

With it’s relative simplicity and lower hardware requirements serial communications are widely used in the electronics industry and mostly has superseded parallel communications. One of the corner stones of serial communications is widely known as RS-232 (Recommended Standard 232) or formally but less popularly named EIA/TIA-232-F. Which is a set of standards for serial communications that is widely supported and accepted. Originally it was used to connect teletypewriters(TTYs) and video computer terminals to modems. Teletypewriters used to be the primary I/O device for gigantic mainframe computers that the humans would interact with before the advent of the self-contained personal computers we see today. The concept of TTYs is still used in the UNIX based operating systems and kernels such as Linux. It is one of the oldest serial interfaces[1].

Acronyms & Abbreviations

  • EIA : Energy Information Administration
  • TIA : Telecommunications Industry Association
  • RS : Recommended Standard
  • DTE : Data Terminal Equipment
  • DCE : Data Circuit-Terminating Equipment
  • OSI : Open Systems Interconnection
  • SPI : Serial Peripheral Interface
  • LSB : Least Significant Bit
  • MSB : Most Significant Bit
  • USB : Universal Serial Bus
  • UART : Universal Asynchronous Receiver/Transmitter
  • TTY : Teletypewriter

Preliminaries

Baud Rate

Baud rate is the number of bits sent every second, it is basically the communication frequency. Because RS-232 is an asynchronous serial connection sender and receiver must first agree on the baud rate for correct communication. All clocks have some error when timing though, and this discrepancy will add up in time if we don’t have a way of resynchronizing the clocks at arbitrary intervals. RS-232 uses every packets’s start bit to resynchronize the clock of the receiver.

LSB and MSB

0b01100001 Least significant bit is the bit that changes the value of the binary number the least and vice versa. Converting binary to base-10 we can see that changing the bits increases or decreases the value by powers of 2. And the power of two is the location of the bit (counting 0 to 7 from left to right) eg. while changing the most right bit (the 0th bit) would decrease or increase the value by 2^0 = 1 changing the most left bit (the 7th bit) would increase or decrease the value of the binary number by 2^7 = 128 . Thus while the 0th bit is the LSB the 7th bit is the MSB

DTE and DCE

Data Circuit-Terminating Equipment and Data Terminal Equipment terms are generalized names for nodes in the RS-232 connection. Looking at the connection between the DCE and DTE. We can put any RS-232 capable device in the role of a DCE and any computer in the role of DTE, there is no distinct difference between them as both can send and receive but by convention the DTE is considered the master and DCE the slave.

What is RS-232 and What it’s not

RS-232 is a set of standards for interfacing between electronics using an asynchronous serial communications connection that has been released in 1962. The asynchronous part of this definition means that we are not sending a separate clock signal to synchronize the incoming and outgoing bits (the advantages and disadvantages of this property of RS-232 will be mentioned in Using RS-232 . If you are rich enough to get your own copy of the EIA/TIA-232-F standard for 150 dollars or a pirate enough to steal one you might notice in the document that the standard explicitly defines the electrical connections and does not mention the packets or information that is being sent or received. That makes the RS-232 a physical layer when we think about in terms of the OSI model. In truth RS-232 is not interested in the data being sent at all, only about how it’s sent. You can change the packet size to increase or decrease the number of bits that are being sent behind every start bit. The standard states that a DB25 connector must be used but even then we usually do not need most of the pins and manufacturers also used DB9 connectors instead to cut costs and save space. The use of DB9 connector has been released as EIA/TIA-574 standard.

There is a lot of confusion and confidently incorrect information about RS-232 on the internet. From what I’ve personally seem some youtube “instructors” think that the serial port on an arduino is the same thing as an RS-232 port[MS1]! In actuality it is a UART communication that can be converted to USB communication with the onboard chip allowing you to read and write data from your usb port. When it comes to the RS-232 well here lays the difference: Every RS-232 based communication is a serial communication, but not all serial communication is RS-232 communication. Basically it is a subset of serial communications as it may seem obvious. I’ve even seen some articles claiming that USB adheres to the RS-232 standard[MS2]. That is just plain misinformation because the USB voltage levels mostly stand in the undefined region of the RS-232 standard. It is capable of only a single transmitter and receiver at a time [1]. Though hacky solutions such as connecting the same RS-232 line to different devices and setting them up so that they only answer if certain conditions are met is also possible.

Pinout of the DB9 Connector

Summary

  • It is a standard for interfacing devices with asynchronous serial interfaces
  • It is only concerned about the physical connection and electrical properties making it the physical layer in the OSI model
  • Conventionally used in older electronics and industrial equipment
  • Conventionally used to send and receive ASCII encoded characters
  • Does not define baudrate, message structure or error correction
  • Does define internal clock resync using start bit and voltage levels for the bits

Misinforming Sources

  • https://www.youtube.com/watch?v=tgk_SVN2TcA
  • https://www.totalphase.com/blog/2022/01/understanding-differences-between-uart-and-usb/

Properties of the RS-232 Interface

 

-25V ---------- -3V --- 0V --- +3V ---------- +25V     |::::: 0b1 ::::| UNDEFINED  |:::: 0b0 :::::| 

RS-232 was invented before the TTL logic levels and that might explain why it is not using the standard 0V and 5V signal levels. One other reason for the bigger band for the 0 and 1 bits is increased noise immunity. Because lets say for -5V and 5V signals we have a common mode tolerance of +-2V but this is for a sender and receiver that meets RS-232 standard. Which sadly most of the time devices that claim to be meeting the standard might actually NOT!

Oh my god they are lying for monetary gain!? How could that be!?

You might be asking, if they are not meeting the standard how does my devices work most of the time then? Well dear reader, because good engineers follow the Robustness Principle when designing devices (also known as Postel’s law after Jon Postel), it states that Be conservative in what you send, be liberal in what you accept Which that liberal-ness of accepting devices is sometimes abused to cut costs. But I digress.

Most modern systems do not operate on RS-232 voltage levels, which makes an interface IC a must (such as MAX232). This IC will turn our TTL voltage signals to RS-232 voltage levels and vice versa. The voltage levels that will be selected by the interface device changes from device to device and can be confirmed using their datasheet.

Using RS-232

RS-232 is an extremely practical and versatile physical layer. It’s simplicity is it’s shining star. Just using a MAX232 chip and some cables will let you read and write ASCII encoded data between devices that is relatively far away to each other in a high noise environment (given that you have chose a large enough voltage band) so it is a convenient option when we want a command line interface to control and monitor an equipment we are making.

For example lets say that we have crafted an industrial equipment but we want to add a debug interface into it that we can conveniently plug right into. Of course we didn’t leave the chip programmer in the equipment that would just be wasted bucks in our equipment. Instead we went with an RS-232 port on the PCB itself. Allowing for technicians, operators and us the OEM to run diagnostics and debug the equipment. We can connect the GPIO pins of our respective microcontroller to the TTL inputs and outputs on the MAX232, when we toggle our output GPIO pin, MAX232 will create the respective RS-232 compliant voltage levels for us. We choose a good band that won’t be enough to fry an unsuspecting computer but also big enough to not go whacko-mode besides an industrial inverter. By that note we also choose a safe baud rate for our equipment, too fast and data maybe corrupted if the connected terminal is placed farther away. Too slow and it will just make automated diagnostic tests take unnecessarily long. As such we note our voltage level and baud rate on the datasheet of our equipment like the good equipment manufacturer we are. We also explicitly make it clear that we are in fact using 8 bit packets that will be ASCII encoded, so the receiving computer will not feel like the british military intercepting german communications in WW2. We explain that there will be no parity bit or the like. Just 2 simple start and stop bits. After that we just need to program our microcontroller to read the GPIO input pin and write to output pin in correct times, but luckily for us there is most likely a library to do that automatically, but even if there isn’t it’s still not going to be a huge deal. After we hand our equipment to the consumer and count the cash our conscious rests easy as we gave them an equipment that is Right-to-Repair friendly. Well politics aside, the consumer now can check our datasheet, connect the USB to RS-232 interface to our device, setup their terminal according to the information on the datasheet and will voila, start receiving and transmitting vital information. All this for a little code and a cheap integrated circuit (STONKS)!

References

  • Louis E. Frenzel, Chapter Twenty-Five – RS-232, Editor(s): Louis E. Frenzel, Handbook of Serial Communications Interfaces, Newnes, 2016, Pages 109-112, ISBN 9780128006290, https://doi.org/10.1016/B978-0-12-800629-0.00025-5.
  • Dallas Semiconductor. (1998, September 3). Application Note 83 Fundamentals of RS–232 Serial Communications. Texas.
  • Texas Instruments. (2002, September). Interface Circuits for TIA/EIA-232-F. Texas.
  • File:RS-232 Pinouts for the DE-9 Connector.svg. (2022, December 16). Wikimedia Commons. https://commons.wikimedia.org/w/index.php?title=File:RS-232_Pinouts_for_the_DE-9_Connector.svg