gijzelaerr / python-snap7

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

util.set_string doesn't set max string size byte #366

Closed swamper123 closed 2 years ago

swamper123 commented 2 years ago

In #291 was an error mentioned, which I have now as well.

https://github.com/gijzelaerr/python-snap7/blob/5b1fa247e6efab34b4af5fec77d5926293305d10/snap7/util.py#L414

util.set_string doesn't provide a max. char size (Byte 0) which leads to undefined behaviour on SPS side (seems that DB breaks partly).

Two changes are needed:

1) bytearray_[byte_index + 1] = max_size has to be added. 2) for r in range(i + 1, bytearray_[byte_index]): has to be changed to for r in range(i + 1, bytearray_[byte_index] - 2):, so that no out of range error appears.

This would fix the wrong behaviour, PR will income soon.