Skip to content

Commit

Permalink
Work around Goodwe bug returning wrong TCP response length
Browse files Browse the repository at this point in the history
  • Loading branch information
mletenay committed May 19, 2024
1 parent 95d4d2c commit a30ecfe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion goodwe/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,14 @@ def validate_modbus_tcp_response(data: bytes, cmd: int, offset: int, value: int)
logger.debug("Response is too short.")
return False
expected_length = int.from_bytes(data[4:6], byteorder='big', signed=False) + 6
if len(data) < expected_length:
# The weird expected_length != 12 is work around Goodwe bug answering wrong (hardcoded 6) length.
if len(data) < expected_length and expected_length != 12:
raise PartialResponseException(len(data), expected_length)

if data[7] == MODBUS_READ_CMD:
expected_length = data[8] + 9
if len(data) < expected_length:
raise PartialResponseException(len(data), expected_length)
if data[8] != value * 2:
logger.debug("Response has unexpected length: %d, expected %d.", data[8], value * 2)
return False
Expand Down
2 changes: 2 additions & 0 deletions tests/test_modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def test_create_modbus_tcp_multi_request(self):
def test_validate_modbus_tcp_read_response(self):
self.assert_tcp_response_ok('000100000007b4030445565345', 0x3, 310, 2)
self.assert_tcp_response_ok('000100000007b4030400000002', 0x3, 331, 2)
# technically illegal, but work around Goodwe bug
self.assert_tcp_response_ok('000100000006f703020000', 0x3, 47510, 1)
# length too short
self.assert_tcp_response_partial('000100000007b403040000', 0x03, 331, 2)
# failure code
Expand Down

0 comments on commit a30ecfe

Please sign in to comment.