utilities — Extra Modbus Helpers

API Documentation

Modbus Utilities

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

pymodbus.utilities.default(value)

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.

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

An initialized property set

pymodbus.utilities.pack_bitstring(bits)

Creates a string out of an array of bits

Parameters:bits – A bit array

example:

bits   = [False, True, False, True]
result = pack_bitstring(bits)
pymodbus.utilities.unpack_bitstring(string)

Creates bit array out of a string

Parameters:string – The modbus data packet to decode

example:

bytes  = 'bytes to decode'
result = unpack_bitstring(bytes)
pymodbus.utilities.__generate_crc16_table()

Generates a crc16 lookup table

Note

This will only be generated once

pymodbus.utilities.computeCRC(data)

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

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

True if matched, False otherwise

pymodbus.utilities.computeLRC(data)

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

Parameters:
  • data – The data to calculate
  • check – The LRC to validate
Returns:

True if matched, False otherwise

pymodbus.utilities.rtuFrameSize(data, byte_count_pos)

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

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

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).