对于一个项目,我在 RS485 串行线路上连接了 4 个 modbus 设备。这些设备工作正常,现在我正在编写一个控制器,我对pymodbus
.
我找到了这个线程Python modbus 库,在我看来,那里可能有更好的 python modbus 库。pymodbus
易于使用,如果可能的话,我宁愿继续使用它。
但是我发现任何读/写例程返回的时间都等于超时。这对我来说似乎不对,所以我写了一个快速测试:
from pymodbus.client.sync import ModbusSerialClient
import time
for t in xrange(1, 11):
client = ModbusSerialClient("rtu", port="/dev/ttyUSB0", baudrate=9600, timeout=t)
start = time.time()
data = client.read_holding_registers(0x9000, count=7, unit=2)
stop = time.time()
if data:
succ = "was successful"
else:
succ = "failed"
print "timeout: %ss, read %s, time spent reading: %fs" % (t, succ, stop-start)
这是我得到的输出
timeout: 1s, read was successful, time spent reading: 1.039731s
timeout: 2s, read was successful, time spent reading: 2.038965s
timeout: 3s, read was successful, time spent reading: 3.041441s
timeout: 4s, read was successful, time spent reading: 4.040762s
timeout: 5s, read was successful, time spent reading: 5.043523s
timeout: 6s, read was successful, time spent reading: 6.040139s
timeout: 7s, read was successful, time spent reading: 7.042159s
timeout: 8s, read was successful, time spent reading: 8.045216s
timeout: 9s, read was successful, time spent reading: 9.047682s
timeout: 10s, read was successful, time spent reading: 10.048799s
我用不同的 RS845<->USB 转换器对其进行了测试,我总是得到类似的结果。
其他人可以证实这一点吗?或者我是否遗漏了一些未记录的论点,这些论点会提高ModbusSerialClient
's 的性能?