Serial communication and its common problems
[Copy link]
What is serial communication
Serial communication means that data is transmitted on only one line, and only one bit can be transmitted at a time. To transmit a byte, it needs to be transmitted 8 times. Serial communication is to transmit data in a string on a line, so it is called a serial port.
When the speed requirement is not high, using one wire to send data brings great convenience and practical value.
In order to send and receive correct data normally, asynchronous serial communication needs to be in the format shown below.
The communication parameters of the serial port include baud rate, start bit, data bit, check bit, and stop bit.
Serial communication is mainly divided into three communication modes: 232, 485, and 422. What are the differences between these three?
232
232 communication is mainly composed of three wires: RX, TX and GND.
RX connects to TX, TX connects to RX, and GND connects to GND. Here, sending and receiving are handled by different lines, which means that data can be sent and received at the same time, which is the so-called full-duplex communication.
Let me expand on this. Serial communication also has a function called full-function serial communication, also called standard serial port. Because data is transmitted between two devices, some devices process data faster and some slower. In order to ensure normal data transmission, several control pins are added on the basis of RX and TX, and finally become 9 pins, which is the common DB9, as shown in the figure below.
However, nowadays, many controllers, human-machine interfaces, PLCs, etc. generally do not use standard serial ports in serial communication, but directly use the three wires of RX, TX, and GND to communicate.
485
The emergence of 485 is to solve the problem of limited communication distance of 232.
485 communication only requires two wires, + and -, or also called A and B. The differential level signal of the two wires A and B is used as the data signal transmission. Since both sending and receiving use these two wires, it means that each can only be used for sending or receiving. Therefore, 485 is half-duplex communication.
485 sacrifices the full-duplex efficiency of 232 to achieve its own long transmission distance.
422
The emergence of 422 is to achieve the full-duplex communication mode of 232 and to increase the transmission distance like 485. 422 is also often marked as 485-4, while 485 is marked as 485-2. Because 485-2 is 2 wires and 485-4 is 4 wires, the following figure is a schematic diagram of 422.
422 is to split the RX of 232 into two lines, RX+, RX-, and TX into TX+, TX-. In this way, it can send and receive at the same time, and it can have a longer transmission distance like 485. However, such a communication method with great advantages is not used much, and the most commonly used ones are 232 and 485.
Common Serial Communication Problems
The computer can communicate with the device using USB to serial port, but the screen and the device cannot communicate
1) It is possible that the computer USB to serial port is connected to the device using the standard serial port function, that is, in addition to RX, TX, and GDN, other pins are also used. For example, in the actual communication with the screen, some pins need to be short-circuited like Omron PLC and Mitsubishi PLC.
2) When the computer communicates with the controller or PLC, the baud rate parameters are scanned and adaptive, and the screen communication parameters may not be as good as the equipment. In Mitsubishi, Keyence and other PLCs, there is a process of changing the baud rate for communication interaction.
3) It may also be that the wiring method is incorrect. Because some DB9s require male and female connectors. If you are not careful, you may connect TX to TX and RX to RX. This is something you need to pay attention to.
4) I would like to add here that sometimes you may use some serial port assistants to send test data to communicate with the controller. The parity check of some serial port assistants does not work, so this needs to be reminded.
The screen of company A can communicate with the device, but the screen of company B cannot communicate.
1) First, make sure the wiring is correct and whether RX and TX are compatible.
2) Is the ground wire not connected?
3) In addition to RX, TX, and GND, are there any other pins that need to be short-circuited?
4) Whether the communication protocol is consistent or imperfect, and whether the baud rate is the same.
Communication was possible without grounding before, why do we need grounding when changing equipment?
This question is similar to the previous one, because some devices use isolated power supplies. In the past, they could communicate without grounding, and it is possible that the ground wire has actually been connected, so communication is possible. Maybe you have changed to an isolated power supply, and the grounds of the two devices are isolated, so you need to connect the ground wire on the serial port. I have experienced this myself. A customer always said that his device could not communicate. Later, he took a picture and gave it to me. His ground wire was not connected. He said that he could communicate without connecting the ground wire before. So I gave him some popular science.
One device is 232, the other device is 422, there is no conversion device, what should I do?
I have encountered this situation before. The customer's device is 422 communication, but I don't have 422 equipment, only 232 communication can be tested. Therefore, it is necessary to convert 422 to 232 for communication.
I just talked about the wiring of 422 and 232, because these two are full-duplex, reception and transmission are separate, while 422 only transmits as a differential signal.
Connect the Rx+ of 422 to the TX of 232, and the RX- of 422 to the GND of 232.
Connect TX+ of 422 to RX of 232, and TX- of 422 to GDN of 232.
In this way, the data that the 422 device wants to send can be sent to the RX of 232. After the TX of 232 sends the data, since TX and GND also form a differential signal to 422, 422 can receive the data.
There is no problem with 232 communication, there is no problem with 485 communication, but after using 232 to 485, the communication is unstable
The biggest difference between 232 and 485 in terms of communication principle is the difference between full-duplex and half-duplex. However, the application layer does not care whether it is full-duplex or half-duplex when sending and receiving data.
But 485 has to be managed. Since it is half-duplex, it must be strictly ensured that only data can be sent or received on the channel. Once data is sent and received at the same time, the data will conflict. So the solution is that the master station device, that is, the party that actively commands, needs to strictly control the rhythm of sending data commands. Of course, some 232 to 485 devices are better and can optimize this, but the master station still needs to control it, and adjust the communication rate to a slower rate.
What is a good way to achieve two screens or two master stations accessing modbus devices through 485?
In 485 communication, there is basically one master and multiple slaves. However, in actual use, some customers want to use two screens to access a modbus device. There is no good way yet. After this function is released, I will demonstrate the operation to you.
Weaknesses of Serial Communication
1) Signal interference problem
It is recommended to use shielded wires and strictly connect them, such as grounding. For some 485 communications, consider connecting terminal resistors for matching. For 232, try not to make the wire too long. For communication protocols, try to avoid long message data communications.
2) Baud rate matching problem
Because the baud rate calculated by some devices has errors, especially some controllers, because the crystal oscillators used are different. Therefore, there are errors in some baud rates, such as 9600 baud rate. What is the impact of the error? Because the receiver calculates a bit by time. If a message is too long, there will be a problem of error accumulation, and the calculation will be biased. Therefore, this is also some of the instability of serial communication. When using it, you should pay attention to avoid sending packets with too long data.
3) In some cases where there may be interference, you can consider using odd or even parity. Although the possibility of errors is small, since there is interference, if you add parity, at least you can filter out the wrong messages. It is better than not having parity and then not knowing that the communication data is wrong. Or try to use some protocols with parity to prevent data errors.
4) Serial communication is inherently slow, so please lower the requirements for data response.
Because serial communication itself is slower than Ethernet. Moreover, serial communication cannot be multi-threaded like CPU. Because there is only one port and one line for data to go out, even if you use multi-threading to process data in your application, there is only one port at the bottom, and only one bit or one byte can be transmitted at a time. Because some customers use 9600 baud rate communication, but hope that the data can be responded within a certain millisecond.
However, serial communication still requires seeking truth from facts, so a correct understanding of serial communication is of great help to application, development, and communication.
Why not use synchronous communication?
As mentioned earlier, synchronous communication needs to rely on clock signals. This raises a question: who initiates this clock signal? In synchronous communication, a master device is often required to initiate a clock signal to read the data from the module. In practice, there are screens that read PLCs and screens that read screen data. From the perspective of asynchronous serial communication, there is no master-slave relationship. Both parties are equal roles and can send and receive information to each other. Synchronous communication is generally used for CPU to read some modules, and the CPU initiates the clock signal. For example, to read the SD card module, you can use the SPI method, as well as some sensor modules.
丨 The article is organized to spread related technologies, the copyright belongs to the original author丨
丨 If there is any infringement, please contact us to delete丨
|