-
Notifications
You must be signed in to change notification settings - Fork 35
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
Add support for modbus over serial (RTU) #41
base: master
Are you sure you want to change the base?
Conversation
This allows a top-level "slave" entry in the config to set the slave address globally (defaulting to 1 as before), but also allows overriding the slave address for individual registers (which allows communicating with multiple slaves on the same bus).
@@ -31,6 +31,7 @@ def __init__(self, hostname, port, username, password, config_file, mqtt_topic_p | |||
self.prefix = mqtt_topic_prefix | |||
self.address_offset = self.config.get('address_offset', 0) | |||
self.registers = self.config['registers'] | |||
self.default_slave = self.config.get('slave', 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making these safer - I like it
if mask == 0xFFFF: | ||
self._mb.write_register(addr, value, unit=0x01) | ||
self._mb.write_register(addr, value, unit=slave) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for fixing up the hard-coded values
@iconnor I've pushed up some commits to a fork of this branch to fix up the tests: master...3devo-serial-modbus I haven't had time to 100% test and finish it off yet though. I also really don't like the "slave" terminology, and I think it's daft that upstream pymodbus is moving from "unit" to "slave". |
How does it look like with merge? |
This PR:
I'm marking this as a draft pullrequest, since I do still have some doubts about the implementation:
modbus_interface.__init__()
separately. I'm not sure if this approach really scales well, maybe some other method of passing config as a dict or so should be considered.modbus_interface.__init__()
has default values for its parameters, but AFAICS these are never used, becausemqtt_interface.connect_modbus()
always passes values (and has its own defaults for values missing in the config file).variant
, which seems a bit weird to me, but given the only other variant,sungrow
is a variation on ModbusTcpClient, it makes sense like this, but does feel a bit weird. Maybe this requires a bit more thought.For now, this code works well enough for my usecase, so I'll probably not put (much) more work in this (I could make some more updates if there is specific feedback), but I wanted to at least share this code in case it is helpful for others as-is already.