zhu9296
zhu9296
采纳率0%
2018-12-11 07:36

求教python 编程问题,关于modbus-tk

用modbus-tk,来读取功率仪的704地址的数据,想实现每隔三秒读取一次,求教该怎么编写循环程序,目前已实现单次读取,程序如下:

import serial 
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import time

def sleeptime(hour,min,sec):
    return hour*3600 + min*60 + sec
second = sleeptime(0,0,3)
time.sleep(second)
master = modbus_rtu.RtuMaster(
    serial.Serial('COM8', baudrate=19200, bytesize=8, parity='N', stopbits=1)
)
master.set_timeout(5.0)
data = master.execute(1, cst.READ_HOLDING_REGISTERS, 704, 1)
print (data)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • wlq199101 wlq199101 3年前
    master = modbus_rtu.RtuMaster(serial.Serial('COM8', baudrate=19200, bytesize=8, parity='N', stopbits=1))
    master.set_timeout(5.0)
    while (True):
    
        data = master.execute(1, cst.READ_HOLDING_REGISTERS, 704, 1)
        print (data)
        second = sleeptime(0,0,3);
        time.sleep(second);
    
    点赞 评论 复制链接分享