From 822cdebce61227989d17f36fa6e93a2f10bdc301 Mon Sep 17 00:00:00 2001 From: Benjamin Aeby <58775333+b-aeby@users.noreply.github.com> Date: Mon, 25 Sep 2023 11:39:05 +0200 Subject: [PATCH] Update ads.py Workaround for encoding issue. I'm reading an array of structure with 'cp-1252' encoded strings. --- pyads/ads.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pyads/ads.py b/pyads/ads.py index 9ca4f401..c5e9d53f 100644 --- a/pyads/ads.py +++ b/pyads/ads.py @@ -320,11 +320,18 @@ def dict_from_bytes( if plc_datatype == PLCTYPE_STRING: if str_len is None: str_len = PLC_DEFAULT_STRING_SIZE - var_array.append( - bytearray(byte_list[index: (index + (str_len + 1))]) - .partition(b"\0")[0] - .decode("utf-8") - ) + try: + var_array.append( + bytearray(byte_list[index: (index + (str_len + 1))]) + .partition(b"\0")[0] + .decode("utf-8") + ) + except: + var_array.append( + bytearray(byte_list[index: (index + (str_len + 1))]) + .partition(b"\0")[0] + .decode("cp-1252") + ) index += str_len + 1 elif plc_datatype == PLCTYPE_WSTRING: if str_len is None: # if no str_len is given use default size