Warm tip: This article is reproduced from serverfault.com, please click

python-逐位读取保持寄存器西门子

(python - Read an holding register bit by bit Siemens)

发布于 2020-07-23 14:42:13

我需要用python一点一点地读取Siemens 1200的保持寄存器,在网上搜索我发现PyModbus是一个很好的库,但是用它我只能读取保持寄存器这样的整数而不是一点一点。

这是代码:

from pymodbus.client.sync import ModbusTcpClient

client = ModbusTcpClient('x.y.z.w', port=xxx)


result = client.write_registers(1, [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]) # write some register

results = client.read_holding_registers(0, 50) # read the first fifty register like int
print("0-50", results.registers)
results = client.read_holding_registers(50, 50) # read the last fifty register like int
print("50-100:", results.registers)


client.close()

有人有任何提示吗?

Questioner
Giuseppe
Viewed
0
Joao Luiz 2021-03-13 04:31:08
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian

CLP_MB = ModbusTcpClient(['IP'], port=502)

adress_Tag=1
adress_Tag=int(adress_Tag)
size=50
value = CLP.read_coils(adress_Tag, size, unit=0)
#last part
value_bit_0 = int(value.bits[0])
value_bit_1 = int(value.bits[1])
..... 
you can use a for loop to automation this last part