Skip to content

Commit 0fca338

Browse files
authored
Fix utils (#444)
1 parent 36676ab commit 0fca338

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

snap7/util.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,31 +449,31 @@ def set_fstring(bytearray_: bytearray, byte_index: int, value: str, max_length:
449449
bytearray_[byte_index + r] = ord(' ')
450450

451451

452-
def set_string(bytearray_: bytearray, byte_index: int, value: str, max_size: int = 255):
452+
def set_string(bytearray_: bytearray, byte_index: int, value: str, max_size: int = 254):
453453
"""Set string value
454454
455455
Args:
456456
bytearray_: buffer to write to.
457457
byte_index: byte index to start writing from.
458458
value: string to write.
459-
max_size: maximum possible string size, max. 255 as default.
459+
max_size: maximum possible string size, max. 254 as default.
460460
461461
Raises:
462462
:obj:`TypeError`: if the `value` is not a :obj:`str`.
463463
:obj:`ValueError`: if the length of the `value` is larger than the `max_size`
464-
or 'max_size' is greater than 255 or 'value' contains non-ascii characters.
464+
or 'max_size' is greater than 254 or 'value' contains non-ascii characters.
465465
466466
Examples:
467467
>>> data = bytearray(20)
468-
>>> snap7.util.set_string(data, 0, "hello world", 255)
468+
>>> snap7.util.set_string(data, 0, "hello world", 254)
469469
>>> data
470470
bytearray(b'\\xff\\x0bhello world\\x00\\x00\\x00\\x00\\x00\\x00\\x00')
471471
"""
472472
if not isinstance(value, str):
473473
raise TypeError(f"Value value:{value} is not from Type string")
474474

475-
if max_size > 255:
476-
raise ValueError(f'max_size: {max_size} > max. allowed 255 chars')
475+
if max_size > 254:
476+
raise ValueError(f'max_size: {max_size} > max. allowed 254 chars')
477477
if not value.isascii():
478478
raise ValueError("Value contains non-ascii values, which is not compatible with PLC Type STRING."
479479
"Check encoding of value or try set_wstring() (utf-16 encoding needed).")
@@ -553,7 +553,7 @@ def get_string(bytearray_: bytearray, byte_index: int) -> str:
553553
str_length = int(bytearray_[byte_index + 1])
554554
max_string_size = int(bytearray_[byte_index])
555555

556-
if str_length > max_string_size or max_string_size > 255:
556+
if str_length > max_string_size or max_string_size > 254:
557557
logger.error("The string is too big for the size encountered in specification")
558558
logger.error("WRONG SIZED STRING ENCOUNTERED")
559559
raise TypeError("String contains {} chars, but max. {} chars are expected or is larger than 254."
@@ -1038,9 +1038,7 @@ def get_lreal(bytearray_: bytearray, byte_index: int) -> float:
10381038
>>> snap7.util.get_lreal(data, 0)
10391039
12345.12345
10401040
"""
1041-
x = bytearray_[byte_index:byte_index + 8]
1042-
lreal = struct.unpack('>d', struct.pack('8B', *x))[0]
1043-
return lreal
1041+
return struct.unpack_from(">d", bytearray_, offset=byte_index)[0]
10441042

10451043

10461044
def set_lreal(bytearray_: bytearray, byte_index: int, lreal: float) -> bytearray:
@@ -1067,8 +1065,7 @@ def set_lreal(bytearray_: bytearray, byte_index: int, lreal: float) -> bytearray
10671065
10681066
"""
10691067
lreal = float(lreal)
1070-
_bytes = struct.unpack('8B', struct.pack('>d', lreal))
1071-
bytearray_[byte_index] = _bytes[0]
1068+
struct.pack_into(">d", bytearray_, byte_index, lreal)
10721069
return bytearray_
10731070

10741071

0 commit comments

Comments
 (0)