Skip to content

Commit

Permalink
feat: Improved modbus bogus frame detection
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Jul 30, 2024
1 parent 2a10012 commit 096c9cc
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import concurrent.futures

from datetime import datetime

from umodbus.client.serial import rtu
from umodbus.client.serial.redundancy_check import get_crc
from pysolarmanv5 import PySolarmanV5Async, V5FrameError, NoSocketAvailableError
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo, format_mac
from homeassistant.helpers.update_coordinator import UpdateFailed
Expand Down Expand Up @@ -88,16 +90,15 @@ async def _send_receive_v5_frame(self, data_logging_stick_frame):

async def _get_modbus_response(self, mb_request_frame):
"""
Overridden to catch the 00 00
Overridden to catch the trailing 00 00
"""
mb_response_frame = await self._send_receive_modbus_frame(mb_request_frame)
try:
self.log.debug("[%s] MBRP: %s", self.serial, mb_response_frame.hex(" "))
modbus_values = rtu.parse_response_adu(mb_response_frame, mb_request_frame)
except struct.error as e:
if not 'requires a buffer of' in e.args[0] or (mb_response_frame[-2] != 0 and mb_response_frame[-1] != 0):
if not 'requires a buffer of' in e.args[0] or get_crc(mb_response_frame[:-4]) != mb_response_frame[-4:-2]:
raise e
modbus_values = rtu.parse_response_adu(mb_response_frame[0:-2], mb_request_frame)
modbus_values = rtu.parse_response_adu(mb_response_frame[:-2], mb_request_frame)
return modbus_values

async def async_connect(self) -> None:
Expand Down

0 comments on commit 096c9cc

Please sign in to comment.