Skip to content

Commit

Permalink
fix modbus function
Browse files Browse the repository at this point in the history
  • Loading branch information
pszafer committed Sep 20, 2024
1 parent 09d4a3f commit 517cba6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion boneio/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,33 @@ def _pymodbus_connect(self) -> bool:
_LOGGER.error(exception_error)
return False

async def read_single_register(
self, unit: int, address: int, 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 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()

async def read_multiple_registers(
self, unit: int, address: int, count: int = 2, method: str = "input"
) -> ModbusResponse:
"""Read single or multiple register(s)."""
"""Call sync. pymodbus."""
async with self._lock:
if not self._pymodbus_connect():
_LOGGER.error("Can't connect to Modbus.")
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.7.2dev3"
__version__ = "0.7.2dev4"

0 comments on commit 517cba6

Please sign in to comment.