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

Raising ModbusException in modbus.Slave.handle_request doesn't work #166

Open
ErikTempelaarVO opened this issue May 11, 2022 · 0 comments
Open

Comments

@ErikTempelaarVO
Copy link

ErikTempelaarVO commented May 11, 2022

To test the robustness of our modbus client we'd like the server to throw some errors on requests.

Attempting to do so via hook "modbus.Slave.handle_request" doesn't work properly, because in the except-clause the function code is expected to be available, but it isn't.

retval = call_hooks("modbus.Slave.handle_request", (self, request_pdu))
if retval is not None:
return retval
# get the function code
(function_code, ) = struct.unpack(">B", request_pdu[0:1])

except ModbusError as excpt:
LOGGER.debug(str(excpt))
call_hooks("modbus.Slave.on_exception", (self, function_code, excpt))
return struct.pack(">BB", function_code+128, excpt.get_exception_code())

Fix seems trivial, by reordering the code and fetching the function code before calling said hooks.

Hooking more specifically also works fine:

hooks.install_hook("modbus.Slave.handle_read_input_registers_request", self.on_modbus_slave_handle_request)
hooks.install_hook("modbus.Slave.handle_read_holding_registers_request", self.on_modbus_slave_handle_request)

Does it make sense to request this to be changed? Could open a PR if that's helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant