首页 > 解决方案 > 使用 Python / ModBUS 从 Carlo Gavazzi EM24-DIN.AV5 读取寄存器时出现问题

问题描述

亲爱的,

我有一个 Carlo Gavazzi 能量计 EM24-DIN.AV5,我正在使用 USB/RS485 转换器 (zk-u485) 将它与 Raspberry PI 连接。USB-RS485 的 A/B/GND 端口与设备的 41/42/43 端口相连。

该设备有一个操纵杆来配置串行波特率和 ModBus 地址(在我的情况下它们是 9600 和 3)。

我使用这个代码示例来读取一些寄存器,但总是收到Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received) D

#!/usr/bin/env python
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

from pymodbus.client.sync import ModbusSerialClient as ModbusClient

client = ModbusClient(method='rtu', port='/dev/ttyUSB0', timeout=3, stopbits = 1, bytesize = 8,  parity='N', baudrate= 9600)
val = client.connect()
if not val:
   print("******** Connection error *************")

while True:
#    rr = client.read_input_registers(address=37, count=2, unit=3);
    rr = client.read_input_registers(2,2,unit=3)
    print( rr);

按照完整的调试日志:

DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x3 0x4 0x0 0x2 0x0 0x2 0xd1 0xe9
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received))
DEBUG:pymodbus.framer.rtu_framer:Frame - [] not ready
DEBUG:pymodbus.transaction:Getting transaction 3
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)
DEBUG:pymodbus.transaction:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.transaction:Running transaction 2
DEBUG:pymodbus.transaction:SEND: 0x3 0x4 0x0 0x2 0x0 0x2 0xd1 0xe9
DEBUG:pymodbus.framer.rtu_framer:Changing state to IDLE - Last Frame End - None, Current Time stamp - 1596433492.94
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Incomplete message received, Expected 9 bytes Recieved 0 bytes !!!!
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV:
DEBUG:pymodbus.framer.rtu_framer:Frame - [] not ready
DEBUG:pymodbus.transaction:Getting transaction 3
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Modbus Error: [Input/Output] No Response received from the remote unit/Unable to decode response
DEBUG:pymodbus.transaction:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.transaction:Running transaction 3
DEBUG:pymodbus.transaction:SEND: 0x3 0x4 0x0 0x2 0x0 0x2 0xd1 0xe9
DEBUG:pymodbus.framer.rtu_framer:Changing state to IDLE - Last Frame End - 1596433495.95, Current Time stamp - 1596433495.95
DEBUG:pymodbus.framer.rtu_framer:Waiting for 3.5 char before next send - 4.01 ms
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'

遵循 ModBus 协议文档: https : //www.ccontrols.com/support/dp/CarloGavazziEM24.pdf ModBus 地址

非常感激你的帮助。

标签: modbusraspberry-pi4rs485pymodbus

解决方案


推荐阅读