Skip to content

Commit

Permalink
Fix byte count when for WriteMultipleCoils (#105)
Browse files Browse the repository at this point in the history
For full bytes, the Byte Count field was calculated too high:
    8 // 8 + 1 = 2
while only one byte is needed to represent 8 coils.

Offset the values length before the truncating division to correctly
round to whole bytes.  This is easier than what the standard
formulates:
    N = Quantity of Outputs / 8, if the
    remainder is different of 0 ==> N = N+1

Note that the actual data bytes were already handled with the correct
length.  So in case of 8 coils, uModbus would send a Byte Count of 2,
but only one data byte before the checksum.
  • Loading branch information
acolomb committed Aug 27, 2020
1 parent 63c7679 commit b928ff4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions umodbus/functions.py
Expand Up @@ -1254,8 +1254,8 @@ class WriteMultipleCoils(ModbusFunction):
================ ===============
Function code 1
Starting Address 2
Byte count 1
Quantity 2
Byte count 1
Value n
================ ===============
Expand Down Expand Up @@ -1321,7 +1321,7 @@ def request_pdu(self):

fmt = '>BHHB' + 'B' * len(bytes_)
return struct.pack(fmt, self.function_code, self.starting_address,
len(self.values), (len(self.values) // 8) + 1,
len(self.values), (len(self.values) + 7) // 8,
*bytes_)

@classmethod
Expand Down

0 comments on commit b928ff4

Please sign in to comment.