Hello everyone, I am Da Yi, and today we are going to talk about the serial communication protocol between microcontrollers (MCU) and PLCs. When it comes to serial communication, many of you might feel confused, but don’t worry, I will clarify everything shortly.
What is Serial Communication?
Communication is everywhere; for example, the way you and I are communicating right now is a form of communication. Similarly, electronic devices also need a standardized way to transmit data, which is the role of communication protocols.
As the name suggests, serial communication sends data bit by bit in a sequence, much like how we humans converse one word at a time.
Unlike parallel communication, serial communication only requires a pair of transmission and reception lines during transmission, which makes the hardware structure simple and requires fewer pins. Therefore, it is widely used in embedded systems like microcontrollers and PLCs where resources are limited.
Common Serial Communication Protocols
There are several popular serial protocols on the market, such as RS-232, RS-485, SPI, and I2C. Among them, RS-232 (half-duplex/full-duplex) and RS-485 (half-duplex) are the most common for serial communication between microcontrollers and PLCs.
RS-232
RS-232, also known as the serial port, is the most familiar to everyone. It uses single-ended transmission, theoretically allowing for longer transmission distances (up to 15 meters), but it has poor anti-interference capability. Another drawback is that devices must be connected one-to-one, which is not suitable for networking.
RS-485
RS-485 uses differential transmission, which provides strong anti-interference capability, with a theoretical distance of up to 1.2 kilometers. It also supports bus-type multi-point connections and can be networked, commonly used in industrial settings to facilitate data exchange between control systems and devices.
However, RS-485 itself is only a physical layer protocol that specifies electrical characteristics, allowable load capacity, etc., without addressing the details of communication data format. Therefore, it is often layered with other protocols like Modbus and CanOpen.
Modbus Protocol Analysis
The Modbus protocol is the most widely used for serial communication between PLCs and microcontrollers. So, what is Modbus? In simple terms, it is a set of communication formats regarding how master-slave devices transmit data.
Modbus data is transmitted in the form of “messages,” and the instruction format is very fixed. Each message consists of:
-
Message Header: Indicates who is sending, who is receiving, and the data length -
Function Code: For example, read/write register, read coil status, etc. -
Data Area: Contains specific operational data -
Error Check Code: CRC checksum
The specific format is shown in the figure below:
[Message Header] [Function Code] [Data Area] [CRC Check]
Modbus can operate in either ASCII or RTU transmission modes, corresponding to different header formats. However, RTU mode is more efficient and widely used.
If you want to delve deeper into Modbus details, I strongly recommend buying a book titled “Detailed Explanation of the Modbus Serial Communication Protocol,” which contains a wealth of typical case analyses.
Wiring for PLC and Microcontroller Communication
Alright, after discussing so much theoretical knowledge, it’s time to get into some practical content. For example, if we want to establish Modbus-RTU communication between a microcontroller and a PLC, how should we connect the hardware?
It’s simple; just connect the TX of the microcontroller to the RX of the PLC, the RX of the microcontroller to the TX of the PLC, and then connect the grounds. Of course, you can also add a 485 chip as a transceiver, as shown below:
┌─────────┐ ┌───────┐
│ │ TX ──────────────> RX │
│ Microcontroller │ RX <─────── TX │
│ │ /RTS──┬ │
└────┬────┘ │ │
│ 485 Chip │ ┌─┴─┐
│ ┌────────┐ │ │ │
└──┤DE /RE│<─────── │PLC│
│A B │ │ │
└───>─────┘ └───┘
\_____
\_____ Ground
Once both are powered, they can communicate with each other via the serial cable. The serial assistant is a great debugging tool that allows you to visually monitor the data being transmitted and received, which I use regularly during development.
If the data format is incorrect, it may be due to mismatched parameters such as baud rate, data bits, or parity bits, or incorrect wiring. During debugging, you can first perform a loopback test; if the microcontroller can receive the data it sent itself, it indicates that the microcontroller’s serial port is functioning correctly, and you can then troubleshoot the PLC or communication program.
Summary
Alright, that’s all for the basic principles of serial communication protocols and the details of Modbus communication. Did you grasp the key points? Serial communication is widely used between microcontrollers and PLCs, so it is essential to master the basic principles and common communication formats.
Finally, I leave you with a small exercise: try to write a simple Modbus slave program to implement read/write operations on a single holding register. You can directly send read/write commands to your program using the serial assistant for testing. Completing this small project will surely deepen your understanding of serial communication. If you encounter any issues, please leave a message in the comments section. See you next time!