Skip to content

Commit

Permalink
Merge pull request favalex#6 from RJ/write_single_register
Browse files Browse the repository at this point in the history
Use function `0x6` for single register writes
  • Loading branch information
favalex authored Dec 5, 2020
2 parents d41f2b0 + 65f438b commit cfbf1d6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ BINARY_TYPE = <pack format>
VALUE = <number>
The value to be written to the register. If not present, the register will be read instead.

If only one register is written to, the modbus function ``6 (0x6)``, "write single register" is used.
If multiple registers are written to, the modbus function ``16 (0x10)``, "write multiple registers" is used.

EXAMPLES
========

Expand Down
5 changes: 4 additions & 1 deletion modbus_cli/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ def write_registers_send(self, modbus):

words.extend([h << 8 | l for h, l in grouper(struct.pack(pack_type, value), 2)])

message = modbus.protocol.write_multiple_registers(modbus.slave_id, self.address(), words)
if len(self.values_to_write) == 1:
message = modbus.protocol.write_single_register(modbus.slave_id, self.address(), words[0])
else:
message = modbus.protocol.write_multiple_registers(modbus.slave_id, self.address(), words)

logging.debug('→ < %s >', dump(message))

Expand Down
2 changes: 1 addition & 1 deletion modbus_cli/modbus_rtu.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def receive(self, request):
response += self.connection.read(1)
count = 2 + response[-1]
response += self.connection.read(count)
elif function in (5, 15):
elif function in (5, 6, 15):
# Function with fixed size
response += self.connection.read(6)
elif function & 0x80:
Expand Down

0 comments on commit cfbf1d6

Please sign in to comment.