pythonraspberry-piplcmodbus-tcppymodbus

How do you read strings from a data register using pymodbus?


I'm trying to use my raspberry pi as a client for modbus communication between it and a S7-1200 Siemens plc. I got it to read integers from holding register, but not strings. When I try to search for a solution online on how to read strings from the plc holding register nothing useful comes up. I tried a program online that is supposed to be able to do it but I just keep getting an error every time I run it. Can anyone help? I have the code for the program and the error message below.

from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian
from pymodbus.compat import iteritems

if __name__ == '__main__':
    client = ModbusClient("192.168.0.1", port=502, auto_open=True)
    client.connect()
    result  = client.read_holding_registers(1, 1, unit=1)
    print("Result : ",result)
    decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big, wordorder=Endian.Big)

    decoded = {
        'name': decoder.decode_string(10).decode(),
    }



    for name, value in iteritems(decoded):
        print ("%s\t" % name, value)
    client.close()

Error Message

Data Register

PLC MB Server Block Setup


Solution

  • you can't take strings value from modbus communication