Skip to content

Commit 7d9c810

Browse files
fix bug of wrong response on single coil write, see #21
1 parent 45338c0 commit 7d9c810

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

umodbus/functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ def validate_resp_data(data: bytes,
248248
if function_code in [Const.WRITE_SINGLE_COIL, Const.WRITE_SINGLE_REGISTER]:
249249
resp_addr, resp_value = struct.unpack(fmt, data)
250250

251+
# if bool(True) or int(1) is used as "output_value" of
252+
# "write_single_coil" it will be internally converted to int(0xFF00),
253+
# see Modbus specification, which is actually int(65280).
254+
# Due to the non binary, but real value comparison of "value" and
255+
# "resp_value", it would never match without the next two lines
256+
# see #21
257+
if function_code == Const.WRITE_SINGLE_COIL:
258+
resp_value = bool(resp_value)
259+
value = bool(value)
260+
251261
if (address == resp_addr) and (value == resp_value):
252262
return True
253263
elif function_code in [Const.WRITE_MULTIPLE_COILS,

0 commit comments

Comments
 (0)