AttributeError: 'ExceptionResponse' object has no attribute 'registers' (ModbusRTU over TCP/IP)?
AttributeError: 'ExceptionResponse' object has no attribute 'registers' (ModbusRTU over TCP/IP)?
首先,我很抱歉创建一个单独的论坛,我知道在不同的论坛中回答了同样的问题但是.. none 它似乎解决了我面临的问题。所以,我正在 pymodbus
库中创建一个 ModbusRTU master(或服务器-客户端术语中的客户端)问题是 RTU 从站(现场设备)通过 TCP/IP 发送串行消息。所以我使用 ModbusTcpClient
和 ModbusRtuFramer
来实现一个 Modbus RTU 主机,它通过 TCP 端口发送 ModbusRTU 查询帧并监听响应并将其记录在 csv 文件中。下面是我的代码,但是当我执行它时,出现以下错误。我寻求专家帮助解决这个问题
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'
代码:
#! /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)
提前致谢
此致,Mr.B
我认为您可能需要删除 30000 偏移量,尝试更改此行:
result = client.read_input_registers(200,8,unit=UNIT)
首先,我很抱歉创建一个单独的论坛,我知道在不同的论坛中回答了同样的问题但是.. none 它似乎解决了我面临的问题。所以,我正在 pymodbus
库中创建一个 ModbusRTU master(或服务器-客户端术语中的客户端)问题是 RTU 从站(现场设备)通过 TCP/IP 发送串行消息。所以我使用 ModbusTcpClient
和 ModbusRtuFramer
来实现一个 Modbus RTU 主机,它通过 TCP 端口发送 ModbusRTU 查询帧并监听响应并将其记录在 csv 文件中。下面是我的代码,但是当我执行它时,出现以下错误。我寻求专家帮助解决这个问题
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'
代码:
#! /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)
提前致谢 此致,Mr.B
我认为您可能需要删除 30000 偏移量,尝试更改此行:
result = client.read_input_registers(200,8,unit=UNIT)