utilities — Extra Modbus Helpers

API Documentation

Modbus Utilities

A collection of utilities for packing data, unpacking data computing checksums, and decode checksums.


Given a python object, return the default value of that object.

Parameters:value – The value to get the default of
Returns:The default value
pymodbus.utilities.dict_property(store, index)

Helper to create class properties from a dictionary. Basically this allows you to remove a lot of possible boilerplate code.

  • store – The store store to pull from
  • index – The index into the store to close over

An initialized property set


Creates a string out of an array of bits

Parameters:bits – A bit array


bits   = [False, True, False, True]
result = pack_bitstring(bits)

Creates bit array out of a string

Parameters:string – The modbus data packet to decode


bytes  = 'bytes to decode'
result = unpack_bitstring(bytes)

Generates a crc16 lookup table


This will only be generated once


Computes a crc16 on the passed in string. For modbus, this is only used on the binary serial protocols (in this case RTU).

The difference between modbus’s crc16 and a normal crc16 is that modbus starts the crc value out at 0xffff.

Parameters:data – The data to create a crc16 of
Returns:The calculated CRC
pymodbus.utilities.checkCRC(data, check)

Checks if the data matches the passed in CRC

  • data – The data to create a crc16 of
  • check – The CRC to validate

True if matched, False otherwise


Used to compute the longitudinal redundancy check against a string. This is only used on the serial ASCII modbus protocol. A full description of this implementation can be found in appendex B of the serial line modbus description.

Parameters:data – The data to apply a lrc to
Returns:The calculated LRC
pymodbus.utilities.checkLRC(data, check)

Checks if the passed in data matches the LRC

  • data – The data to calculate
  • check – The LRC to validate

True if matched, False otherwise

pymodbus.utilities.rtuFrameSize(data, byte_count_pos)

Calculates the size of the frame based on the byte count.

  • data – The buffer containing the frame.
  • byte_count_pos – The index of the byte count in the buffer.

The size of the frame.

The structure of frames with a byte count field is always the same:

  • first, there are some header fields
  • then the byte count field
  • then as many data bytes as indicated by the byte count,
  • finally the CRC (two bytes).

To calculate the frame size, it is therefore sufficient to extract the contents of the byte count field, add the position of this field, and finally increment the sum by three (one byte for the byte count field, two for the CRC).