首先,您应该获取最小和最大寄存器,然后通过带有count
参数的请求读取,然后您必须将考虑值分配给每个寄存器。
注:最大计数为125
.
像这样的东西:
def chunking(registers):
# do stuff.
def assignment_regs_to_values(values, registesr):
# do stuff.
registers = [4246,6622,6626,6676,6624,6496,6658,4096,4346]
min_reg = min(registers)
max_reg = max(registers)
count_ = max_reg - min_reg + 1
if count_ > 125:
sub_regs = chunking(registers)
# get min & max of each sub_reg list ...
else:
sorted_regs = sorted(registers)
res = client.read_holding_registers(min_reg, count=count_ , unit=1) # Note
assignment_regs_to_values(res.registes, sorted_regs)
在您的情况下,最佳请求数将是 4 个请求(而不是 9 个请求):
sub_regs1 = [6676, 6658, 6626, 6624, 6622]
sub_regs2 = [6496]
sub_regs3 = [4336, 4246]
sub_regs3 = [4096]
count1 = max(sub_regs1) - min(sub_regs1) + 1
res1 = client.read_holding_registers(min(sub_regs1), count=count1, unit=1)
res2 = client.read_holding_registers(6496, count=1, unit=1)
count3 = max(sub_regs3) - min(sub_regs3) + 1
res3 = client.read_holding_registers(min(sub_regs3), count=count3, unit=1)
res4 = client.read_holding_registers(4096, count=1, unit=1)