bbartling
bbartling

Reputation: 3502

Modbus client script read a sensor value

I have a Modbus server setup on a LAN with IP address 192.168.0.111 and Modbus map is this snip below where I am trying read the sensor highlighted yellow:

enter image description here

Can someone give me a tip on how to run a Modbus client script and read the sensor value?

from pymodbus.client import ModbusTcpClient

client = ModbusTcpClient('192.168.0.111')
result = client.read_coils(30500,1)
print(result.bits[0])

client.close()

This will error out:

print(result.bits[0])
AttributeError: 'ExceptionResponse' object has no attribute 'bits'

Experimenting a bit and changing the print to print(result) this will return without an exception

Exception Response(129, 1, IllegalFunction)

Upvotes: 0

Views: 785

Answers (1)

bbartling
bbartling

Reputation: 3502

In the chat for this question...this overstackoverflow question was referenced. Complete answer:

from pymodbus.client import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder


client = ModbusTcpClient('192.168.0.111')
result = client.read_input_registers(500,2,units=1)
print(result.registers)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big)
print(decoder.decode_32bit_float())
client.close()

Upvotes: 1

Related Questions