Search code examples
pythonattributeerrormodbuspymodbus

AttributeError: 'ExceptionResponse' object has no attribute 'registers' (ModbusRTU over TCP/IP)?


Firstly, I'm sorry to create a separate forum, I am aware the same question is answered in separate forums but.. none of it seems to address the issue Im facing. So, I am working in pymodbus library to create a ModbusRTU master (or client in server-client terminology) the problem is the RTU slave (field device) sends serial message over TCP/IP. So I am using ModbusTcpClient and ModbusRtuFramer to implement a Modbus RTU master which sends ModbusRTU query frame over TCP port and listens for response and logs it in csv file. Below is my code, But when I execute it I am getting the following error. I seek expert help solving this issue

 2020-06-07 17:59:58,895 MainThread     DEBUG    ModbusRTU_DataCollection_Script:42       Reading Input Registers
 2020-06-07 17:59:58,903 MainThread     DEBUG    transaction    :114      Current transaction state - IDLE
 2020-06-07 17:59:58,908 MainThread     DEBUG    transaction    :119      Running transaction 1
 2020-06-07 17:59:58,912 MainThread     DEBUG    transaction    :219      SEND: 0x1 0x4 0x75 0xf8 0x0 0x8 0x6a 0x31
 2020-06-07 17:59:58,918 MainThread     DEBUG    sync           :75       New Transaction state 'SENDING'
 2020-06-07 17:59:58,924 MainThread     DEBUG    transaction    :228      Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
 2020-06-07 17:59:58,929 MainThread     DEBUG    transaction    :304      Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
 2020-06-07 17:59:58,934 MainThread     DEBUG    transaction    :233      RECV: 0x1 0x84 0x2 0xc2 0xc1
 2020-06-07 17:59:58,939 MainThread     DEBUG    rtu_framer     :180      Getting Frame - 0x84 0x2
 2020-06-07 17:59:58,944 MainThread     DEBUG    factory        :266      Factory Response[132]
 2020-06-07 17:59:58,948 MainThread     DEBUG    rtu_framer     :115      Frame advanced, resetting header!!
 2020-06-07 17:59:58,953 MainThread     DEBUG    transaction    :383      Adding transaction 1
 2020-06-07 17:59:58,958 MainThread     DEBUG    transaction    :394      Getting transaction 1
 2020-06-07 17:59:58,962 MainThread     DEBUG    transaction    :193      Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
 Traceback (most recent call last):
   File "D:\JOB FILES\ModbusRTU_Master_DataCollector\ModbusRTU_DataCollection_Script.py", line 65, in <module
     run_sync_client()
   File "D:\JOB FILES\ModbusRTU_Master_DataCollector\ModbusRTU_DataCollection_Script.py", line 51, in run_sync_client
     decoder = BinaryPayloadDecoder.fromRegisters(result.registers,Endian.Little,wordorder=Endian.Little)
 AttributeError: 'ExceptionResponse' object has no attribute 'registers'

Code:

#! /usr/bin/python3
#=========================================================================================================#
#   --------------------------- Modbus RTU Data Collection -----------------------------------------------#
# Author: Mister.B                                                                                        #
# Date  : 4June2020                                                                                       #
# Objective: To write a script to query water meter for given Interval and save it into a csv file        #
# Version: v1.0                                                                                           #
# Interpreter : Python 3.7                                                                                #
# Third Party Libraries: 1) pymodbus (git://github.com/bashwork/pymodbus.git)                             #
#=========================================================================================================#
# Importing required libraries
import csv
import logging
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from time import sleep as delay
from datetime import datetime

# TCP Server Configuration
server_ip = "127.0.0.1"
port_no = 4059


#Configure the client logging
FORMAT = ('%(asctime)-15s %(threadName)-15s'
          '%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)#, filename="ModbusRTU_DC.log")
log = logging.getLogger()
#Set logging level (OPTIONS - DEBUG, INFO, WARNING, ERROR, CRITICAL)
log.setLevel(logging.DEBUG)

UNIT = 0x1

def run_sync_client():
    #create a Modbus Client object with ModbusRtuFramer
    client = ModbusClient(server_ip,port=port_no,retries=3,retry_on_empty=True,framer=ModbusRtuFramer)
    #connect to the Server
    client.connect()
    #slave query
    log.debug("Reading Input Registers")
    result = client.read_input_registers(30200,8,unit=UNIT)
    #[4, 3, 2, 1] - byteorder=Endian.Big, wordorder=Endian.Big
    #[3, 4, 1, 2] - byteorder=Endian.Little, wordorder=Endian.Big
    #[1, 2, 3, 4] - byteorder=Endian.Little, wordorder=Endian.Little
    decoder = BinaryPayloadDecoder.fromRegisters(result.registers,Endian.Little,wordorder=Endian.Little)
    value = decoder.decode_64bit_float()
    log.debug("Decoded value: "+str(value))
    now = datetime.now()
    S_datetime = now.strftime("%d-%m-%Y %H:%M:%S")
    with open("ModbusRTU_DataCollector.csv") as csv_file:
        csv_writer = csv.writer(csv_file,delimiter=",",quotechar='"',quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(list(S_datetime,str(value)))
    assert(not ReadInputRegister.isError())
    #close client
    client.close()

if __name__ == "__main__":
        run_sync_client()
        delay(1000)

Thanks in advance Regards, Mr.B


Solution

  • I think you might need to drop the 30000 offset, try changing this line:

    result = client.read_input_registers(200,8,unit=UNIT)