[首先,很抱歉创建一个单独的论坛,我知道在单独的论坛中也会回答相同的问题,但是..这似乎都无法解决我所面临的问题。因此,我正在pymodbus
库中创建ModbusRTU主站(或服务器-客户机术语中的客户机),问题是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)
谢谢问候,B先生
[首先,很抱歉创建一个单独的论坛,我知道在单独的论坛中也会回答相同的问题,但是..这似乎都无法解决我所面临的问题。因此,我正在pymodbus库中工作...
我认为您可能需要降低30000的偏移量,尝试更改此行:
Modbus协议非常简单,您可能很快就会创建自己的字节序列并使用原始套接字进行发送和接收。