Modbus、python和最小Modbus
我正在使用python库Minimal Modbus读取电能表(RS-485) 我试图使用该函数一次读取多个寄存器 读取寄存器(寄存器地址、寄存器编号、函数代码=3) 以下是python库的代码:Modbus、python和最小Modbus,python,modbus,Python,Modbus,我正在使用python库Minimal Modbus读取电能表(RS-485) 我试图使用该函数一次读取多个寄存器 读取寄存器(寄存器地址、寄存器编号、函数代码=3) 以下是python库的代码: def read_registers(self, registeraddress, numberOfRegisters, functioncode=3): """Read integers from 16-bit registers in the slave. The
def read_registers(self, registeraddress, numberOfRegisters, functioncode=3):
"""Read integers from 16-bit registers in the slave.
The slave registers can hold integer values in the range 0 to 65535 ("Unsigned INT16").
Args:
* registeraddress (int): The slave register start address (use decimal numbers, not hex).
* numberOfRegisters (int): The number of registers to read.
* functioncode (int): Modbus function code. Can be 3 or 4.
Any scaling of the register data, or converting it to negative number (two's complement)
must be done manually.
Returns:
The register data (a list of int).
Raises:
ValueError, TypeError, IOError
"""
_checkFunctioncode(functioncode, [3, 4])
_checkInt(numberOfRegisters, minvalue=1, description='number of registers')
return self._genericCommand(functioncode, registeraddress, \
numberOfRegisters=numberOfRegisters, payloadformat='registers')
我遇到的问题是,寄存器将数据保存为Long
,但此函数将它们作为int
列表返回,并且查看这些值时,它似乎不正确
这是我目前的剧本:
##!/usr/bin/env python
import minimalmodbus
import time
import glob
import sys
import MySQLdb
instrument = minimalmodbus.Instrument('/dev/ttyUSB1', 7)
#Debug Options
#instrument.debug = True #debug modbus
read = False
def convert(value):
data=chr(value)
return data
def read_registers_ime():
data = instrument.read_registers(4096,20)
return data
while True:
try:
while read == False:
data = read_registers_ime()
print data
time.sleep(0.11)
read = True
break
except KeyboardInterrupt:
print "Stopped"
sys.exit()
except TypeError:
print "TypeError"
except IOError:
print "IOError"
此时,它返回以下内容:
[3,39192,3,44592,3,44592,0,423,0,0,0,0,6,19884,6,24584,6,19884]
到目前为止,我尝试将数据转换回原始格式的所有操作都失败了。我真的很想在这里得到一些帮助
非常感谢,
Bryan我在使用umodbus而不是最小modbus处理modbus时已经遇到了这种问题。我通过以下方式解决了这个问题:
- 成对获取寄存器
- 使用将对转换为整数
此示例可能需要根据您的具体需要进行一些调整,但可能会有所帮助。我已经在使用umodbus而不是最小modbus处理modbus时遇到过此类问题。我通过以下方式解决了这个问题:
- 成对获取寄存器
- 使用将对转换为整数
import struct
registers = [3, 39192, 3, 44592, 3, 44592, 0, 423, 0, 0, 0, 0, 0, 0, 6, 19884, 6, 24584, 6, 19884]
def grouped(iterable, group_size):
"""iterates on iterable, yielding groups of group_size elements"""
it = iter(iterable)
while True:
group = [next(it) for _ in range(group_size)]
yield group
for group in grouped(registers, 2):
bytes = b""
for word in group:
word_bytes = struct.pack("H", word)
bytes += word_bytes
bytes_hex = "0x" + "".join(["{:>02X}".format(ord(byte_)) for byte_ in bytes])
print("{} -> {} -> {}".format(group, bytes_hex, struct.unpack("<i", bytes)[0]))
[3, 39192] -> 0x03001899 -> -1726480381
[3, 44592] -> 0x030030AE -> -1372585981
[3, 44592] -> 0x030030AE -> -1372585981
[0, 423] -> 0x0000A701 -> 27721728
[0, 0] -> 0x00000000 -> 0
[0, 0] -> 0x00000000 -> 0
[0, 0] -> 0x00000000 -> 0
[6, 19884] -> 0x0600AC4D -> 1303117830
[6, 24584] -> 0x06000860 -> 1611137030
[6, 19884] -> 0x0600AC4D -> 1303117830