首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AttributeError:'ExceptionResponse‘对象没有属性’寄存器‘(ModbusRTU over TCP/IP)?

AttributeError:'ExceptionResponse‘对象没有属性’寄存器‘(ModbusRTU over TCP/IP)?
EN

Stack Overflow用户
提问于 2020-06-07 14:41:05
回答 1查看 727关注 0票数 1

首先,我很抱歉创建了一个单独的论坛,我知道同样的问题在不同的论坛上得到了回答,但是..。所有这些似乎都没有解决我面临的问题。因此,我正在pymodbus库中创建一个ModbusRTU主服务器(或服务器-客户端术语中的客户端)问题是RTU从站(现场设备)通过TCP/IP发送串行消息。因此,我使用ModbusTcpClientModbusRtuFramer来实现一个ModbusRTU主机,它通过TCP端口发送ModbusRTU查询帧,并侦听响应并将其记录在csv文件中。下面是我的代码,但是当我执行它时,我会得到以下错误。我寻求专家帮助解决这个问题。

代码语言:javascript
运行
复制
 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'

代码:

代码语言:javascript
运行
复制
#! /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)

谢谢你,B先生

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-10 16:42:01

我认为您可能需要删除30000偏移量,尝试更改这一行:

代码语言:javascript
运行
复制
result = client.read_input_registers(200,8,unit=UNIT)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62246967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档