Understanding RS232, RS485, RJ45, and Modbus Protocols

(Click the above brown words to download the materials)

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

Note that there are three essential conditions to be met:

The first is that the shapes, sizes, pin diameters, and lengths of the plug and socket pins must correspond one-to-one; otherwise, the connection cannot be completed. This stipulates 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 requirements 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, perfect power supply cannot be achieved. This determines the operational nature of the power supply.

These three points essentially define the normative protocol for the physical layer of the power supply plug combination.

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 stipulates the mechanical shape of the interface, pin definitions, interface levels, and byte format.

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

Next, let’s examine the operational modes of communication interfaces and networks.

When we make a phone call on our mobile phone, we find that both parties can talk and listen simultaneously; this is called full duplex (two-way operation mode); if one party cannot listen while talking, and cannot talk while listening, but both parties have the ability to speak and listen, this is the communication style of a walkie-talkie, which is called half duplex.

Understanding RS232, RS485, RJ45, and Modbus Protocols

(To clarify: these images are excerpts from a PPT I did at ABB regarding MODBUS communication; they are somewhat dated but the basic content is correct. These images are for reference.)

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

For half duplex interfaces, it is evident that 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 and slave stations seems to be just a difference in communication working modes, but in essence, it is a reasonable allocation of control over the communication bus among all parties involved.

Next, let’s examine the bus connection issue.

We can take an example from a power supply. We can draw a main trunk line from the power supply, and then parallel several branches to supply power to several loads. As long as the power supply’s power requirements are met, this is clearly 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 also be added. If any point on this communication line experiences a disconnection, the communication on the subsequent communication links will also be interrupted. This wiring method is vividly referred to as the daisy-chain connection method or chain connection method, while the wiring method for power supplies is called the 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 links are parallel. However, from a communication perspective, the links are daisy-chained, representing an orderly connection, one after another.

Now we can summarize:

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

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

With a physical layer communication interface, can communication be established? The answer is no. The physical layer communication interface merely provides the conditions for communication. However, if neither party understands what the other is saying, or if the speaking methods and grammatical structures of both parties do not match, 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 can ensure that the communication language is in a format that both parties can understand.

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

MODBUS is also master-slave structured. Similar to the bus control in the physical layer, the master-slave relationship here specifies the control rights over the communication bus. The master station first issues commands, occupying the bus; then vacates the bus for the slave station to write response codes; after the slave station completes its task, it returns the bus to the master station.

Now let’s take a look at the frame structure specified 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, the frame structure of different command function codes varies. For the read register command, the frame structure of the MODBUS master station is: 2 bytes of address code, 1 byte of function code, 2 bytes of data address code, and 2 bytes of CRC check code; the frame structure of the MODBUS slave response is: 2 bytes of function code, 1 byte of total number of response bytes, N bytes of response data, and 2 bytes of CRC check code.

Although the physical layer protocol and the data link layer protocol are different, the execution of the data link layer protocol must be based on the requirement that the physical layer connections of both parties are compliant, and that unobstructed information exchange can be achieved.

This rule must be 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 and fault-free connection and information exchange relationship for the upper-level protocols. This means that the hierarchical relationship between layers must be absolute.

From the data link layer upwards, we move to the network layer. Its task is to form the information exchange network of the fieldbus.

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

Since the network structures of both parties may differ, the same types of networks need to use bridges to connect, while different types of networks require gateways for connection.

There may be multiple channels between networks. Data packets can choose from various paths when sending. The component responsible for selecting paths is called a router. The router not only determines the actual data exchange network path, but can also construct virtual network paths, and determine the order of data packet transmission. 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 are collectively referred to as the fieldbus, and its communication interface is the 8-pin RJ45 crystal head. Clearly, RJ45 is completely different from RS232/RS485/RA422.

The data packets of the network layer are combinations of data frames. In layman’s terms, data packets are short texts, or a page of data combination units to be transmitted.

The routing issue and receiving combination issue of data packets at the network layer are shown in the following diagrams:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Understanding RS232, RS485, RJ45, and Modbus Protocols

We see that at the network layer, communication first determines the routing path via the router, and then sends the packets to the other party. Upon receiving the packets, the other party combines them in order and unpacks them into the actual documents.

It is important to note: with the router at the network layer, the network layer supports star network structures.

Now let’s take a look at 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 also ignore them. However, it is important 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, which will be omitted for brevity.

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

When a PLC exchanges information with a certain electrical instrument, and these electrical instruments comply with RS485/MODBUS-RTU communication specifications, what do we need to do?

The first step is to wire according to the daisy-chain structure communication link requirements, connecting the PLC’s communication interface with N electrical instrument interfaces. The last electrical instrument’s end must be equipped with a 100-ohm terminating resistor.

The second step is to determine each electrical instrument’s address according to the principle of increasing addresses, for example, 01H, 02H, 1FH, etc. Here, H indicates hexadecimal, and 1F represents 16 + 15 = 31.

The third step is to set the communication rate specified by the electrical instruments in the PLC programming software.

The fourth step is to set the MODBUS communication codes according to the data area address codes of the electrical instruments in the PLC programming software, as well as the cyclic relationships of each slave station.

Note that the MODBUS communication codes here meet the requirements of the PLC’s IEC 61131-3 programming module; 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.

The fifth step is to allocate a dedicated data area in the PLC’s memory, where the information read from the electrical instruments will be stored and processed, allowing higher-level master stations to read the information. This data area is called a data point table and is sometimes referred to as a communication protocol.

Finally, of course, it’s time for testing. There is a lot of content involved, and due to space limitations, I will not elaborate further.

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

A certain electrical instrument has the address 01H. At memory position 2000 of the electrical instrument, six data points such as three-phase current and three-phase voltage are stored, each occupying two bytes, totaling 12 bytes.

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

We also know that the frame structure for the master station to read data (downstream frame) includes 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 taken for the master station to send 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, 03 is the command, and 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 six consecutive words, which are the current and voltage parameters in memory, and 0XC545H is the CRC check code for 01 03 07 D0 00 06.

The response frame of the electrical instrument (upstream frame) includes: 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, taking: 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 meanings as before, 0X0CH indicates that 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 at 100A, 0X00DCH indicates that phase A voltage is 220V, and the following two groups are phase B and phase C voltages, both at 220V, and finally, 0XD6F5H is the CRC check code.

From the master station, a downstream communication frame is initiated, and after waiting 10 milliseconds for the slave station to respond, the total time taken from the master station’s initiation to the completion of the last response is:

Understanding RS232, RS485, RJ45, and Modbus Protocols

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

Understanding RS232, RS485, RJ45, and Modbus Protocols

The 1.12 seconds is the reading data cycle for these 31 instruments with a communication rate of 9600bps, and it ignores the waiting time for the master station to send the downstream communication frame again, so the actual time will be slightly longer.

Believe that after reading this, everyone should have a deeper understanding of the communication frames under MODBUS-RTU.

Just a reminder: each word consists of two bytes. Generally, bytes can only express 8 binary states. However, for analog quantities, a word is used to express them. For example, a current of 1250A is 04E2H in hexadecimal, which requires 2 bytes to express completely. Therefore, various electrical instruments express analog quantities using words.

The following are some commonly used function codes of MODBUS, i.e., command codes:

Understanding RS232, RS485, RJ45, and Modbus Protocols

Here are examples of the downstream and upstream communication frames when the PLC reads the data point table of the ASCO dual 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 bus control issue.

It’s 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. The 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 occupancy problem.

2) When a link experiences a disconnection, dual master station measures can be used to avoid communication interruptions.

Dual master stations (two RS485 interfaces of the PLC) are connected with a handshake line; usually, the primary RS485 is active, while the secondary RS485 is floating. The floating RS485, while connected to the bus, is in a high impedance state, equivalent to being completely disconnected. When a disconnection occurs, the slave station confirms and immediately activates communication, connecting communication from both ends of the link.

Sometimes, ring communication measures are also taken. Due to space limitations, I will not elaborate.

3) MODBUS can operate 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 company Modicon, whose mission is: the MODBUS protocol is a free and open protocol. Later, Modicon was acquired by Schneider Electric, which inherited Modicon’s practices; MODBUS remains a free and open protocol. Since MODBUS has become Schneider’s protocol, Schneider extended it to the network layer, developing the MODBUS-TCP protocol at the network layer, along with the internally proprietary MODBUS-PLUS protocol. Due to space limitations, I will omit the description of these two protocols.

5) Regarding 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 dozen meters, while the transmission distance of RS485/RS422 interfaces can reach 1200 meters.

Understanding RS232, RS485, RJ45, and Modbus Protocols

Understanding RS232, RS485, RJ45, and Modbus Protocols

From the images, we can see that although the RS232 and RS485 interfaces have the same appearance, their performance and information exchange modes differ, thus affecting their anti-interference capabilities.

6) When distances are long, the RS485 interface can also connect to fiber optics, but requires a pair of fiber optic converters. The reason for a 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 fiber optic transceivers is optical cable or fiber. (Note: fiber is the core of optical cable, don’t confuse it with two different things.)

Optical fibers are classified as single-mode and multi-mode. Single-mode fibers are thinner, and light reflects less during transmission, resulting in less distortion, with a transmission distance of over 15km; multi-mode fibers are thicker, and light reflects more during transmission, resulting in greater distortion, with a transmission distance of 1.5km.

Understanding RS232, RS485, RJ45, and Modbus Protocols

7) The CRC check code is a binary non-borrowing division 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 f(x) refers to the MODBUS communication frame excluding the CRC check code, and the divisor is CRC16. The CRC in the frame is the remainder after the calculation.

Before the master station sends the frame, it first performs a CRC calculation on the frame, then attaches the CRC remainder to the end of the frame and sends it to the slave station. Upon receiving the frame, the slave station first performs a CRC calculation on the frame excluding the CRC part to verify 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 CRC. If an error is found, it requests the slave station to resend.

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

If the MODBUS byte expresses data in BCD code, it is referred to as MODBUS-RTU; if it uses ASCII code, it is referred to as MODBUS-ASC; if MODBUS operates at the network layer, it is referred to as MODBUS-TCP.

The contents of ASCII code are as follows:

Understanding RS232, RS485, RJ45, and Modbus Protocols

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

BCD code is as follows:

Understanding RS232, RS485, RJ45, and Modbus Protocols

It is important to note 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) Regarding the twisted communication lines and grounding used in RS485 networks

We know that between two parallel cables, there will be distributed capacitance, which weakens 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 type of line is called a twisted pair. The twisting length of twisted pairs is standardized and closely related to the communication rate. In practical use, the appropriate twisted pair must be selected according to the communication rate.

The outer layer of twisted pairs has a shielding layer. The shielding layer must be grounded at a single point, and should not be grounded at both ends of the wire, to prevent ground currents from causing interference. In practical wiring, independent grounding for each line segment should be adopted, and it is crucial to avoid the practice of connecting the shielding layers of all segments to a unified ground.

10) Regarding the daisy-chain communication link connection method

Absolutely daisy-chain structured networks do not exist. In fact, the communication network constructed using the daisy-chain wiring method has nodes as wiring terminals, connected to various slave stations via twisted pairs, and these twisted pairs form a star-like structure. We can refer to this wiring method as quasi-star wiring under the chain network.

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

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

A 100-ohm terminating resistor can be added or not at low communication rates, but when the communication rate is high (above 19.2kbps), it is recommended to definitely add one. For example, in the RS485 network under PROFIBUS, the terminating resistor is already embedded in the terminal device, and it 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 propagating wave traveling to the other end, and we can also see a reflected wave. If the striking frequency is appropriate, a standing wave appears in the middle of the rope, which is called a standing wave.

For communication, both reflected waves and standing waves can severely affect communication quality. The terminating resistor is used to absorb reflected waves and can also elevate the final slave station’s signal level.

Both RS485 and MODBUS concepts need to be mastered through practice; simply reading texts makes it difficult to understand and grasp them. If this article can benefit everyone’s practical activities, I will be extremely pleased.

Hurry up and share or save it!

Source: Internet

Leave a Comment