Understanding RS232, RS485, RJ45, and Modbus Protocols

Consider a DC power supply with an output socket interface that has three pins: positive, negative, and ground. Accordingly, the load’s plug should also have three pins corresponding to the power side to correctly obtain power supply.

Note that there are three essential conditions that must be met:

The first is that the shape, size, pin diameter, and length of the plug and socket pins must correspond, otherwise the connection cannot be completed. This specifies the physical structure and pin definitions of the plug combination.

The second is that the output voltage value of the power supply must meet the requirement of the load side; otherwise, the electrical parameters cannot be satisfied. This determines the voltage specifications of the plug combination.

The third is that the output impedance of the power supply must match the input impedance of the load; otherwise, proper power supply cannot be achieved. This determines the working nature of the power supply.

These three points are essentially the normative protocols of the power supply plug combination at the physical level.

Now let’s look at communication interfaces. In the ISO/OSI model related to computer information exchange, the physical layer is the lowest layer (Layer 1), which specifies the mechanical appearance of the interface, pin definitions, interface levels, and byte formats.

The byte format here refers to how many data bits are in a byte, how many start/stop bits there are, and how many parity bits are there. Generally, a byte has 8 data bits, 1 start bit (stop bit), and 1 parity bit. Note: The start and stop bits can be combined.

Next, let’s look at the working mode of communication interfaces and networks.

When we make a phone call with our mobile phone, we find that both parties can talk and listen at the same time; this is called full duplex (two-way working mode); if one party cannot listen while speaking, and cannot speak while listening, but both parties can talk and listen, such as in a walkie-talkie communication style, this is called half duplex.

Understanding RS232, RS485, RJ45, and Modbus Protocols

(Just to clarify: These images are excerpts from a MODBUS communication PPT I made at ABB, which is quite old, but the basic content is correct. These images are for reference.)

RS422 and RS232 interfaces are full duplex interfaces, while RS485 is a half duplex interface.

For half duplex interfaces, there must be a communication initiator, so the RS485 interface and network must have a master station and several slave stations, and the number of slave stations is also specified. Generally, the number of slave stations is 32.

The relationship between the RS485 master station and slave stations seems to be just a difference in communication working mode, but in essence, it is a reasonable distribution of control rights over the communication bus among the parties.

Next, let’s look at bus connection issues.

We can take the power supply as an example. We can draw a main line from the power supply and then parallel several branches to several loads. As long as the power supply’s power requirements are met, this is evidently feasible.

If we use the same method to draw the RS485 communication line, is it feasible? The answer is no. We must first draw a line from the communication master station to the first communication slave station, then draw a second line from the first slave station to the second slave station, and so on until the last slave station. At the end of the communication line, a terminating resistor must be added. If any point on this communication line experiences a disconnection, communication on the subsequent links will also be interrupted. This wiring method is vividly referred to as the daisy chain connection method, or chain connection method, while the power supply wiring method is referred to as star connection method.

Understanding RS232, RS485, RJ45, and Modbus Protocols

Understanding RS232, RS485, RJ45, and Modbus Protocols

We find that from an electrical wiring perspective, the link is parallel. But from a communication perspective, the link is daisy-chained, which is an orderly connection one after another.

Now we can summarize:

The wiring method of the RS485 bus network must be a daisy chain wiring method, and it belongs to half duplex communication mode; RS232 is a point-to-point wiring method, belonging to full duplex communication. Whether it is the RS232 interface or the RS485 interface, they must comply with the communication protocols of the physical layer.

Next, let’s look at the MODBUS-RTU communication protocol:

With the physical layer communication interface, can communication be established? The answer is no. The physical layer communication interface only provides the conditions for communication. However, if both parties cannot understand what the other is saying, or if the speaking methods and grammatical structures of both parties do not match, clearly, communication cannot occur.

In the OSI model, the data link layer is above the physical layer. The MODBUS-RTU protocol is a data link layer protocol, as long as both parties adopt the MODBUS-RTU protocol, it ensures that the communication language is a statement format that both parties can understand.

Note the term “statement” here. The physical layer defines bytes, which is equivalent to words in language, while the data link layer organizes bytes into statements, that is, frames. The frame specifies the grammatical structure of the statements used by both parties in communication.

MODBUS is also a master-slave structure. Similar to the bus control of the physical layer, the master-slave relationship here specifies the control rights over the communication bus. The master station first issues a command, occupying the bus; then vacates the bus for the slave station to write the response code; after the slave station completes, it returns the bus to the master station.

Now let’s look at the frame structure defined by ISO’s HDLC, which is the grammatical structure of communication statements, as follows:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Under the MODBUS communication protocol, different command function codes have different frame structures. For the read register command, the MODBUS master station frame structure is: a 2-byte address code, a 1-byte function code, a 2-byte data address code, and a 2-byte CRC check code; the MODBUS slave response frame structure is: a 2-byte function code, a 1-byte total number of response bytes, N bytes of response data, and a 2-byte CRC check code.

Although the physical layer protocol and data link layer protocol are different, the execution of the data link layer protocol must be based on the physical layer connections of both parties meeting the requirements, and being able to achieve unobstructed information exchange.

This rule must be fully and thoroughly executed in the seven-layer protocol of the ISO/OSI model. In the ISO/OSI model, the lower-level protocols of both parties must establish a transparent, fault-free connection and information exchange relationship for the upper-level protocols. In other words, the hierarchical relationship between layers must be absolute.

Above the data link layer, it is the network layer. Its task is to form the information exchange network of the field bus.

The functions of the network layer include: packaging communication frames into data packets, and then sending the data packets to the other party.

Since the network structures of both parties may differ, the same type of network requires a bridge for connection, while different types of networks require a gateway for connection.

There may be multiple channels between networks. Data packets have multiple paths to choose from when sent. The component responsible for selecting the path is called a router. The router not only determines the actual data exchange network path, but can also construct virtual network paths, and decide the sending order of data packets. Therefore, the router is the most complex and critical equipment in the network layer.

In the OSI model, the physical layer + data link layer + network layer is collectively referred to as the field bus, with the communication interface being the 8-pin RJ45 crystal head. It is evident that RJ45 is completely different from RS232/RS485/RS422.

Data packets at the network layer are combinations of data frames. In simple terms, data packets are a short article, or a page of data combination unit to be transmitted.

The routing issues and receiving combination issues of data packets sent by the network layer are shown in the figure below:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Understanding RS232, RS485, RJ45, and Modbus Protocols

We see that during communication, the network layer first determines the routing path through the router, and then sends the packet to the other party. After the other party receives the packet, it combines the packets in order and unpacks them into the actual document.

It should be noted that: with the router in the network layer, the network layer supports star network structures.

Now let’s focus on the ISO/OSI seven-layer model, as follows:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Since the questioner did not mention higher-level protocols, we will ignore them. However, it is necessary to clarify that: from the network layer upwards, the information units sent between layers are already complete messages. The OSI model also specifies the grammatical structure of messages, but we will ignore it due to space constraints.

It is worth noting that the communication interfaces of RS232/RS485/RS422 and their definitions are very clear. This includes the voltage levels of the pins, the functional definitions of the pins, and the data flow timing relationships during information transmission and reception, all of which must be accurate and strict; otherwise, information exchange cannot be executed.

When a PLC exchanges information with a certain electric meter, and these electric meters comply with the RS485/MODBUS-RTU communication specifications. What do we need to do?

First, we wire according to the daisy chain structure communication link requirements, connecting the PLC’s communication interface with N electric meter interfaces. The last electric meter must be equipped with a 100-ohm terminating resistor.

Second, we determine the addresses of these N electric meters according to the principle of incrementing addresses, for example, 01H, 02H, 1FH, etc. Here, H indicates hexadecimal, and 1F means 16 + 15 = 31.

Third, we set the communication rate specified by the electric meter in the PLC programming software.

Fourth, we set the MODBUS communication code according to the data area address code of the electric meter in the PLC programming software, as well as the cyclic relationship of each slave station.

Note that the MODBUS communication code here meets the requirements of the IEC 61131-3 programming module of the PLC, and general PLC ladder diagrams do not have this function. Ladder diagrams meet the requirements of IEC 61131-1, but do not meet the requirements of IEC 61131-3.

Fifth, a dedicated data area is opened in the PLC’s memory, where the information read from the electric meter is stored and processed, so that the higher-level master station can read the information. This data area is called the data point table, and is sometimes simply referred to as the communication protocol.

Finally, of course, there is the power-on test. There is a lot of content involved, and we will not introduce it further due to space constraints.

Let’s look at an example of reading data on an RS485 network using MODBUS-RTU:

An electric meter has an address of 01H. At memory position 2000 of the electric meter, there are six data points such as three-phase current and three-phase voltage, each data occupying two bytes, totaling 12 bytes.

The communication rate of this electric meter is 9600bps. What does this mean? bps indicates one 0/1, which is a bit, and this means that 9600 bits can be sent per second on this bus. We already know that a byte has 8 data bits, 1 start bit, and 1 parity bit, totaling 10 bits or 10 bits, so if the communication rate of the electric meter is 9600bps, then in one second, we can send: 9600/10=960 bytes.

We also know that in the frame structure of the master station’s read data (downlink frame), there is 1 byte of address, 1 byte of function code, 2 bytes of memory address, 2 bytes of data quantity, and 2 bytes of CRC check code, totaling 8 bytes, so the time occupied by the master station sending the read data MODBUS communication frame is: 8X10/9600=8.33 milliseconds.

In this example, we know that the MODBUS-RTU read data command is 0X03H, which is the 03 command. Note the notation here: 0X is the prefix, the middle 03 is the command, and the last H indicates hexadecimal.

The specific communication frame is: 01 03 07 D0 00 06 C5 45, where 0X01H is the address, 0X03H is the command, 0X07D0H is the memory address 2000, 0X0006H indicates reading 6 continuous words, which are the current and voltage parameters in memory, and 0XC545H is the CRC check code for 01 03 07 D0 00 06.

Thus, the response frame of the electric meter (uplink frame) has the frame structure of: 1 byte of address, 1 byte of function code, 1 byte of data area byte count, 12 bytes of data, and 2 bytes of CRC check code, totaling 17 bytes, with a time occupied of: 17X10/9600=17.7 milliseconds.

The specific response communication frame of the instrument is: 01 03 0C 00 64 0064 0064 00 DC 00 DC 00 DC D6 F5, where 0X01H and 0X03H have the same meaning as above, 0X0CH indicates there are 12 bytes in the upload data area, 0X0064H indicates that phase A current is 100A, the following two groups are phase B and phase C currents, both 100A, 0X00DCH indicates that phase A voltage is 220V, the following two groups are phase B and phase C voltages, both 220V, and finally, 0XD6F5H is the CRC check code.

From the master station initiating the downlink communication frame, waiting 10 milliseconds for the slave station to respond, and then receiving the uplink communication frame returned by the slave station, the total duration is:

Understanding RS232, RS485, RJ45, and Modbus Protocols

If there are 31 identical instruments waiting for the master station to access one by one, then the total duration from the master station starting to access the first instrument to the last response completion is:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Here, the 1.12 seconds is the reading data cycle for these 31 instruments at a communication rate of 9600bps, and ignoring the waiting time for the master station to send the downlink communication frame again, the actual time will be slightly longer.

Believe that by this point, everyone should have a deeper understanding of the communication frame under MODBUS-RTU.

Reminder: One word has two bytes. Generally, a byte can only express 8 switch quantities. But for analog quantities, it must be expressed with a word. For example, a current of 1250A is 04E2H in hexadecimal, and requires 2 bytes to express completely. Therefore, in various electric meters, analog quantities are expressed using words.

Below are some commonly used function codes of MODBUS, which are command codes:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Here are examples of downlink and uplink communication frames when the PLC reads the data point table of the ASCO double-throw switch controller:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Let me explain a few related issues:

1) Some field buses use tokens to solve the control rights issue of the bus.

It is easy to think that if a slave station has an urgent matter that requires the master station’s service, but MODBUS stipulates polling rules, waiting for its turn may be too late. Therefore, many field buses have invented a special thing called a token. A token is short, only one byte, and can be quickly passed on the bus. The token is passed among stations; whoever gets the token becomes the master station and can publish information. If the current station has nothing to publish, it passes the token to the next station, thus solving the bus occupation problem.

2) When a link experiences a disconnection, to avoid communication interruption, dual master station measures can be adopted. Dual master stations (two master station RS485 interfaces of the PLC) are connected by handshake lines, usually with the main RS485 activated while the auxiliary RS485 is floating. The floating RS485, although connected to the bus, is in a high impedance state equivalent to complete disconnection. When disconnection occurs, the slave station confirms and immediately activates communication, connecting communication from both ends of the link.

Sometimes, circular communication measures are also taken. Due to space constraints, I will not introduce it.

3) MODBUS can work at the network layer, at which point the protocol becomes MODBUS-TCP, but it still conforms to the master-slave structure.

4) The MODBUS protocol was invented by the American Modicon company, whose purpose is: the MODBUS protocol is a non-chargeable open protocol. Later, Modicon was acquired by Schneider Electric, which inherited Modicon’s practices, and MODBUS remains a non-chargeable open protocol. Since MODBUS has become Schneider’s protocol, Schneider extended it to the network layer, constructing the network layer MODBUS-TCP protocol, as well as the internally used MODBUS-PLUS protocol. Due to space constraints, I will omit the description of these two protocols here.

5) About the differences between RS232 and RS485

Those who have studied analog and digital electronics know about differential circuits. Differential circuits have common-mode rejection ratios, which can eliminate common-mode errors. The RS485 interface has this characteristic. Therefore, the transmission distance of the RS232 interface is only a few meters, while the transmission distance of RS485/RS422 interfaces is 1200 meters.

Understanding RS232, RS485, RJ45, and Modbus Protocols

Understanding RS232, RS485, RJ45, and Modbus Protocols

We see from the diagram that although the RS232 and RS485 interfaces have the same appearance, their performance and information exchange modes differ, thus their anti-interference capabilities are also different.

6) When the distance is very long, the RS485 interface can also be connected to fiber optics, but it requires a pair of fiber converters. The reason for one pair is that one is used for electrical to optical conversion, while the second is used for optical to electrical conversion. The communication medium between the fiber transceivers is fiber optic or cable. (Note that fiber optics are the core of fiber cables, do not confuse them as two different things)

Fiber optics are divided into single-mode and multi-mode. Single-mode fiber is thinner, and light reflects less during transmission, thus having less distortion, and its transmission distance can reach over 15km; multi-mode fiber is thicker, and light reflects more during transmission, thus having more distortion, and its transmission distance is 1.5km.

Understanding RS232, RS485, RJ45, and Modbus Protocols

7) The CRC check code is a binary division without borrowing, used to check whether the received information is erroneous.

Understanding RS232, RS485, RJ45, and Modbus Protocols

Understanding RS232, RS485, RJ45, and Modbus Protocols

Note that here f(x) is the MODBUS communication frame without the CRC check code, and the divisor is CRC16. The CRC in the frame is the remainder after calculation.

The master station calculates the CRC before sending the frame, and then attaches the calculated CRC remainder to the end of the frame to send to the slave station. After the slave station receives the frame, it first performs CRC calculation on the frame without the CRC part to check for correctness; if incorrect, the slave station requests the master station to resend.

Similarly, when the slave station sends information to the master station, the master station also checks the data’s correctness based on the CRC. If an error is found, it requests the slave station to resend.

8) About MODBUS-RTU, MODBUS-ASC, and MODBUS-TCP

If the way MODBUS expresses data in bytes uses BCD code, it is called MODBUS-RTU; if the way MODBUS expresses data in bytes uses ASCII code, it is called MODBUS-ASC; if MODBUS operates at the network layer, it is called MODBUS-TCP.

The contents of ASCII code are as follows:

Understanding RS232, RS485, RJ45, and Modbus Protocols

In actual use of MODBUS, most use BCD code, thus MODBUS-RTU is widely applied.

BCD code is as follows:

Understanding RS232, RS485, RJ45, and Modbus Protocols

It is worth noting that in protocol usage, the values in the data frame are all expressed in hexadecimal. For example, a current of 100A is written as 0X64H, while a voltage of 380V is written as 0X17CH.

9) About the twisted pair communication line and grounding used in RS485 networks

We know that there will be distributed capacitance between two parallel cables, and distributed capacitance will weaken the signal strength. To eliminate distributed capacitance, the two parallel lines of the communication line need to be twisted together at a certain length, and this wire is called a twisted pair. The twisting length of the twisted pair has standards, and it is closely related to the communication rate. In practical use, it is necessary to choose a suitable twisted pair according to the communication rate.

The outer layer of the twisted pair has a shielding layer. The shielding layer must be grounded at a single point, and must not be grounded at both ends of the line, to prevent ground currents from flowing through and causing interference. In practical wiring, each segment should be grounded independently, and it is strictly forbidden to connect the shielding layers of all segments to a unified ground before and after.

10) About the daisy chain communication link connection method

Absolutely daisy chain networks do not exist. In fact, in the communication network constructed using daisy chain wiring methods, each node is a terminal, connected to each slave station through twisted pairs, and these twisted pairs form a star structure. We can refer to this wiring method as quasi-star wiring under daisy chain networks.

Engineering practice has proven that the length of quasi-star wiring should not exceed 70cm. Once it exceeds, communication instability may occur.

The 70cm length has become an unwritten quality inspection standard in the industry.

A 100-ohm terminating resistor can be added or not when the communication rate is low, but when the communication rate is high (above 19.2kbps), it is recommended to definitely add it. For example, in the RS485 network under PROFIBUS, the terminating resistor has been integrated into the terminal device, and can be added or removed simply by toggling a switch.

The purpose of the terminating resistor is to absorb reflected waves.

When we tightly tie a rope between two trees and then strike one side of the rope, we will see a conducting wave traveling to the other end and observe a reflected wave. If the striking frequency is appropriate, a standing wave will appear in the middle of the rope, which is called a standing wave.

For communication, whether it is reflected waves or standing waves, they will severely affect communication quality. The terminating resistor is used to absorb reflected waves and can also enhance the final level of the slave station.

Both RS485 and MODBUS concepts need to be mastered through practice; it is difficult to understand and grasp them solely by reading texts. If this article can benefit your practical activities, I would be very pleased.

END

Click “Read the Original” to purchase Mr. Zhang’s work

Leave a Comment