gijzelaerr / python-snap7

A Python wrapper for the snap7 PLC communication library
http://python-snap7.readthedocs.org/
MIT License
650 stars 245 forks source link

Error in util.Set_String and util.Get_String #479

Open PoitrasJ opened 10 months ago

PoitrasJ commented 10 months ago

When reading the first Byte in the ByteArray, which contains the maximum length of the string, the Hexadecimal value is sometimes in a format that Python will not cast to Integer : b'\ff' will stay the type "Bytes" and the slice access fails.

this Section in set_string

    # fill the rest with empty space
    for r in range(i + 1, (bytearray_[byte_index]) - 2):
        bytearray_[byte_index + 2 + r] = ord(' ')

this Section in get_string

    str_length = int(bytearray_[byte_index + 1])
    max_string_size = int(bytearray_[byte_index])

The solution i came up is to evaluate the value using ord :

    # fill the rest with empty space
    for r in range(i + 1, ord(bytearray_[byte_index]) - 2):
        bytearray_[byte_index + 2 + r] = ord(' ')
    str_length = ord(bytearray_[byte_index + 1])
    max_string_size = ord(bytearray_[byte_index])
gijzelaerr commented 3 months ago

Can you supply a unit test or string to break the current behaviour?