Search code examples
pythonmodbus-tcppymodbus

Read an holding register bit by bit Siemens


I need to read a Siemens 1200 holding register bit by bit with python, searching on the web I found PyModbus an excellent library, but with it I'm only able to read the holding register such an integer and not bit by bit.

This is the code:

from pymodbus.client.sync import ModbusTcpClient

client = ModbusTcpClient('x.y.z.w', port=xxx)


result = client.write_registers(1, [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]) # write some register

results = client.read_holding_registers(0, 50) # read the first fifty register like int
print("0-50", results.registers)
results = client.read_holding_registers(50, 50) # read the last fifty register like int
print("50-100:", results.registers)


client.close()

Someone have any hint?


Solution

  • from pymodbus.client.sync import ModbusTcpClient
    from pymodbus.payload import BinaryPayloadDecoder
    from pymodbus.constants import Endian
    
    CLP_MB = ModbusTcpClient(['IP'], port=502)
    
    adress_Tag=1
    adress_Tag=int(adress_Tag)
    size=50
    value = CLP.read_coils(adress_Tag, size, unit=0)
    #last part
    value_bit_0 = int(value.bits[0])
    value_bit_1 = int(value.bits[1])
    ..... 
    you can use a for loop to automation this last part