问题描述
我目前正在尝试开发使用Modbus-RTU协议的应用程序,并且我必须在Python 2.7中使用 modbus_tk .
I'm currently trying to develop an application that uses the Modbus-RTU protocol, and I have to use modbus_tk in Python 2.7.
我应该使用来自另一个应用程序的代码,该应用程序能够通过modbus与微控制器进行通信.当我运行以下代码时,它可以在该应用程序上运行,但是在我的应用程序中运行相同的行时出现错误.
I'm supposed to use bits of code from another application which is able to communicate with the micro-controller via modbus. It works on that app when I run the following code, but I get an error when I run the same lines in my app.
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_rtu as modbus_rtu
import serial
MB_Add_Status = 8 + 5001
def MB_GetStatus(MB_Master_handle):
try:
status = MB_Master_handle.execute(1, cst.READ_HOLDING_REGISTERS, MB_Add_Status, 1)
return status
except modbus_tk.modbus.ModbusError, e:
logger.error("%s- Code=%d" % (e, e.get_exception_code()))
MB_port = 3
masterMB = modbus_rtu.RtuMaster(serial.Serial(port='COM'+str(MB_port), baudrate=19200, bytesize=8, parity='N', stopbits=2, xonxoff=0))
status = MB_GetStatus(masterMB)
首先,我需要在构造函数调用中删除自变量 baudrate
, bytesize
等,因为它出现了如下错误:
First I needed to delete the arguments baudrate
, bytesize
, etc. in the constructor call because it rose an error like :
TypeError: __init__() got an unexpected keyword argument 'stopbits'
但是当我们调用 execute
时,又出现了一个错误,我无法解决:
But then when we get to the call to execute
, there is an error again, which I couldn't solve yet :
modbus_tk.modbus.ModbusInvalidResponseError: Response length is invalid 0
我找到的唯一文档是: https://github.com/Nobatek/modbus-tk/tree/master/docs ,但我不太了解.如果有人可以首先向我解释此错误的真正含义,以及我应该看的地方,将不胜感激.非常感谢你!
The only documentation I found is: https://github.com/Nobatek/modbus-tk/tree/master/docs, but I couldn't quite understand much of it. If someone could first explain me what this error really means, and where I should look, this would be highly appreciated. Thank you very much !
推荐答案
找到了!
我更新了库并正确设置了构造函数的参数.知道这行得通.
I updated the library and set the parameters of the constructor correctly. This works fine know.
这篇关于无法使用Python的modbus_tk连接到从站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!