Skip to content

Commit

Permalink
fix: Workaround for modbus tcp responses w/o address
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 9, 2024
1 parent 2b00be4 commit 9c802f3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, address, serial, port, mb_slave_id):
self._passthrough = False

async def _tcp_send_receive_frame(self, mb_request_frame):
return mb_response_adu if not len(mb_response_adu := await self._send_receive_v5_frame(mb_request_frame)) == 10 else mb_response_adu + b'\x00\x01'
return mb_response_adu if (l := len(mb_response_adu := await self._send_receive_v5_frame(mb_request_frame))) and not 8 <= l <= 10 else mb_response_adu + b_(b'\x00\x01', 10 - l)

async def _tcp_parse_response_adu(self, mb_request_frame):
return parse_response_adu(await self._tcp_send_receive_frame(mb_request_frame), mb_request_frame)
Expand Down
3 changes: 3 additions & 0 deletions custom_components/solarman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def is_ethernet_frame(frame):
return int.from_bytes(frame[5:6], byteorder = "big") == len(frame[6:])
return False

def b_(suffix, count):
return b'\x00' * count + suffix

def format_exception(e):
return re.sub(r"\s+", " ", f"{type(e).__name__}{f': {e}' if f'{e}' else ''}")

Expand Down

0 comments on commit 9c802f3

Please sign in to comment.