pymodbus3pymodbus

pyModBus : check if coil is True or False


I am trying to learn how to introduce values to a PLC trough python ModBus module what I'm currently trying to do is just to read the value of coil 1 to check if its True or false so I am using

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

and I get this as response ReadBitResponse(8) how can I get a "True" value from reading a coil


Solution

  • You can access the individual coils from the response ReadCoilResponse using the bits property . More on response could be found 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