Skip to content

Commit ff47adc

Browse files
authored
fix set_string (#368)
* add non-ascii check and test fix and add doc for set_string() fix possible out of range error in set_string() fix doc in get_string() * drop python 3.6 versions in checks update docs * correct CIBW Build to 3.7 * add non-ascii check and test fix and add doc for set_string() fix possible out of range error in set_string() fix doc in get_string()
1 parent 1aeefec commit ff47adc

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

snap7/util.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,33 +422,42 @@ def set_string(bytearray_: bytearray, byte_index: int, value: str, max_size: int
422422
423423
Raises:
424424
:obj:`TypeError`: if the `value` is not a :obj:`str`.
425-
:obj:`ValueError`: if the length of the `value` is larger than the `max_size`.
425+
:obj:`ValueError`: if the length of the `value` is larger than the `max_size`
426+
or 'max_size' is greater than 255 or 'value' contains non-ascii characters.
426427
427428
Examples:
428429
>>> data = bytearray(20)
429430
>>> snap7.util.set_string(data, 0, "hello world", 255)
430431
>>> data
431-
bytearray(b'\\x00\\x0bhello world\\x00\\x00\\x00\\x00\\x00\\x00\\x00')
432+
bytearray(b'\\xff\\x0bhello world\\x00\\x00\\x00\\x00\\x00\\x00\\x00')
432433
"""
433434
if not isinstance(value, str):
434435
raise TypeError(f"Value value:{value} is not from Type string")
435436

436437
if max_size > 255:
437438
raise ValueError(f'max_size: {max_size} > max. allowed 255 chars')
439+
if not value.isascii():
440+
raise ValueError("Value contains non-ascii values, which is not compatible with PLC Type STRING."
441+
"Check encoding of value or try set_wstring() (utf-16 encoding needed).")
438442
size = len(value)
439443
# FAIL HARD WHEN trying to write too much data into PLC
440444
if size > max_size:
441445
raise ValueError(f'size {size} > max_size {max_size} {value}')
446+
447+
# set max string size
448+
bytearray_[byte_index] = max_size
449+
442450
# set len count on first position
443451
bytearray_[byte_index + 1] = len(value)
444452

445453
i = 0
454+
446455
# fill array which chr integers
447456
for i, c in enumerate(value):
448457
bytearray_[byte_index + 2 + i] = ord(c)
449458

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

454463

@@ -468,7 +477,7 @@ def get_string(bytearray_: bytearray, byte_index: int) -> str:
468477
469478
Examples:
470479
>>> data = bytearray([254, len("hello world")] + [ord(letter) for letter in "hello world"])
471-
>>> snap7.util.get_string(data, 0, 255)
480+
>>> snap7.util.get_string(data, 0)
472481
'hello world'
473482
"""
474483

test/test_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ def test_write_string(self):
215215
pass
216216
# value should still be empty
217217
self.assertEqual(row['NAME'], '')
218+
try:
219+
row['NAME'] = 'TrÖt'
220+
except ValueError:
221+
pass
218222

219223
def test_get_int(self):
220224
test_array = bytearray(_bytearray)

0 commit comments

Comments
 (0)