pyModbusTCP 读取零点 CN- 8031 /CT-121F DI 数字输入

from pyModbusTCP.client import ModbusClient # Modbus TCP Client
import  time

# TCP auto connect on modbus request, close after it
ModbusBMS = ModbusClient(host="192.168.1.100", port=502, unit_id=1, auto_open=True, auto_close=False) 
#
bits_addr=1
def ModbusTCP():
    global  bits_addr
    bits_value=ModbusBMS.read_discrete_inputs(bits_addr,1)
    print("Bit:",bits_addr,"=","%s" %bits_value) 
  
if __name__ == '__main__':
    while True:
        ModbusTCP()
        time.sleep(2)  
        bits_addr=bits_addr+1
        if bits_addr==16:
               bits_addr=0

你可能感兴趣的:(python,开发语言)