Modbus RTU CRC in Python
Introduction
Modbus is a popular communication protocol used in industrial automation systems. It is widely used for connecting various devices such as PLCs, sensors, and actuators. Modbus RTU is one of the most common variants of the Modbus protocol. It uses a binary representation of data and is transmitted over a serial communication link.
In Modbus RTU, each message is encapsulated in a frame. The frame consists of a start bit, the actual message payload, and an error checking field called the CRC (Cyclic Redundancy Check). The CRC is used to ensure the integrity of the data being transmitted.
In this article, we will explore how to calculate the CRC for Modbus RTU messages in Python.
CRC Calculation
The CRC is calculated using a mathematical algorithm that generates a unique checksum based on the data being transmitted. The receiver can then use the same algorithm to calculate the CRC of the received data and compare it with the transmitted CRC to check for errors.
The CRC algorithm used in Modbus RTU is called CRC-16-Modbus. It is a 16-bit CRC algorithm that performs a bitwise XOR operation on the data being transmitted. The result is a 16-bit checksum that is appended to the message.
To calculate the CRC in Python, we can use the crcmod
library. This library provides an implementation of the CRC-16-Modbus algorithm that we can use directly.
import crcmod
# Create a CRC-16-Modbus object
crc16 = crcmod.predefined.Crc('modbus')
# Calculate the CRC for a message
message = b'\x01\x03\x00\x00\x00\x02'
crc = crc16.calculate(message)
# Print the CRC value
print(f"CRC: {crc}")
In the code above, we first import the crcmod
module and create a CRC-16-Modbus object. We then calculate the CRC for a sample message and print the CRC value.
Verifying CRC
To verify the integrity of a received message, we need to calculate the CRC of the received data and compare it with the transmitted CRC.
import crcmod
# Create a CRC-16-Modbus object
crc16 = crcmod.predefined.Crc('modbus')
# Verify the CRC for a received message
received_message = b'\x01\x03\x00\x00\x00\x02\xC5\xCD'
received_crc = int.from_bytes(received_message[-2:], 'big')
calculated_crc = crc16.calculate(received_message[:-2])
# Compare the received and calculated CRC
if received_crc == calculated_crc:
print("CRC verification passed")
else:
print("CRC verification failed")
In the code above, we first import the crcmod
module and create a CRC-16-Modbus object. We then extract the received CRC from the received message and calculate the CRC for the rest of the message. Finally, we compare the received CRC with the calculated CRC to determine if the CRC verification passed or failed.
Conclusion
In this article, we have explored how to calculate and verify the CRC for Modbus RTU messages in Python. The CRC-16-Modbus algorithm is widely used in industrial automation systems to ensure the integrity of data being transmitted. By using the crcmod
library, we can easily calculate and verify the CRC for Modbus RTU messages in our Python applications.
Remember to always include the CRC field in your Modbus RTU messages and verify the CRC of received messages to ensure the reliability of your communication.