简体   繁体   English

pymodbus读取仪表寄存器

[英]pymodbus read meter registers

I'm new to modbus but I have a small project to work on. 我是Modbus的新手,但是我有一个小项目需要处理。 I need to read some values from an energy meter. 我需要从电表读取一些值。 I wrote this from some examples found in the internet: 我是从互联网上的一些示例中编写的:

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()

It seems to be connecting to the meter 'cause this is my output: 似乎正在连接到仪表,因为这是我的输出:

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)

I want to read from register 40012 to 40014 and this is Modbusdbus map I have: Modbus map 我想从寄存器40012读取到40014 ,这是我拥有的Modbusdbus映射: Modbus映射

I appreciate your help. 我感谢您的帮助。 Regards, 问候,

I think you should set the unit and port argument, and for getting the value use the rr.registers , so you need to know the unit_ID value, and the device port. 我认为您应该设置unitport参数,并使用rr.registers来获取该值,因此您需要知道unit_ID值以及设备端口。

In most cases, the unit is 1 and the port is 502 as the modbus default. 在大多数情况下, unit1port502作为modbus的默认值。

If you want to read from address 40012 to 40014 , you could read from 40012 as a bulky reading with count=3 . 如果要从地址4001240014进行读取,则可以从40012读取,其count=3


I improved your code, try it: 我改进了您的代码,尝试一下:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM