pyModBus:检查线圈是真还是假

pyModBus : check if coil is True or False

我正在尝试学习如何通过 python ModBus 模块将值引入 PLC,我目前正在尝试做的只是读取线圈 1 的值以检查其是否 Truefalse 所以我使用

order_ready = client.read_coils(0, 1)
print(order_ready)

我得到这个作为响应 ReadBitResponse(8) 我怎样才能从读取线圈中得到“True”值

您可以使用 bits 属性 从响应 ReadCoilResponse 访问各个线圈。可以找到有关响应的更多信息 here

order_ready = client.read_coils(0, 1)
if not order_ready.isError():
     #response.bits would return a list (multiple of 8) of booleans each bit representing the output of one coils
    # In your case accessing 1st element should give the actual value
    order_ready = order_ready.bits[0]
else:
     # Handle error