Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 逐位读取保持寄存器_Python_Modbus Tcp_Pymodbus - Fatal编程技术网

Python 逐位读取保持寄存器

Python 逐位读取保持寄存器,python,modbus-tcp,pymodbus,Python,Modbus Tcp,Pymodbus,我需要用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,

我需要用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()
有人有任何提示吗?

Modbus保持寄存器是16位整数,协议没有单独读取位的规定。当然,一旦接收到整数位,就可以对其执行任何操作。
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