python - pymodbus 读取仪表寄存器
问题描述
我是 modbus 的新手,但我有一个小项目要做。我需要从能量计中读取一些值。我从网上找到的一些例子写了这个:
import logging
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
client = ModbusClient('192.168.80.210')
client.connect()
rr = client.read_holding_registers(40012, 1)
print rr
client.close()
它似乎正在连接到仪表,因为这是我的输出:
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x9c 0x4c 0x0 0x1
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 8 bytes (0 received))
DEBUG:pymodbus.framer.socket_framer:Processing:
DEBUG:pymodbus.transaction:Getting transaction 1
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 8 bytes (0 received)
我想从寄存器读取40012
,40014
这是我拥有的 Modbusdbus 映射:
Modbus 映射
我感谢您的帮助。问候,
解决方案
我认为您应该设置unit
andport
参数,并使用 获取值rr.registers
,因此您需要知道 unit_ID 值和设备端口。
在大多数情况下,unit
is1
和port
is502
作为 modbus 默认值。
如果您想从 address 读取40012
到40014
,您可以40012
使用count=3
.
我改进了你的代码,试试看:
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('192.168.80.210', port=502)
if client.connect():
res = client.read_holding_registers(40012, count=3, unit=1)
if not res.isError():
'''.isError() was implemented in pymodbus version 1.4.0 and above.'''
print(res.registers)
else:
# handling error
print(res)
client.close()
推荐阅读
- sql - Recursive CTE on Subset of Table Data
- php - 如何在 laravel 控制器中读取动态输入
- python - 隔离列表括号内的特定元素-Python
- c# - Unity Mobile 2D - 选择按钮 A,然后从按钮 B 调用函数
- oracle - 错误 - 单行子查询返回多于一行
- arrays - 在列表中查找单词并在文本中删除
- c# - 必应地图没有响应代码。WPF 应用程序
- r - 如何从 r 中的小时数中删除分钟数?例如,我有 23:45,我只想有 23 个而不是 23:00,我怎么能在 r 中做到这一点
- java - Java文件按路径制作目录
- json - 与其他格式相比,哪种 XML 更适合数据传输?