Skip to content

Commit e2c1cba

Browse files
setters.py -> set_string() was limited to ascii 0-128 because of .isascii(). Siemens allows ascii 0-255.
1 parent e1d2b3d commit e2c1cba

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

snap7/util/setters.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def set_string(bytearray_: bytearray, byte_index: int, value: str, max_size: int
212212
Raises:
213213
:obj:`TypeError`: if the `value` is not a :obj:`str`.
214214
:obj:`ValueError`: if the length of the `value` is larger than the `max_size`
215-
or 'max_size' is greater than 254 or 'value' contains non-ascii characters.
215+
or 'max_size' is greater than 254 or 'value' contains ascii characters > 255.
216216
217217
Examples:
218218
>>> from snap7.util import set_string
@@ -226,11 +226,11 @@ def set_string(bytearray_: bytearray, byte_index: int, value: str, max_size: int
226226

227227
if max_size > 254:
228228
raise ValueError(f"max_size: {max_size} > max. allowed 254 chars")
229-
if not value.isascii():
230-
raise ValueError(
231-
"Value contains non-ascii values, which is not compatible with PLC Type STRING."
232-
"Check encoding of value or try set_wstring() (utf-16 encoding needed)."
233-
)
229+
230+
if any(ord(char) < 0 or ord(char) > 255 for char in value):
231+
raise ValueError("Value contains ascii values > 255, which is not compatible with PLC Type STRING."
232+
"Check encoding of value or try set_wstring() (utf-16 encoding needed).")
233+
234234
size = len(value)
235235
# FAIL HARD WHEN trying to write too much data into PLC
236236
if size > max_size:

tests/test_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ def test_write_string(self) -> None:
319319
pass
320320
# value should still be empty
321321
self.assertEqual(row["NAME"], "")
322+
row["NAME"] = "TrÖt"
323+
self.assertEqual(row["NAME"], "TrÖt")
322324
try:
323-
row["NAME"] = "TrÖt"
325+
row["NAME"] = "TrĪt"
324326
except ValueError:
325327
pass
326328

0 commit comments

Comments
 (0)