zhu9296
zhu9296
采纳率0%
2018-12-11 07:59 阅读 1.9k

求大神看看这程序哪里错了?为什么一直报错,求大神指教

程序是为了循环读取功率仪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
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)

但是每次运行只有第一次能读出来,然后后面就出现以下报错:

Traceback (most recent call last):
  File "C:\Users\zqc\Desktop\新建文本文档 (2).PY", line 15, in <module>
    data = master.execute(1, cst.READ_HOLDING_REGISTERS, 704, 1)
  File "E:\Python\lib\site-packages\modbus_tk\utils.py", line 39, in new
    raise excpt
  File "E:\Python\lib\site-packages\modbus_tk\utils.py", line 37, in new
    ret = fcn(*args, **kwargs)
  File "E:\Python\lib\site-packages\modbus_tk\modbus.py", line 306, in execute
    response_pdu = query.parse_response(response)
  File "E:\Python\lib\site-packages\modbus_tk\modbus_rtu.py", line 46, in parse_response
    raise ModbusInvalidResponseError("Response length is invalid {0}".format(len(response)))
modbus_tk.exceptions.ModbusInvalidResponseError: Response length is invalid 0
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • yinshunyao yinshunyao 2018-12-13 07:12

    看提示呀,响应消息长度不对。 是不是构造的命令不对? 抓包看下通信消息。

    点赞 评论 复制链接分享
  • ydwzjw521 ydwzjw521 2020-12-06 10:36

    我也是这个问题,我的原因是AB接触不良,这问题不好排查,哈哈,总以为是程序的事呢

    点赞 评论 复制链接分享

相关推荐