Python: Codesamples

Read 8 Discrete Inputs from Modbus-TCP Server – Server available at Port 502 (IP-Address 190.172.268.100) – Starting Address “1”, Number of Inputs to Read: “8” (Notice that the Starting address might be shifted by “1”. In this example we are reading 8 Inputs, the first is Register “1” (Addressed with “0”)

modbusClient = ModbusClient(‘190.172.168.100’, 502)
modbusClient.Connect()
discreteInputs = modbusClient.ReadDiscreteInputs(0, 8)
print (discreteInputs)
modbusClient.close()


 

Read 8 Inputs Registers from Modbus-TCP Server – Server available at Port 502 (IP-Address 190.172.268.100) – Starting Address “1”, Number of Registers to Read: “8” (Notice that the Starting address might be shifted by “1”. In this example we are reading 8 Registers, the first is Register “1” (Addressed with “0”)

modbusClient = ModbusClient(‘190.172.168.100’, 502)
modbusClient.Connect()
inputRegisters = modbusClient.ReadInputRegisters(0, 8)
print (inputRegisters)
modbusClient.close()


 

Write a single coil to the Server – Coil number “1”

modbusClient = ModbusClient(‘190.172.168.100’, 502)
modbusClient.Connect()
modbusClient.WriteSingleCoil(0, True)
modbusClient.close()


 

Write a float value to a Modbus TCP Server (Two Registers are required, since one Modbus Register has 16 Bit)
modbusClient = ModbusClient(‘190.172.168.100’, 502)
modbusClient.Connect()
modbusClient.WriteMultipleRegisters(0, ConvertFloatToTwoRegisters(3.141517))
modbusClient.close()


 

Read a Float Value from Modbus TCP Server (Two Registers are required, since one Modbus Register has 16 Bit
modbusClient = ModbusClient(‘190.172.168.100’, 502)
modbusClient.Connect()
holdingRegisters = ConvertRegistersToFloat(modbusClient.ReadHoldingRegisters(0, 2))
print (holdingRegisters)
modbusClient.close()