Skip to content

Commit

Permalink
Improve UTF-16 strings decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mletenay committed Jan 2, 2024
1 parent bf33ee6 commit 0301bfb
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion goodwe/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async def read_device_info(self):
self.model_name = response[22:32].decode("ascii").rstrip()
except:
print("No model name sent from the inverter.")
self.serial_number = response[6:22].decode("ascii")
self.serial_number = self._decode(response[6:22])
self.dsp1_version = read_unsigned_int(response, 66)
self.dsp2_version = read_unsigned_int(response, 68)
self.arm_version = read_unsigned_int(response, 70)
Expand Down
2 changes: 1 addition & 1 deletion goodwe/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async def read_device_info(self):
response = response.response_data()
self.firmware = self._decode(response[0:5]).rstrip()
self.model_name = self._decode(response[5:15]).rstrip()
self.serial_number = response[31:47].decode("ascii")
self.serial_number = self._decode(response[31:47])
self.software_version = self._decode(response[51:63])
try:
if len(self.firmware) >= 2:
Expand Down
2 changes: 1 addition & 1 deletion goodwe/et.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ async def read_device_info(self):
self.modbus_version = read_unsigned_int(response, 0)
self.rated_power = read_unsigned_int(response, 2)
self.ac_output_type = read_unsigned_int(response, 4) # 0: 1-phase, 1: 3-phase (4 wire), 2: 3-phase (3 wire)
self.serial_number = response[6:22].decode("ascii")
self.serial_number = self._decode(response[6:22])
self.model_name = self._decode(response[22:32])
self.dsp1_version = read_unsigned_int(response, 32)
self.dsp2_version = read_unsigned_int(response, 34)
Expand Down
2 changes: 1 addition & 1 deletion goodwe/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def _decode(data: bytes) -> str:
"""Decode the bytes to ascii string"""
try:
if any(x < 32 for x in data):
return data.hex()
return data.decode("utf-16be").rstrip().replace('\x00', '')
return data.decode("ascii").rstrip()
except ValueError:
return data.hex()
2 changes: 1 addition & 1 deletion tests/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def __init__(self, methodName='runTest'):

def test_GW6000_ES_20_device_info(self):
self.loop.run_until_complete(self.read_device_info())
self.assertEqual('00000000000000000000', self.model_name)
self.assertEqual('', self.model_name)
self.assertEqual('56000ESN00AW0000', self.serial_number)
self.assertEqual('0002000205', self.firmware)
self.assertEqual(0, self.dsp1_version)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_et.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ def __init__(self, methodName='runTest'):

def test_GW25K_ET_device_info(self):
self.loop.run_until_complete(self.read_device_info())
self.assertEqual('00000000000000000000', self.model_name)
self.assertEqual('', self.model_name)
self.assertEqual('9025KETT00000000', self.serial_number)
self.assertEqual(25000, self.rated_power)
self.assertEqual(0, self.modbus_version)
Expand All @@ -905,7 +905,7 @@ def test_GW25K_ET_device_info(self):
self.assertEqual(6017, self.dsp_svn_version)
self.assertEqual(8, self.arm_version)
self.assertEqual(362, self.arm_svn_version)
self.assertEqual('00300034003000360032002d', self.firmware)
self.assertEqual('04062-', self.firmware)
self.assertEqual('02020-08-S01', self.arm_firmware)

def test_GW25K_ET_runtime_data(self):
Expand Down

0 comments on commit 0301bfb

Please sign in to comment.