簡體   English   中英

PyModbus 無法通過 TCP 讀取 RTU 中的輸入寄存器

[英]PyModbus failing to read input registers in RTU Over TCP

我嘗試使用pymodbus讀取 Modbus 信號但出現錯誤源如下:
服務器響應TCP時有MAC地址通知功能。 Modbus 通訊怎么可能沒有錯誤? 如何刪除 TCP 標頭部分?

在此處輸入圖片說明

手冊地址如下:https ://www.eztcp.com/en/download/pds_files/an_ezmanager_en.pdf

from pymodbus.client.sync import ModbusTcpClient
from pymodbus.transaction import ModbusRtuFramer as ModbusFramer
import time

import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

def run_read_data(ip, port_num, unit_num):
    while True:
        client = ModbusTcpClient(ip, port=port_num, framer=ModbusFramer)
        con = client.connect()
        while con :
            rr = client.read_input_registers(0,10, unit=unit_num)        
            try:
                print(rr,rr.registers)

            except Exception as e:
                print(f"{type(e).__name__}: {e}")
  
                time.sleep(2)
                client.close()
                time.sleep(2)
                break
            time.sleep(1)
        time.sleep(1)

if __name__ == "__main__":
    ip = "x.x.x.x" 
    port_num = x
    unit_num = 0x1
    run_read_data(ip, port_num,unit_num)

結果是這樣的:

DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x18 0x4 0x0 0x0 0x0 0xa 0x72 0x4
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x30 0x30 0x33 0x30 0x66 0x39 0x31 0x31 0x34 0x30 0x38 0x35 0xd 0xa 0x18 0x4 0x14 0xe8 0xf9 0x2 0x45 0xf3 0x88 0x1 0x22
DEBUG:pymodbus.framer.rtu_framer:CRC invalid, discarding header!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x30 0x30 0x33 0x30 0x66 0x39 0x31 0x31 0x34 0x30 0x38 0x35 0xd 0xa 0x18 0x4 0x14 0xe8 0xf9 0x2 0x45 0xf3 0x88 0x1 0x22
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer -
DEBUG:pymodbus.transaction:Getting transaction 24
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
AttributeError: 'ModbusIOException' object has no attribute 'registers'
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x18 0x4 0x0 0x0 0x0 0xa 0x72 0x4
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x30 0x30 0x33 0x30 0x66 0x39 0x31 0x31 0x34 0x30 0x38 0x35 0xd 0xa 0x18 0x4 0x14 0xe9 0x9f 0x2 0x45 0xf3 0xd5 0x1 0x22
DEBUG:pymodbus.framer.rtu_framer:CRC invalid, discarding header!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer - 0x30 0x30 0x33 0x30 0x66 0x39 0x31 0x31 0x34 0x30 0x38 0x35 0xd 0xa 0x18 0x4 0x14 0xe9 0x9f 0x2 0x45 0xf3 0xd5 0x1 0x22
DEBUG:pymodbus.framer.rtu_framer:Frame check failed, ignoring!!
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer -
DEBUG:pymodbus.transaction:Getting transaction 24
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
AttributeError: 'ModbusIOException' object has no attribute 'registers'

您收到的消息無效,您可以看到 CRC 檢查失敗 - 問題出在 modbusTCP 服務器上。 由於該response沒有屬性registers導致異常的原因。 然而,如果你想看看錯誤是什么樣的,你應該打印rr而不做任何其他事情。

檢查是否有錯誤的好方法是使用isError()方法:

if rr.isError():
    # handle error
else:
    # proceed with response

如果您嘗試解碼消息,則應按如下方式使用BinaryPayloadDecoder

decoder = BinaryPaloadDecoder.fromRegisters(rr.registers)
value = decoder.decode_32bit_float()

完整參考 在這里

不幸的是,我不知道您所說的“如何刪除 TCP 標頭部分”是什么意思。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM