I'm trying to monitor the power usage of multiple devices and store that data in a database using Python. I am connecting my RPi 3 to the power meter via an Ethernet cable and using Modbus TCP. To use this I need a fixed IP which seems to have been set correctly but I'm really not sure. I installed the Pymodbus Python library through apt, pip and easy_install-none working.

Specs: RPi 3 using Jessie Lite, Pymodbus TCP, Power Meter (PM-3112), Python 2.7

Network Config Files:

/etc/dhcpcd.conf (end of file)

interface eth0
static ip_address=

/etc/network/interfaces (start of file)

auto    lo
iface   lo   inet loopback
iface   eth0   inet static

Python Script:

from pymodbus.client.sync import ModbusTcpClient as ModbusClient
import time
import ctypes

AveragingInterval = 2           
ESM_IP_ADDRESS=''      # connect to the ESM
modbus_client = ModbusClient(ESM_IP_ADDRESS)

def readESMInputRegisterInt32(Address):
    response = modbus_client.read_input_registers(Address,2)
    #response = modbus_client.read_holding_registers(Address,2)
    if (response is not None):
        #ReturnValue = ((response.getRegister(0) << 16) + response.getRegister(1))
        ReturnValue = int(str(((response.registers[0], 16) + response.registers[1], 16)))
        if (ReturnValue & 0x80000000):              # MSB set so negative
            ReturnValue = -0x100000000 + ReturnValue
        return ReturnValue                          #((response.getRegister(0) << 16) + response.getRegister(1))
        return -1

def readESMkW2():
    return readESMInputRegisterInt32(0x1215)

kWSum = 0

while True: 
    if modbus_client.connect():
        print("Port open")
    kW = readESMkW2()
    kWSum = kWSum + kW

I tried both read_input_registers and read_holding_registers

registers[] and getRegister() with the same error

Registers used were obtained from the spec sheet


modbus/client/common.py line 120 (read_input_registers)

modbus/client/sync.py line 82 (execute)

I think it's to do with the static IP but after following multiple tutorials I'm still unsure.

  • Those errors seem to be too terse for Python. It usually gives several lines of potentially useful information. – joan Mar 8 at 16:31
  • Yeah, they're not quoted, I'm running Raspbian Lite so I can't paste the entire error message. – Jack Mar 8 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.