I am using python snap7 to communicate data between PLCs and am having trouble reading from an integer. The snap7 get_int function works with the following parameters (byteArray, byteIndex)
. However, when I pass the bytearray into the function it gives me the following error:
data[1] = data[1] & 0xff
IndexError: bytearray index out of range
I was wondering what is resulting in the byte index of being out of range, here is the get_int function as a part of the library:
def get_int(bytearray_: bytearray, byte_index: int) -> int:
"""Get int value from bytearray.
Notes:
Datatype `int` in the PLC is represented in two bytes
Args:
bytearray_: buffer to read from.
byte_index: byte index to start reading from.
Returns:
Value read.
Examples:
>>> data = bytearray([0, 255])
>>> snap7.util.get_int(data, 0)
255
"""
data = bytearray_[byte_index:byte_index + 2]
data[1] = data[1] & 0xff
data[0] = data[0] & 0xff
packed = struct.pack('2B', *data)
value = struct.unpack('>h', packed)[0]
return value
Also, here is the snap7 docs:
https://python-snap7.readthedocs.io/en/latest/_modules/snap7/util.html