Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

Data transmission between computers or between a computer and a terminal can be achieved using serial communication and parallel communication. Due to the advantages of serial communication, such as fewer lines and lower costs, especially in long-distance transmission, it has been widely adopted to avoid inconsistencies in multiple line characteristics.

In serial communication, both parties must use a standard interface to facilitate communication between different devices. The RS-232-C interface (also known as EIA RS-232-C) is currently the most commonly used serial communication interface.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!1. Basic Knowledge of RS232

RS-232-C is a serial physical interface standard established by the Electronic Industry Association (EIA) in the United States.

RS is an abbreviation for “Recommended Standard,” 232 is the identification number, and C indicates the revision number, representing the latest modification of RS232 (1969).

Prior to this, there were RS232B and RS232A. It was established in 1970 by the EIA in collaboration with Bell Systems, modem manufacturers, and computer terminal manufacturers as a standard for serial communication. Its full name is “Technical Standard for Serial Binary Data Exchange Interface between Data Terminal Equipment (DTE) and Data Communication Equipment (DCE).”

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

1. Electrical Characteristics

EIA-RS-232C specifies the electrical characteristics, logic levels, and functions of various signal lines.

On TxD and RxD: Logic 1 (MARK) = -3V to -15V, Logic 0 (SPACE) = +3V to +15V. On control lines such as RTS, CTS, DSR, DTR, and DCD: Signal active (on, ON state, positive voltage) = +3V to +15V; Signal inactive (off, OFF state, negative voltage) = -3V to -15V.

Depending on the power supply of the device, levels such as +-5, +-10, +-12, and +-15 are possible.

2. Mechanical Characteristics of Connectors

Since RS-232C does not define the physical characteristics of connectors, various types of connectors such as DB-25, DB-15, and DB-9 have emerged, each with different pin definitions.

Recently, the 8-pin RJ-45 connector has become increasingly common, although its pin assignments vary widely. The EIA/TIA 561 standard specifies a method for pin assignments, but the widely used Yost Serial Device Wiring Standard, invented by Dave Yost, and many other devices do not adopt any of the aforementioned wiring standards. The table below lists the signals and pin assignments commonly used in RS-232:

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

The signal annotations are from the perspective of the DTE device, where TD, DTR, and RTS signals are generated by the DTE, while RD, DSR, CTS, DCD, and RI signals are generated by the DCE.

The RS-232 port on a PC is a 9-pin socket. Some devices connected to the PC’s RS-232 interface do not use the transmission control signals from each other and only require three interface lines: “Transmit Data TXD,” “Receive Data RXD,” and “Signal Ground GND.”

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

The bidirectional interface can be made with only three wires because all signals in RS-232 share a common ground. The unbalanced circuit makes RS-232 very susceptible to voltage offset between the two devices.

For the rise and fall times of signals, RS-232 also has relatively poor control capabilities, making crosstalk issues likely. RS-232 is recommended for communication over short distances (within 15m); due to the unbalanced circuit, RS-232 interface cables are usually not made of twisted pairs.

3. Transmission Cable

The RS-232-C standard specifies data transmission rates of 50, 75, 100, 150, 300, 600, 1200, 2400, 4800, 9600, 19200 baud, with drivers allowing a capacitive load of 2500pF, and communication distance will be limited by this capacitance.

For example, using a communication cable with 150pF/m capacitance, the maximum communication distance is 15m; if the capacitance per meter of cable is reduced, the communication distance can be increased. Another reason for the short transmission distance is that RS-232 uses single-ended signal transmission, which has common ground noise and cannot suppress common-mode interference, so it is generally used for communication within 20m.

According to the RS-232C standard, under the condition that the pulse distortion is less than 4%, the length of the transmission cable should be 50 feet. In practice, this 4% pulse distortion is very conservative, and in actual applications, about 99% of users operate within a pulse distortion range of 10-20%, so the maximum distance in practical use will far exceed 50 feet.

DEC in the United States once allowed a pulse distortion of 10% and obtained the following experimental results. Cable 1 is a shielded cable, model DECP.NO.9107723, containing three pairs of twisted wires, each pair made of 22# AWG, covered with a shielding mesh. Cable 2 is an unshielded cable, model DECP.NO.9105856-04, which is a 22#AWG four-core cable.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

4. Link Layer

In the RS-232 standard, characters are transmitted one after another as a series of bits. The most commonly used encoding format is the asynchronous start-stop format, which uses a start bit followed by 7 or 8 data bits, possibly a parity bit, followed by two stop bits. Therefore, sending a character requires 10 bits, which results in a good effect of making the overall transmission rate, the rate of sending signals, divided by 10.

Serial communication requires multiple settings in software configuration, the most common settings include baud rate, parity, and stop bits. The baud rate refers to the rate at which bits are transmitted from one device to another, i.e., how many bits per second (bit/s).

Typical baud rates are 300, 1200, 2400, 9600, 19200, etc. Generally, both devices at the communication ends must be set to the same baud rate, but some devices can also be set to automatically detect the baud rate.

Parity is used to verify the correctness of the data. Parity is generally not used, but if used, it can be either odd or even parity.

Parity works by modifying each transmitted byte (or limiting the bytes sent). If no parity is used, the data will not be changed.

In even parity, the parity bit will be set to 1 or 0 (usually the highest or lowest bit) so that the number of “1” bits in all transmitted bits (including the bits of the character and the parity bit) is even; in odd parity, the number of “1” bits in all transmitted bits (including the bits of the character and the parity bit) is odd.

Parity can be used by the receiving party to check whether transmission errors have occurred—if the number of “1” bits in a byte has changed, then that byte must have encountered an error during transmission. If the parity is correct, then either no error has occurred or an even number of errors has occurred.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

Stop bits are sent after each byte transmission to help the receiving signal hardware resynchronize.

In serial communication software settings, D/P/S is the conventional symbol representation. 8/N/1 (very common) indicates 8-bit data, no parity, and 1 stop bit. Data bits can be set to 7, 8, or 9, parity bits can be set to none (N), odd (O), or even (E), and parity bits can use bits from the data, so 8/E/1 indicates a total of 8 data bits, with one bit used for the parity bit. Stop bits can be 1, 1.5, or 2 bits (1.5 is used for teletypes at 60wpm baud rate).

5. Transmission Control

When handshake signals or data integrity checks are needed, other settings must be established. Common combinations include RTS/CTS, DTR/DSR, or XON/XOFF (in practice, special characters are inserted into the data stream instead of using connector pins).

The receiving party sends XON/XOFF signals to the sending party to control when to send data; these signals are opposite to the direction of data transmission. The XON signal tells the sender that the receiver is ready to accept more data, while the XOFF signal tells the sender to stop sending data until the receiver is ready again. XON/XOFF is generally not recommended; it is recommended to use RTS/CTS flow control instead.

XON/XOFF is an in-band method that works between terminals, but both ends must support this protocol, and there may be confusion during sudden starts.

XON/XOFF can work on a 3-wire interface. RTS/CTS was originally designed for half-duplex communication between teletypes and modems, where only one side can send data at a time. The terminal must send a request to send signal and then wait for the modem to respond to clear the send signal. Although RTS/CTS achieves handshaking through hardware, it has its own advantages.

6. Limitations of the RS-232 Standard

Over the years, improvements in RS-232 devices and communication technology have greatly increased the communication distance of RS-232.

Due to the early emergence of the RS-232 interface standard, it inevitably has some shortcomings, mainly four points:

(1) The signal level values of the interface are relatively high, which can easily damage the interface circuit chips, and because they are not compatible with TTL levels, level conversion circuits are required to connect with TTL circuits.

(2) The transmission rate is relatively low; in asynchronous transmission, the baud rate is 20Kbps. Now, with the use of new UART chips like 16C550, baud rates can reach 115.2Kbps.

(3) The interface uses one signal line and one signal return line to form a common ground transmission form, which is prone to common-mode interference, making it weak against noise interference.

(4) The transmission distance is limited, with a maximum standard transmission distance of 50 meters, but in practice, it can only be used for about 15 meters.

2. Basic Knowledge of RS485

In response to the limitations of the RS-232 serial port standard, the RS-422 and RS-485 interface standards were proposed.

RS-485/422 uses balanced transmission and differential reception to achieve communication: the sending end converts the TTL level signal of the serial port into differential signals A and B, which are output, and after transmission through the cable, the differential signals are restored to TTL level signals at the receiving end.

Since the transmission line usually uses twisted pairs and is a differential transmission, it has a strong ability to resist common-mode interference, and the bus transceiver sensitivity is very high, capable of detecting voltages as low as 200mV. Therefore, transmission signals can be restored even over kilometers.

1. Electrical Characteristics of RS-485Drivers can output a common-mode voltage of ±7V; the input resistance of the receiver RIN ≥ 12kΩ; input capacitance ≤ 50pF.

With 32 nodes and a terminal resistance of 120Ω, the driver can still output a voltage of at least 1.5V (the size of the terminal resistance is related to the parameters of the twisted pair used).

On the sending end: Logic “1” is represented by a voltage difference of + (2 to 6)V between the two wires; Logic “0” is represented by a voltage difference of – (2 to 6)V. The input sensitivity of the receiver is 200mV (i.e., (V+) – (V-) ≥ 0.2V indicates signal “0”; (V+) – (V-) ≤ -0.2V indicates signal “1”).

2. Transmission Rate and Distance The maximum data transmission rate of RS-485 is 10Mbps, with a maximum communication distance of about 1219M. The transmission rate is inversely proportional to the transmission distance; at a transmission rate of 10Kb/S, the maximum communication distance can be achieved.

However, since RS-485 often needs to communicate with the RS-232 port of a PC, the actual maximum is generally 115.2Kbps. Also, due to the high rate, the transmission distance of RS-485 decreases, so it is often around or below 9600bps.

3. Network Topology

The RS-485 interface uses a combination of balanced drivers and differential receivers, enhancing the ability to resist common-mode interference, thus having good noise immunity. RS-485 operates in half-duplex mode and supports multipoint data communication.

The RS-485 bus network topology generally adopts a terminal-matched bus structure, connecting various nodes in series along a single bus, and does not support ring or star networks. If a star structure is needed, RS-485 repeaters or hubs must be used.

The RS-485/422 bus generally supports a maximum of 32 nodes; if special 485 chips are used, it can reach 128 or 256 nodes, with a maximum of 400 nodes supported.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

4. Connectors The international standard for RS-485 does not specify a standard for RS-485 interface connectors, so terminal blocks or DB-9, DB-25, etc., connectors can be used.

3. Basic Knowledge of RS422

The electrical performance of RS-422 is similar to that of RS-485. The main differences are:

(1) RS-485 has 2 signal lines: A and B for both sending and receiving. Since RS-485 shares two lines for receiving and sending, it cannot receive and send simultaneously (half-duplex).

(2) RS-422 has 4 signal lines: two for sending (Y, Z) and two for receiving (A, B). Since RS-422 separates receiving and sending, it can receive and send simultaneously (full-duplex).

(3) RS-422 supports multi-machine communication; shorting Y-A as RS-485’s A and Z-B as RS-485’s B can easily convert it to RS-485.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

Many people often mistakenly believe that the RS-422 serial interface is the full-duplex version of the RS-485 serial interface; in fact, there are many differences in electrical characteristics, and the common-mode voltage range and receiver input resistance differ, making these two standards suitable for different application fields.

Drivers for the RS-485 serial interface can be used in RS-422 serial interface applications because the RS-485 serial interface meets all performance parameters of the RS-422 serial interface, but the reverse is not true.

For RS-485 serial interface drivers, the output range of common-mode voltage is between -7V and +12V; for RS-422 serial interface drivers, this performance index is only ±7V. The minimum input resistance of RS-422 serial interface receivers is 4KΩ; while the minimum input resistance of RS-485 serial interface receivers is 12KΩ.

4. Basic Knowledge of Serial Ports

The serial port is a very common communication protocol for device communication on computers (do not confuse it with Universal Serial Bus or USB).

Most computers contain two RS232-based serial ports. The serial port is also a common communication protocol for instrumentation devices; many GPIB-compatible devices also have RS-232 ports. At the same time, the serial communication protocol can also be used to obtain data from remote acquisition devices.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

The concept of serial communication is very simple; the serial port sends and receives bytes bit by bit.

Although it is slower than parallel communication, which sends data by byte, the serial port can send data on one line while receiving data on another line. It is simple and can achieve long-distance communication. For example, when IEEE488 defines parallel communication, it states that the total length of device lines must not exceed 20 meters, and the length between any two devices must not exceed 2 meters; for serial ports, the length can reach 1200 meters.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

Typically, the serial port is used for the transmission of ASCII code characters.

Communication is completed using 3 wires: (1) ground, (2) transmit, (3) receive.

Since serial communication is asynchronous, the port can send data on one line while receiving data on another line. Other lines are used for handshaking but are not mandatory. The most important parameters for serial communication are baud rate, data bits, stop bits, and parity. For two ports communicating, these parameters must match:

1. Baud Rate

This is a parameter that measures communication speed. It indicates the number of bits transmitted per second. For example, 300 baud means sending 300 bits per second. When we refer to clock cycles, we are referring to the baud rate; for example, if the protocol requires a baud rate of 4800, then the clock is 4800Hz. This means that the sampling rate on the data line for serial communication is 4800Hz. Typically, the baud rates for telephone lines are 14400, 28800, and 36600. Baud rates can be much higher than these values, but baud rate is inversely proportional to distance. High baud rates are often used for communication between instruments placed very close together, a typical example being GPIB device communication.

2. Data Bits

This is a parameter that measures the actual data bits in communication. When a computer sends a packet of information, the actual data will not be 8 bits; standard values are 5, 7, and 8 bits. How it is set depends on the information you want to transmit. For example, standard ASCII code is 0-127 (7 bits). Extended ASCII code is 0-255 (8 bits). If the data uses simple text (standard ASCII code), then each data packet uses 7 bits of data. Each packet refers to a byte, including start/stop bits, data bits, and parity bits. Since the actual data bits depend on the selected communication protocol, the term “packet” refers to any communication situation.

3. Stop Bits

These indicate the last bit of a single packet. Typical values are 1, 1.5, and 2 bits. Since data is timed on the transmission line, and each device has its own clock, there may be slight desynchronization between the two devices during communication. Therefore, stop bits not only indicate the end of transmission but also provide an opportunity for the computer to correct clock synchronization. The more bits suitable for stop bits, the greater the tolerance for desynchronization between different clocks, but the data transmission rate also becomes slower.

4. Parity Bit

This is a simple error-checking method in serial communication. There are four types of error-checking methods: even, odd, high, and low. Of course, it is also possible to have no parity bit. For even and odd parity, the serial port will set a parity bit (one bit after the data bits) to ensure that the transmitted data has an even or odd number of logical high bits.

For example, if the data is 011, then for even parity, the parity bit is 0, ensuring that the number of logical high bits is even. If it is odd parity, the parity bit is 1, resulting in three logical high bits. High and low bits do not actually check the data; they simply set the logical high or low for checking. This allows the receiving device to know the state of a bit and provides an opportunity to determine whether noise has interfered with communication or whether the transmitted and received data are out of sync.

5. Basic Knowledge of Handshaking

The RS-232 communication method allows for simple connections with three lines: Tx, Rx, and ground. However, for data transmission, both parties must use the same baud rate for timing. Although this method is sufficient for most applications, it is limited in cases of receiver overload. In such cases, the handshaking function of the serial port is required. In this section, we discuss the three most common forms of RS-232 handshaking: software handshaking, hardware handshaking, and Xmodem.1. Software Handshaking

The first handshaking we discuss is software handshaking. It is usually used when the actual data is control characters, similar to how GPIB uses command strings.

The necessary lines are still three:Tx, Rx, and ground, because control characters are indistinguishable from ordinary characters on the transmission line. The function SetXModem allows users to enable or disable the use of two control characters, XON and OXFF. These characters are sent by the receiving party during communication to pause the sending party.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

For example: Suppose the sender is sending data at a high baud rate. During transmission, the receiver finds that the CPU is busy with other tasks, and the input buffer is full. To temporarily stop transmission, the receiver sends XOFF, typically the value is decimal 19, which is hexadecimal 13, until the input buffer is empty. Once the receiver is ready to receive again, it sends XON, typically the value is decimal 17, which is hexadecimal 11, to continue communication.

When the input buffer is half full, LabWindows sends XOFF. Additionally, if the XOFF transmission is interrupted, LabWindows will send XOFF when the buffer reaches 75% and 90%. Clearly, the sender must follow this rule to ensure transmission continues.

2. Hardware Handshaking

The second method is to use hardware line handshaking. Along with Tx and Rx lines, RTS/CTS and DTR/DSR work together, one as output and the other as input.

The first pair of lines is RTS (Request to Send) and CTS (Clear to Send).

When the receiving party is ready to receive data, it sets the RTS line high to indicate readiness; if the sender is also ready, it sets the CTS high to indicate it is about to send data.

The second pair of lines is DTR (Data Terminal Ready) and DSR (Data Set Ready).

These are mainly used for modem communication, indicating the status of the serial port and modem communication. For example, when the modem is ready to receive data from the PC, it sets the DTR line high, indicating that the connection to the telephone line has been established. Reading the DSR line high indicates that the PC is starting to send data. A simple rule is that DTR/DSR indicates system communication readiness, while RTS/CTS is used for the transmission of individual data packets.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

In LabWindows, the function SetCTSMode enables or disables hardware handshaking. If CTS mode is enabled, LabWindows uses the following rules: when the PC sends data, the RS-232 library must detect the CTS line high before sending data. When the PC receives data: if the port is open and the input queue has empty space for receiving data, the library function sets RTS and DTR high. If the input queue is 90% full, the library function sets RTS low but keeps DTR high. If the port queue is nearly empty, the library function sets RTS high but keeps DTR high. If the port is closed, the library function sets RTS and DTR low.

3. XModem Handshaking

The last handshaking we discuss is the XModem file transfer protocol. This protocol is very common in modem communication. Although it is usually used in modem communication, the XModem protocol can also be used directly in communication between other devices that follow this protocol.

In LabWindows, the actual XModem application is hidden from the user.

As long as the PC and other devices use the XModem protocol, the LabWindows XModem functions are used for file transfer. The functions are XModemConfig, XModemSend, and XModemReceive.

Fundamental Knowledge of RS232/485 Serial Communication Every Electrical Engineer Must Know!

XModem uses a protocol with the following parameters: start_of_data, end_of_data, neg_ack, wait_delay, start_delay, max_tries, and packet_size.

These parameters need to be agreed upon by both communicating parties; the standard XModem has a standard definition; however, it can be modified through the XModemConfig function to meet specific needs. The usage of these parameters is determined by the character neg_ack sent by the receiving party. This notifies the sender that it is ready to receive data. It begins attempting to send, with a timeout parameter start_delay; when the timeout attempts exceed max_ties, or the sender receives start_of_data from the receiver, the sender stops attempting.

If the sender receives start_of_data, the receiver will read the subsequent information data packet. The 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 for further error checking.

After reading the data, the receiver will call wait_delay and then send a response to the sender. If the sender does not receive a response, it will resend the data packet until a response is received or the maximum number of retries max_tries is exceeded.

If no response is received, the sender notifies the user that the data transfer has failed.

Since data must be sent in packets of packet_size bytes, when the last data packet is sent, if there is not enough data to fill a packet, it will be padded with ASCII NULL (0) bytes. This results in the received data being larger than the original data.

In the case of XModem, do not use XON/XOFF, as the number of packets sent by the XModem sender may exceed the values of the XON/OFF control characters, leading to communication failures.

Disclaimer: This article is reproduced from the internet, and the copyright belongs to the original author. If there are any copyright issues, please contact us for deletion. Thank you!

Leave a Comment