Skip to content

Commit

Permalink
Try-excepts attemps to write to modbus
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 5, 2023
1 parent 2971966 commit 9a095d4
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions python/lvmecp/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,29 @@ async def set(self, value: int | bool):
else:
raise ValueError(f"Invalid block mode {self.mode!r}.")

if self.client.connected:
resp = await func(self.address, value) # type: ignore
else:
async with self.modbus:
try:
if self.client.connected:
resp = await func(self.address, value) # type: ignore
else:
async with self.modbus:
resp = await func(self.address, value) # type: ignore

Check warning on line 170 in python/lvmecp/modbus.py

View check run for this annotation

Codecov / codecov/patch

python/lvmecp/modbus.py#L170

Added line #L170 was not covered by tests

if resp.function_code > 0x80:
msg = (
f"Invalid response for element "
f"{self.name!r}: 0x{resp.function_code:02X}."
)
if resp.function_code > 0x80:
msg = (

Check warning on line 173 in python/lvmecp/modbus.py

View check run for this annotation

Codecov / codecov/patch

python/lvmecp/modbus.py#L173

Added line #L173 was not covered by tests
f"Invalid response for element "
f"{self.name!r}: 0x{resp.function_code:02X}."
)
else:
return

if ntries >= MAX_RETRIES:
raise ValueError(msg)
except Exception as err:
msg = f"Error raised while setting {self.name!r}: {err}"

Check warning on line 181 in python/lvmecp/modbus.py

View check run for this annotation

Codecov / codecov/patch

python/lvmecp/modbus.py#L180-L181

Added lines #L180 - L181 were not covered by tests

warnings.warn(msg, ECPWarning)
await asyncio.sleep(0.5)
if ntries >= MAX_RETRIES:
raise ValueError(msg)

Check warning on line 184 in python/lvmecp/modbus.py

View check run for this annotation

Codecov / codecov/patch

python/lvmecp/modbus.py#L184

Added line #L184 was not covered by tests

warnings.warn(msg, ECPWarning)
await asyncio.sleep(0.5)

Check warning on line 187 in python/lvmecp/modbus.py

View check run for this annotation

Codecov / codecov/patch

python/lvmecp/modbus.py#L186-L187

Added lines #L186 - L187 were not covered by tests


class Modbus(dict[str, ModbusRegister]):
Expand Down

0 comments on commit 9a095d4

Please sign in to comment.