Understanding RS232 Serial Communication Message Formats
There are many settings and configurations to consider when using RS232 devices. This article breaks down this communication standard and explains how to send messages from one device to another.
RS232 / Serial Communication
In the early days of computers talking and sharing data, serial communication was used as the primary topology and RS232 was the protocol. Today, RS232 can be found across a number of industrial automation devices. There are many components to an RS232 message, and they need to be assembled correctly in order for the receiving device to be able to decode the message. In the following article, I will break down these components and explain how to send messages from one device to another.
Figure 1. Serial communications connectors, male and female. Image used courtesy of Marsel
What Is a Serial Message?
A serial message is a string of bytes sent from a transmitting device to a receiving device. There are two modes, asynchronous and synchronous. Asynchronous messaging is not synchronous with a clock pulse whereas synchronous messages are. Each device will have a transmit, receive, and ground terminal or a data terminal and ground terminal. When connecting two devices together, the transmitting terminal on one device will be connected to the receiving terminal on the other device. With RS323, there are only two devices connected together, so there is no need for addressing or nodes.
Figure 2. RS232 is still used by industrial devices today, including servers, controllers, robots, etc. Image used courtesy of Nay
What Is RS232?
The protocol was originally used for communication between computers and peripheral devices such as a mouse, printer, or modem. It is hard to find a computer today with a serial port, but the industrial automation world still uses the protocol today. The standard uses between -3 VDC and -15 VDC to determine a “1” and between 3 VDC and 15 VDC as a “0”. With this logic, a binary message can be sent from one device to another. This waveform signal must share a common ground between two devices which makes the signal susceptible to electronic noise from neighboring equipment.
RS232 Protocol Message Formats
There are many settings and configurations when using RS232 devices. Sometimes one device in the network has fixed settings from the factory, and it is then important to properly adjust the settings in the host control device (such as the COM port settings in the device manager of your PC).
The baud rate is simply the data transfer rate in bits per second. Because serial communication uses binary data to send messages or numeric values, the speed at which this data is sent needs to match between devices. If you have one device communicating faster than the other, some messages will be missed.
To ensure the message is correct, the RS232 standard has error checking in the message. The parity bit is a simple way to check for a valid message. The parity bit is a single bit that is appended to the binary message. This error checking has two modes “even” or “odd” which means the parity bit will be either “1” or “0” to make the number of “1” bits rather even or odd. If a message is sent with an even parity bit, then the number of “1” bits should be even. If they are not, then there is an error in the message.
Half Duplex vs Full Duplex
The RS232 protocol uses a full-duplex mode which refers to how messages can be sent or received. With full-duplex, there are different wires for transmitting and receiving so messages can be sent while messages are being received. With half-duplex (simplex), the transmit and receive are on the same wires so the transactions need to be gated.
This tells the receiving device that a message is going to start. We use a start bit because by default RS232 protocol is asynchronous. To signal the receiving device with the start bit, we simply set the line voltage above 5 VDC since the idle state is 0 VDC.
The data of the message comes after the start bit. The size of the data package can range depending on the size of your message, but each character or symbol will be broken up into an 8-bit binary number. If we wanted to send the message “Hello World”, each character would be eight 1s or 0s. For example, to send the letter H, we first convert the character into a number, which is 72, using the ASCII table and then convert it to binary: 0100 1000.
Cyclic Redundancy Check (CRC) or Checksum
A CRC or checksum is a mathematical way of checking the integrity of the message. While a CRC is very different from a checksum, both are used in RS232 devices for validating messages. Some devices will use complex formulas and others will simply sum the number of bytes and divide by 255 with the remainder being the CRC.
The CRC number is then appended to the message for decoding. The receiving unit will then decode the CRC to determine if the message is valid before accepting the message. Typically, the sending and receiving devices will calculate the CRC within the firmware before the message is sent.
HEX to ASCII
Data can be represented in many ways, and RS232 makes use of these ways to send messages. Most RS232 messages will be a command and then a value. To send the command and value, we need to convert it into binary. We do this by first converting the text into numbers. ASCII tables are used to convert symbols, capital letters, lower case letters, and numbers into other data types such as HEX, Oct, and numbers. A HEX number consists of numbers 0 - 9 and letters A - F and uses 8 bits of binary data.
Figure 3 shows an example message with even parity and simple CRC message “Hello”.
Figure 3. An example message with even parity and a simple CRC message, “Hello”.
RS232 Message Types
The message you send to your device will depend on what the device does. For each project, you will need to consult your device manual and review the commands that the device will accept and how to format the message. If, for example, you have a liquid dispense device, you might send dosing configurations to the controller over RS232.
Some small servo motors are controlled over RS232, so a common command might be to move to a position or reference the current position as zero. Typically, your message format will be a string command like “move” or “Ref” followed by a numerical value. If the message is received without errors, an “ack” or acknowledgment message is sometimes sent in response.
Serial communication in the RS232 format is one of the oldest forms of computer networking, but its simplicity and reliability keeps it in practice today when critical equipment must be kept running for decades. It’s important to understand the message formats in order to troubleshoot errors.
Baud rate is not ‘simply’ bit rate. https://techdifferences.com/difference-between-bit-rate-and-baud-rate.html