pymodbus

Communicate with Elfin EW11 RS485 wifi converter


I am trying to communicate with an electrical meter (Orno WE-517) that has an RS485 modbus interface. This serial interface is connected to an Elfin EW11 modbus to Wifi converter.

Using pymodbus, I can connect to the adapter but I don't manage to read anything from the meter. Here is my code:

from pymodbus.client.sync import ModbusTcpClient

from pprint import pprint

import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
          '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()

log.setLevel(logging.DEBUG)

client = ModbusTcpClient('192.168.0.18', 502)
if not client.connect():
    print('Error connecting')
    exit()

print('holding register')
result = client.read_holding_registers(28,1)
response = client.execute(result)
pprint(vars(response))

client.close()

Here is the output of the script:

python3 modBus.py

2021-12-30 10:46:44,112 MainThread      DEBUG    sync           :216      Connection to Modbus server established. Socket ('192.168.0.10', 47763)
holding register
2021-12-30 10:46:44,113 MainThread      DEBUG    transaction    :140      Current transaction state - IDLE
2021-12-30 10:46:44,114 MainThread      DEBUG    transaction    :145      Running transaction 1
2021-12-30 10:46:44,114 MainThread      DEBUG    transaction    :273      SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x1c 0x0 0x1
2021-12-30 10:46:44,115 MainThread      DEBUG    sync           :76       New Transaction state 'SENDING'
2021-12-30 10:46:44,115 MainThread      DEBUG    transaction    :287      Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:44,216 MainThread      DEBUG    transaction    :375      Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
2021-12-30 10:46:44,217 MainThread      DEBUG    transaction    :297      RECV: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread      DEBUG    socket_framer  :147      Processing: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread      DEBUG    factory        :266      Factory Response[ReadHoldingRegistersResponse: 3]
2021-12-30 10:46:44,219 MainThread      DEBUG    transaction    :454      Adding transaction 1
2021-12-30 10:46:44,220 MainThread      DEBUG    transaction    :465      Getting transaction 1
2021-12-30 10:46:44,220 MainThread      DEBUG    transaction    :224      Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
2021-12-30 10:46:44,221 MainThread      DEBUG    transaction    :140      Current transaction state - TRANSACTION_COMPLETE
2021-12-30 10:46:44,221 MainThread      DEBUG    transaction    :145      Running transaction 2
2021-12-30 10:46:44,222 MainThread      DEBUG    transaction    :273      SEND: 0x0 0x2 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,222 MainThread      DEBUG    sync           :76       New Transaction state 'SENDING'
2021-12-30 10:46:44,223 MainThread      DEBUG    transaction    :287      Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:47,227 MainThread      DEBUG    transaction    :303      Transaction failed. (Modbus Error: [Invalid Message] No response received, expected at least 8 bytes (0 received))
2021-12-30 10:46:47,228 MainThread      DEBUG    socket_framer  :147      Processing:
2021-12-30 10:46:47,228 MainThread      DEBUG    transaction    :465      Getting transaction 2
2021-12-30 10:46:47,229 MainThread      DEBUG    transaction    :224      Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
{'fcode': 3,
 'message': '[Input/Output] Modbus Error: [Invalid Message] No response '
            'received, expected at least 8 bytes (0 received)',
 'string': '[Input/Output] Modbus Error: [Invalid Message] No response '
           'received, expected at least 8 bytes (0 received)'}

As you can see, pymodbus manages to connect, send a request and receive a response. But I can't find the way to do something from that response.

Would anyone have an idea to help me?

In advance, thanks a lot for your help :)


Solution

  • Persevering in the look for a solution, I discovered that the values are actually float. This lead me to the following solution:

    from pymodbus.client.sync import ModbusTcpClient
    from pymodbus.payload import BinaryPayloadDecoder
    
    client = ModbusTcpClient('192.168.0.18', 502)
    if not client.connect():
        print('Error connecting')
        exit()
    
    result = client.read_holding_registers(0x1C, 2)
    decoder = BinaryPayloadDecoder.fromRegisters(result.registers, '>', '>')
    print(decoder.decode_32bit_float())
    
    client.close()
    

    The solution was really to find the various methods of BinaryPayloadDecoder.