laitimes

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

author:Siege of the Great Lion

Imagine a DC power supply with three pins on the output socket interface, which are positive, negative, and ground. Correspondingly, the plug of the load should also have three pins that correspond to the power supply side, so that the power supply can be obtained correctly.

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

The first is that the shape, size, diameter and length of the plug and socket pins must correspond one by one, otherwise the plugging operation cannot be completed. This defines the physical structure and pin definition of the plug combination.

The second is that the output voltage value of the power supply must meet the demand value of the load side, otherwise the requirements of the electrical parameters cannot be completed. This determines the level specification of the plug combination.

The third is that the output impedance of the power supply must match the input impedance of the load, otherwise a perfect power supply cannot be realized. This determines the nature of the power supply.

These three points are actually the normative protocols for power plug combinations at the physical level.

Let's look at the communication interface. In the ISO/OSI model of computer information exchange, the physical layer is the lowest layer (layer 1), which specifies the mechanical form factor of the interface, the interface pin definition, the interface level, and the byte format.

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

Let's look at the working system of communication interfaces and communication networks.

When we hang up the phone with our mobile phone, we find that the two sides of the communication can also answer at the same time as the call, which is called full-duplex (two-way work system); If you can't listen when you speak, and you can't speak when you answer, but either party has the ability to speak and listen, that is, the intercom type of talkie, which is called half-duplex.

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

(TO CLARIFY: THESE ARE EARLY PPT EXCERPTS OF THE MODBUS COMMUNICATION I DID AT ABB, BUT THE BASIC CONTENT IS CORRECT.) These figures are for reference. )

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

For half-duplex interfaces, it is obvious that there needs to be an initiator of communication, so the RS485 interface and network must have a master and a number of slaves, and the number of slaves is also specified. In general, the number of slaves is 32.

The relationship between RS485 master and slave seems to be only the difference in the communication work system, and its essence is the reasonable distribution of the control of the communication bus by all parties to the communication.

Let's look at the bus connection issue again.

Let's take the power supply as an example. We can draw a trunk from the power supply, and then connect several branches in parallel and send them to several loads. As long as the power requirements of the power supply are met, this is obviously possible.

Would it be feasible if we used the same method to draw out the RS485 communication line? The answer is no. We must lead a line from the communication master to the first communication substation, and then lead the second line from the first substation to the second substation, and so on until the last substation. At the end of the communication line, a terminal resistor is also equipped. If there is a disconnection at any point on this communication line, the communication on the subsequent communication link will also be disconnected. This wiring method is figuratively called the chrysanthemum petal connection method, or the chain connection method, while the wiring method of the power supply is called the star connection method.

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?
RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

We found that the links were in parallel from the electrical wiring. But from the point of view of communication, the link is chrysanthemum-petaled, belonging to an orderly connection one after another.

Now we can summarize:

The bus network wiring mode of RS485 must be the wiring mode of chain chrysanthemum petals, and it belongs to the half-duplex communication mode; RS232 is a point-to-point wiring method, which belongs to full-duplex communication. Whether it is an RS232 interface or an RS485 interface, they must conform to the communication protocol of the physical layer.

再看MODBUS-RTU通信协议:

With the physical layer communication interface, is it possible to communicate? The answer is no. The physical layer communication interface only enables the communication conditions of the two parties. However, if neither of the two parties understands what they are saying, or if the way they speak and the grammatical structure of the two parties do not match, it is clear that there is no communication either.

In the OSI model, the physical layer is on top of the data link layer. The MODBUS-RTU protocol is the data link layer protocol, as long as both parties use the MODBUS-RTU protocol, it can ensure that the communication language is a statement format that both parties can understand.

Notice the word "statement" here. The physical layer defines bytes, which are equivalent to words in the language, while the data link layer organizes bytes into statements, or frames. Frames specify the grammatical structure of the utterances used by both parties to the communication.

MODBUS IS ALSO MASTER-SLAVE. The bus control is the same as the physical layer, and the master-slave relationship here is to stipulate the control of the communication bus. The master station first gives the order and occupies the bus; Then leave the bus vacant and hand it over to the slave station to write the response code; After the slave station is completed, the bus is returned to the master station.

Now let's take a look at the frame structure specified by the HDLC of ISO, i.e. the syntactic structure of the communication statement, as follows:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

Under the MODBUS communication protocol, the frame structure of different command function codes is different. For the read register command, the MODBUS master frame structure is: 2 bytes of address code, 1 byte of function code, 2 bytes of data address code, 2 bytes of CRC check code; The slave response frame structure of MODBUS is as follows: 2 bytes of function code, 1 byte of total bytes of response area, N bytes of response data, and 2 bytes of CRC check code.

Although the physical layer protocol is different from the data link layer protocol, the implementation of the data link layer protocol must be based on the fact that the physical layer connection between the two parties has met the requirements and can realize information exchange without barriers.

This rule must be fully and thoroughly enforced in the Layer 7 protocol of the ISO/OSI model. In the ISO/OSI model, the lower-level protocols of the communicating parties must establish a transparent, trouble-free connection and information exchange relationship for the higher-level protocols. In other words, the relationship between superiors and subordinates at all levels must be absolute.

From the data link layer further up, there is the network layer. Its task is to form an information exchange network for fieldbuses.

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

Since the network structure of the two parties may be different, the same network needs to be connected by a bridge, and a gateway is required to connect the same network.

There may be more than one channel between networks. There are multiple paths to choose from when sending data packets. The element responsible for selecting the path is called a router. The router not only determines the real data exchange network path, but also builds a virtual network path, and also determines the sending order of data packets. Therefore, routers are the most complex and critical equipment in the network layer.

In the OSI model, the physical layer + data link layer + network layer is combined and called the fieldbus, and its communication interface is an 8-pin RJ45 crystal head. Obviously, RJ45 is not the same thing as RS232/RS485/RA422.

The grouping of data at the network layer is a combination of data frames. In layman's terms, a data grouping is a short article, or a page of data composition units to be delivered.

When the network layer sends data packets, the routing and reception combination problems are shown in the following figure:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?
RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

We see that when the network layer communicates, the routers first determine the routing path and then send packets to each other. After receiving the group, the other party will combine the grouping in the order before and after, and then unpack it into the actual document.

Note that since the network layer has a router, the network layer supports a star network structure.

Now let's focus on the 7-layer model of ISO/OSI, as follows:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

Since the subject did not mention the higher-level agreement, we ignored it. However, it should be made clear that from the network layer and above, the information units sent between the layers are already complete packets. The OSI model also specifies the grammatical structure of packets, which is ignored due to space limitations.

It is worth noting that the RS232/RS485/RS422 communication interfaces and their definitions are very clear. Including the level of the pins, the functional definition of the pins, and the timing of the data flow of the interface as it sends and receives information, these must be accurate and rigorous, otherwise the exchange of information cannot be performed.

When a PLC exchanges information with a power meter, and these power meters comply with the RS485/MODBUS-RTU communication specification. What are we going to do?

First, we wire the communication link according to the chrysanthemum petal structure, and connect the communication interface of the PLC with N power meter interfaces. The end of the last meter should be equipped with a 100 ohm termination resistor.

Second, we determine the respective addresses of these N power meters according to the principle of increasing addresses, such as 01H, 02H, 1FH and so on. Here H means hexadecimal, and 1F means 16+15=31.

Thirdly, we set the specified communication rate of the power meter in the PLC programming software.

Fourthly, we set the MODBUS communication code according to the data area address code of the power meter in the PLC programming software, as well as the cyclic relationship of each substation.

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

Fifth, a dedicated data area is opened up in the memory of the PLC to store the information read and processed from the power meter, so that the information can be read by the higher-level terminus. This data area has a name called a data point table, sometimes referred to simply as a communication protocol.

Finally, of course, it's the boot test. There is a lot of content in it, so I will not introduce it in the limited space.

Let's take a look at an example of reading data on an RS485 network using MODBUS-RTU, as follows:

An electric meter, the address is 01H. In the position of the 2000th position of the memory of the power meter, 6 data such as three-phase current and three-phase voltage are placed, and each data occupies two bytes, a total of 12 bytes.

The communication rate of this power meter is 9600bps. What does that mean? bps stands for a 0/1, which 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, 1 parity bit, exactly 10 bits or 10 bits, so if the communication rate of the power meter is 9600bps, then 1 second can send: 9600/10=960 bytes.

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

FOR THIS EXAMPLE, WE KNOW THAT THE MODBUS-RUT COMMAND TO READ THE DATA IS 0X03H, WHICH IS THE 03 COMMAND. Note the writing here: 0X is the prefix, 03 in the middle is the command, and the last H indicates that it is 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 represents the reading of 6 consecutive words, that is, the current and voltage parameters in the memory, and 0XC545H is the CRC check code of 01 03 07 D0 00 06.

Then the frame structure of the response frame (upstream frame) of the power meter is: 1 byte of address, 1 byte of function code, 1 byte of data area bytes, 12 bytes of data, 2 bytes of CRC check code, a total of 17 bytes, and the occupation time is: 17X10/9600 = 17.7 milliseconds.

The specific instrument response communication frame 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 before, 0X0CH means that there are 12 bytes in the upload data area, 0X0064H means that the current of phase A is 100A, the latter two groups are phase B and phase C current, both are 100A, 0X00DCH means that phase A voltage is 220V, and the last two groups are phase B and phase C voltage, both of which are 220V, Finally, 0XD6F5H is the CRC check code.

The master initiates a downlink communication frame, waits for 10 milliseconds for the slave to respond, and then receives the uplink communication frame sent back by the slave, and the total duration is as follows:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

If there are 31 identical meters waiting for the master to access them one by one, the total duration of the master from the first meter to the last response is completed:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

The 1.12 seconds here is the read data cycle of these 31 meters at a communication rate of 9600bps, and ignores the waiting time for the master to send the downlink communication frame again, and the actual time will be slightly longer.

I believe that seeing this, everyone should have a deeper understanding of the communication frame under MODBUS-RTU.

Reminder: A word has two bytes. In general, bytes can only be used to represent 8 switching quantities. But for analog quantities, it is necessary to express it in words. For example, if the current is 1250A and 04E2H in hexadecimal system, it takes 2 bytes to express the completeness. Therefore, in all kinds of power meters, the analog quantity is expressed in words.

The following are some of the commonly used function codes of MODBUS, that is, command codes:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

The following is an example of the downlink and uplink communication frames for the PLC reading the data point table of the dual-cast switch ASCO controller:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

A few related questions to explain:

1) Some fieldbuses solve the problem of bus control with tokens.

It's easy to think that if the slave has an urgent matter that needs to be serviced by the master, but MODBUS stipulates polling rules, and it may be too late by the time you get there. So many fieldbuses have invented a special thing called a token. The token is short, only one byte, and it can be passed on the bus very quickly. The token is passed in each site, and whoever gets the token is the master site and can publish the information. If there is nothing to be published on this site, the token is handed over to the next site, thus solving the bus occupancy problem.

2) When the link is disconnected, in order to avoid communication interruption, the dual-master measure can be adopted. The two master stations (the two master RS485 interfaces of the PLC) are connected by handshake wires, and the main RS485 is usually turned on, while the auxiliary RS485 is floating. Although the RS485 is connected to the bus, it is in a high-impedance state, which is equivalent to a complete detachment. When a circuit break occurs, the slave confirms that the communication is immediately activated, and the communication is carried out from both ends of the link.

In some cases, circular communication measures are also employed. Due to space limitations, no introduction will be given.

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

4) The MODBUS protocol was invented by the American company Modricane, and the company's purpose is that the MODBUS protocol is a public protocol with no fees. Later, Modicem was acquired by Schneider, and Schneider inherited Modicon's approach, and MODBUS was a no-charge, public agreement. Since MODBUS has become Schneider's protocol, Schneider has extended it to the network layer and built the MODBUS-TCP protocol at the network layer, as well as the internal dedicated MODBUS-PLUS protocol. For space limitations, descriptions of these two agreements are omitted here.

5) About the difference between RS232 and RS485

Anyone who has studied both mode and digital electronics knows about differential circuits. Differential circuits have a common-mode rejection ratio that eliminates common-mode errors. The RS485 interface has this feature. Therefore, the transmission distance of RS232 interface is only more than ten meters, while the transmission distance of RS485/RS422 interface is 1200 meters.

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?
RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

As we can see from the figure, although the RS232 and RS485 interfaces have the same shape, their performance and information exchange mode are different, and therefore their anti-interference capabilities are also different.

6) When the distance is very long, the RS485 interface can also be connected to the optical fiber, but it needs to be equipped with 1 pair of optical fiber converters. The reason why 1 pair is is because one of them is for electrototyping and the second is for photototyping. The communication medium in the middle of the optical fiber transceiver is the optical cable or optical fiber. (Note that optical fiber is the core of the optical cable, don't think it's two things)

Optical fibers are divided into singlemode and multimode. The single-mode optical fiber is thinner, and the light is less reflected during the transmission process, so the distortion is small, and its transmission distance can reach more than 15km; Multimode optical fibers are thicker, and light is reflected more during transmission, so the distortion is large, and its transmission distance is 1.5km.

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

7) The CRC check code is a binary non-borrowing division, which is used to check whether there is an error in the received information.

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?
RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

Note that f(x) here is the MODBUS communication frame with the CRC check sum removed, and the divisor is CRC16. The CRC in the frame is the remainder after the operation.

Before the master station sends the frame, the frame is first CRC calculated, and then the remainder of the CRC operation is attached to the end of the frame and sent to the slave. After receiving the frame, the slave first performs a CRC operation on the part of the frame that removes the CRC to check whether it is correct, and if it is incorrect, the slave requires the master to retransmit it.

Similarly, when the slave sends information to the master, the master also checks the correctness of the data according to the CRC. If an error is found, the slave is required to retransmit.

8)关于MODBUS-RTU、MODBUS-ASC和MODBUS-TCP

If the way the bytes express the data in MODBUS is BCD code, it is called MODBUS-RTU; If the way the bytes express the data in MODBUS is ASCII code, it is called MODBUS-ASC; If MODBUS runs on the network layer, it is called MODBUS-TCP.

The ASCII code reads as follows:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

In practical use, most of the MODBUS uses BCD code, so MODBUS-RTU is widely used.

The BCD code is as follows:

RS232, RS485, RJ45, and Modbus protocols, have these been confused for a long time?

It is important to note that in the use of the protocol, the values in the data frame are expressed in hexadecimal numbers. For example, 100A current is written as 0X64H, while 380V voltage is written as 0X17CH.

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

We know that there will be distributed capacitance between two parallel cables, and the distributed capacitance will weaken the strength of the signal. In order to eliminate the distributed capacitance, two parallel wires of a communication line need to be twisted against each other at a certain length, and this kind of wire is called a twisted pair. There is a specification for the twisted length of twisted pairs, which is closely related to the communication rate. In actual use, it is necessary to select the appropriate twisted pair according to the communication rate.

The twisted pair has a shield on the outside. The shield layer must be grounded at a single point, and must not be grounded at the same time at the beginning and end of the line to prevent interference caused by the flow of ground current. In the actual wiring, each line section is grounded independently, and the shielding layer of all line sections should not be connected to the ground before and after.

10) About the communication link connection method of chrysanthemum petals

There is no such thing as an absolute chain network of chrysanthemum petals. In fact, in the communication network we build with the chrysanthemum petal chain wiring method, each node is a terminal block, which is connected to each substation by a twisted pair by the terminal block, and these twisted pairs constitute a similar star structure, we might as well call this wiring method a quasi-star wiring under the chain network.

It has been proven in engineering practice that the length of the crosshairs should not exceed 70cm. Once exceeded, a state of communication instability may occur.

In fact, 70cm has also become an unwritten quality inspection specification in the industry.

The 100 ohm terminal resistor can be added or not added when the communication rate is low, but when the communication rate is high (higher than 19.2kbps), it is recommended to add it. For example, in the RS485 network under PROFIBUS, the terminal resistor has been implanted in the terminal device, and it can be joined or withdrawn by simply flipping a switch.

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

We tie a rope tightly between two trees, and then we tap on one side of the rope, and we see that there is a conductive wave going to the other end, and we can see the reflected wave. If the frequency of the strike is appropriate, a fixed point of the wave appears in the middle of the rope, which is called a standing wave.

For communication, whether it is a reflected wave or a standing wave, it will seriously affect the quality of communication. The termination resistor is used to absorb the reflected waves and to raise the level of the final substation.

RS485 and MODBUS are two concepts that need to be mastered through practice, and it is difficult to understand and grasp them just by reading the text. If this little article can bring benefits to everyone's practical activities, I will feel very gratified.