Skip to content

Commit

Permalink
feat: Add modbus response pdu validation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Feb 13, 2025
1 parent ee5b16a commit c569217
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions custom_components/solarman/pysolarman/pysolarman.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ async def execute(self, code, **kwargs):
elif "bits" in kwargs and not isinstance(kwargs["bits"], list):
kwargs["bits"] = [kwargs["bits"]]
_, pdu = self._client_framer.processIncomingFrame(await self._get_response(self._server_framer.buildFrame(self._server_decoder.lookup.get(code)(dev_id = self.slave, transaction_id = randint(0, 65535), **kwargs))))
if pdu is None:
raise FrameError(f"Invalid modbus response received")
if pdu.function_code != code:
raise FrameError(f"Incorrect response w/ function code {pdu.function_code} instead of {code} received")
if FUNCTION_CODE.READ_HOLDING_REGISTERS <= code <= FUNCTION_CODE.READ_INPUT_REGISTERS:
return pdu.registers
if FUNCTION_CODE.READ_COILS <= code <= FUNCTION_CODE.READ_DISCRETE_INPUTS:
Expand Down

0 comments on commit c569217

Please sign in to comment.