Python 使用 pymodbus 读取寄存器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24872269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading registers with pymodbus
提问by 5T4TiC
I am very new to Modbus and PyModBus however I have spent a good amount of time trying to read up and experiment with it. If anyone could point me in the right direction I would appreciate it...
我对 Modbus 和 PyModBus 非常陌生,但是我花了很多时间尝试阅读和试验它。如果有人能指出我正确的方向,我将不胜感激......
I have a drive with distance, velocity, acceleration, and deceleration on registers 40001, 40003, 40005, and 40007 (respectively). I was initially able to write to the distance register, using client.write_register(0000, n). After trying to write to velocity the drive started going haywire and faulting, and spinning 10x as fast as it should've been. However, the real priority is reading registers. I am trying to read the data from these registers and having zero luck. I tried using
我在寄存器 40001、40003、40005 和 40007(分别)上有一个带有距离、速度、加速度和减速度的驱动器。我最初能够使用 client.write_register(0000, n) 写入距离寄存器。在尝试写入速度后,驱动器开始失控并出现故障,并以应有的速度旋转 10 倍。然而,真正的优先级是读取寄存器。我试图从这些寄存器中读取数据并且运气为零。我尝试使用
request = client.read_holding_registers(0000,4)
response = client.execute(request)
print response
However, all I get back is "ReadRegisterResponse (0)".
但是,我得到的只是“ReadRegisterResponse (0)”。
So again, my big priority is trying to read values from these registers...any advice? (This is over TCP by the way)
再说一次,我的首要任务是尝试从这些寄存器中读取值……有什么建议吗?(顺便说一下,这是通过TCP)
采纳答案by Azat
Try to:
尝试:
response = client.read_holding_registers(0x00,4,unit=1)
where the unit value is device id of the slave.
其中单位值是从设备的设备 ID。
To print all:
全部打印:
print response.registers
Also is possible to directly get one value (for example third register):
也可以直接获取一个值(例如第三个寄存器):
print response.getRegister(2)
or
或者
print response.registers[2]
回答by Jak
you could parse the response by yourself, the following is my code snippet:
您可以自己解析响应,以下是我的代码片段:
result = client.read_input_registers(0x01,1, unit=0x01)
#print result
t = result.registers[0]
print "current temperature:", t, " ", float(t/100.0)