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

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

1个回答

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

zhu9296
zhu9296 怎抓包啊,我不太会
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐