Skip to content

Commit

Permalink
change modbus check_availability command
Browse files Browse the repository at this point in the history
  • Loading branch information
pszafer committed Sep 26, 2024
1 parent 4059607 commit 332e760
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
44 changes: 22 additions & 22 deletions boneio/modbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,26 @@ def _pymodbus_connect(self) -> bool:
_LOGGER.error(exception_error)
return False

async def read_single_register(
async def read_and_decode(
self,
unit: int | str,
address: int,
payload_type: str,
count: int = 2,
method: str = "input",
) -> float | None:
"""Call sync. pymodbus."""
async with self._lock:
if not self._pymodbus_connect():
_LOGGER.error("Can't connect to Modbus address %s.", address)
return None
kwargs = {"unit": unit, "count": count} if unit else {}
try:
result: ReadInputRegistersResponse = self._read_methods[method](
address, **kwargs
)
except (ModbusException, struct.error) as exception_error:
_LOGGER.error(exception_error)
return None
if not hasattr(result, REGISTERS):
_LOGGER.error(str(result))
return None
return BinaryPayloadDecoder.fromRegisters(
result.registers, byteorder=Endian.Big, wordorder=Endian.Big
).decode_32bit_float()
result = await self.read_registers(
unit=unit, address=address, count=count, method=method
)
if not result or result.isError():
return None
decoded_value = self.decode_value(
payload=result.registers, value_type=payload_type
)
return decoded_value

async def read_multiple_registers(
async def read_registers(
self,
unit: int | str, # device address
address: int, # modbus register address
Expand All @@ -142,10 +134,18 @@ async def read_multiple_registers(
return None
kwargs = {"unit": unit, "count": count} if unit else {}
try:
result: ReadInputRegistersResponse = self._read_methods[method](
read_method = self._read_methods[method]
_LOGGER.debug(
"Multiple reading %s registers from %s with method %s from device %s.",
count,
address,
method,
unit,
)
result: ReadInputRegistersResponse = read_method(
address, **kwargs
)
except ModbusException as exception_error:
except (ModbusException, struct.error) as exception_error:
_LOGGER.error(exception_error)
return None
if not hasattr(result, REGISTERS):
Expand Down
4 changes: 2 additions & 2 deletions boneio/modbus/modbuscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def check_connection(self) -> bool:
f"Checking connection {self._device_address}, address {address}."
)
count = 1 if value_type == "S_WORD" or value_type == "U_WORD" else 2
value = await self._modbus.read_multiple_registers(
value = await self._modbus.read_registers(
unit=self._device_address,
address=address,
count=count,
Expand Down Expand Up @@ -180,7 +180,7 @@ async def async_run_modbus_get(
parity=parity,
)
count = 1 if value_type == "S_WORD" or value_type == "U_WORD" else 2
value = await _modbus.read_multiple_registers(
value = await _modbus.read_registers(
unit=device_address,
address=register_address,
count=count,
Expand Down
7 changes: 5 additions & 2 deletions boneio/modbus/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ async def check_availability(self) -> None:
register_method = first_register_base.get("register_type", "input")
# Let's try fetch register 2 times in case something wrong with initial packet.
for _ in [0, 1]:
register = await self._modbus.read_single_register(
register = await self._modbus.read_and_decode(
unit=self._address,
address=first_register_base[REGISTERS][0][ADDRESS],
method=register_method,
payload_type=first_register_base[REGISTERS][0].get(
"value_type", "FP32"
),
)
if register is not None:
self._discovery_sent = (
Expand All @@ -144,7 +147,7 @@ async def async_update(self, time: datetime) -> Optional[float]:
update_interval = self._update_interval.total_in_seconds
await self.check_availability()
for data in self._db[REGISTERS_BASE]:
values = await self._modbus.read_multiple_registers(
values = await self._modbus.read_registers(
unit=self._address,
address=data[BASE],
count=data[LENGTH],
Expand Down
2 changes: 1 addition & 1 deletion boneio/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# flake8: noqa
__version__ = "0.8.0dev5"
__version__ = "0.8.0dev6"

0 comments on commit 332e760

Please sign in to comment.