Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open and close ModbusClient #1772

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/modules/common/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def __read_registers(self, read_register_method: Callable,
byteorder: Endian = Endian.Big,
wordorder: Endian = Endian.Big,
**kwargs):
if self.is_socket_open() is False:
self.connect()
try:
multi_request = isinstance(types, Iterable)
if not multi_request:
Expand All @@ -107,12 +109,15 @@ def divide_rounding_up(numerator: int, denominator: int):
ModbusDataType.FLOAT_16 else getattr(decoder, t.decoding_method)() for t in types]
return result if multi_request else result[0]
except pymodbus.exceptions.ConnectionException as e:
self.close()
e.args += (NO_CONNECTION.format(self.address, self.port),)
raise e
except pymodbus.exceptions.ModbusIOException as e:
self.close()
e.args += (NO_VALUES.format(self.address, self.port),)
raise e
except Exception as e:
self.close()
raise Exception(__name__+" "+str(type(e))+" " + str(e)) from e

@overload
Expand Down