Module pyModbusTCP.utils

This module provide a set of functions for modbus data mangling.

Bit functions

pyModbusTCP.utils.get_bits_from_int(val_int, val_size=16)

Get the list of bits of val_int integer (default size is 16 bits)

Return bits list, least significant bit first. Use list.reverse() if need.

Parameters:
  • val_int (int) – integer value
  • val_size (int) – bit size of integer (word = 16, long = 32) (optional)
Returns:

list of boolean “bits” (least significant first)

Return type:

list

pyModbusTCP.utils.set_bit(value, offset)

Set a bit at offset position

Parameters:
  • value (int) – value of integer where set the bit
  • offset (int) – bit offset (0 is lsb)
Returns:

value of integer with bit set

Return type:

int

pyModbusTCP.utils.reset_bit(value, offset)

Reset a bit at offset position

Parameters:
  • value (int) – value of integer where reset the bit
  • offset (int) – bit offset (0 is lsb)
Returns:

value of integer with bit reset

Return type:

int

pyModbusTCP.utils.toggle_bit(value, offset)

Return an integer with the bit at offset position inverted

Parameters:
  • value (int) – value of integer where invert the bit
  • offset (int) – bit offset (0 is lsb)
Returns:

value of integer with bit inverted

Return type:

int

pyModbusTCP.utils.test_bit(value, offset)

Test a bit at offset position

Parameters:
  • value (int) – value of integer to test
  • offset (int) – bit offset (0 is lsb)
Returns:

value of bit at offset position

Return type:

bool

Word functions

pyModbusTCP.utils.word_list_to_long(val_list, big_endian=True, long_long=False)

Word list (16 bits) to long (32 bits) or long long (64 bits) list

By default word_list_to_long() use big endian order. For use little endian, set big_endian param to False. Output format could be long long with long_long option set to True.

Parameters:
  • val_list (list) – list of 16 bits int value
  • big_endian (bool) – True for big endian/False for little (optional)
  • long_long (bool) – True for long long 64 bits, default is long 32 bits (optional)
Returns:

list of 32 bits int value

Return type:

list

pyModbusTCP.utils.long_list_to_word(val_list, big_endian=True, long_long=False)

Long (32 bits) or long long (64 bits) list to word (16 bits) list

By default long_list_to_word() use big endian order. For use little endian, set big_endian param to False. Input format could be long long with long_long param to True.

Parameters:
  • val_list (list) – list of 32 bits int value
  • big_endian (bool) – True for big endian/False for little (optional)
  • long_long (bool) – True for long long 64 bits, default is long 32 bits (optional)
Returns:

list of 16 bits int value

Return type:

list

Two’s complement functions

pyModbusTCP.utils.get_2comp(val_int, val_size=16)

Get the 2’s complement of Python int val_int

Parameters:
  • val_int (int) – int value to apply 2’s complement
  • val_size (int) – bit size of int value (word = 16, long = 32) (optional)
Returns:

2’s complement result

Return type:

int

Raises:

ValueError – if mismatch between val_int and val_size

pyModbusTCP.utils.get_list_2comp(val_list, val_size=16)

Get the 2’s complement of Python list val_list

Parameters:
  • val_list (list) – list of int value to apply 2’s complement
  • val_size (int) – bit size of int value (word = 16, long = 32) (optional)
Returns:

2’s complement result

Return type:

list

IEEE floating-point functions

pyModbusTCP.utils.decode_ieee(val_int, double=False)

Decode Python int (32 bits integer) as an IEEE single or double precision format

Support NaN.

Parameters:
  • val_int (int) – a 32 or 64 bits integer as an int Python value
  • double (bool) – set to decode as a 64 bits double precision, default is 32 bits single (optional)
Returns:

float result

Return type:

float

pyModbusTCP.utils.encode_ieee(val_float, double=False)

Encode Python float to int (32 bits integer) as an IEEE single or double precision format

Support NaN.

Parameters:
  • val_float (float) – float value to convert
  • double (bool) – set to encode as a 64 bits double precision, default is 32 bits single (optional)
Returns:

IEEE 32 bits (single precision) as Python int

Return type:

int

Misc functions

pyModbusTCP.utils.crc16(frame)

Compute CRC16

Parameters:frame (str (Python2) or class bytes (Python3)) – frame
Returns:CRC16
Return type:int