概述
如果你的开发环境是python3及以上
pip3 install modbus_tk
否则使用
pip install modbus_tk
系统:win10
IDE:pycharm + Qtdesigner
目标:实现简单modbus通信
import serial
import serial.tools.list_ports
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
PORT = 'COM10'
def main():
try:
master = modbus_rtu.RtuMaster(serial.Serial(port=PORT,
baudrate=115200,
bytesize=8,
parity='O',
stopbits=1,
xonxoff=0))
master.set_timeout(5)
master.set_verbose(True)
print('connected')
#read 方法
#print(master.execute(35,cst.READ_HOLDING_REGISTERS,102,10))#slaveAddr funCode startAddr regNum
#write方法
#print(master.execute(35, cst.WRITE_MULTIPLE_REGISTERS, 9, output_value=[1]))
except modbus_tk.modbus_rtu.ModbusInvalidResponseError as err:
print(err)
if __name__ == '__main__':
main()
- 摘出几个要点:
-
serial.Serial(port=PORT,baudrate=115200,bytesize=8,parity=‘O’,stopbits=1,xonxoff=0)
**port:**对应设备的端口号,win下是COMn,linux下是/dev/ttyp5n
**bytesize:**通信数据长度,可选FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5, 6, 7, 8),常用的还是8,我便没有尝试其他的
**parity:**奇偶校验,可选PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = ‘N’, ‘E’, ‘O’, ‘M’, ‘S’。不建议不校验,我选择ODD校验
**stopbits:**注意这里注意!!!MB协议要求的,若无parity,停止位应该是2位。我已经使用奇校验,故而停止位是1位
**xonoff:**流控制没用
此处程序,使用的是指定PORT口,在另一篇文章中,我设置了自动枚举PORT的功能,具体可以参照:PyQT下枚举USB串口/CH340/PL2303等设备 -
master.execute(35,cst.READ_HOLDING_REGISTERS,102,10))
#slaveAddr : 35(0x23)
#funCode : cst.READ_HOLDING_REGISTERS (0x03)
#startAddr : 102(0x66)
#regNum : 10(0x0A) -
master.execute(35, cst.WRITE_MULTIPLE_REGISTERS, 9, output_value=[1])
#slaveAddr : 35(0x23)
#funCode : cst.WRITE_MULTIPLE_REGISTERS(0x10)
#startAddr : 9(0x09)
#output value : 1(0x01) -
更一般地,操作方法如下:
master.execute(35, cst.READ_COILS, 0, 10))
master.execute(35, cst.READ_DISCRETE_INPUTS, 0, 8))
master.execute(35, cst.READ_INPUT_REGISTERS, 100, 3))
master.execute(35, cst.READ_HOLDING_REGISTERS, 100, 12))
master.execute(35, cst.WRITE_SINGLE_COIL, 7, output_value=1))
master.execute(35, cst.WRITE_SINGLE_REGISTER, 100, output_value=54))
master.execute(35, cst.WRITE_MULTIPLE_COILS, 0, output_value=[1, 1, 0, 1, 1, 0, 1, 1]))
master.execute(35, cst.WRITE_MULTIPLE_REGISTERS, 100, output_value=xrange(12)))
-
python实现的是主机,从机是飞思卡尔K60,从机移植了freeModbus,具体细节可参考
https://blog.csdn.net/ysgjiangsu/article/details/81512310 -
调试建议:从机先用Modbus Poll调通,再写上位机,以便定位问题。此前,从机用Modbus Poll通信30000+次,0错误,结果如下:
而我用Python写的主机,调试结果如下:
D:Python35python.exe "D:Program FilesJetBrainsPyCharm 2018.3helperspydevpydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 2984 --file F:/研究生/咖盟/AMP/prj/pythonserial/test1.py
pydev debugger: process 14416 is connecting
Connected to pydev debugger (build 183.4284.139)
connected
(4370, 4884, 5398, 5912, 6426, 6940, 7454, 7968, 8482, 8996)
Process finished with exit code -1
下次写个界面友好的QT吧,方便些
---------------------------------------------------本篇结束-------------------------------------------
最后
以上就是喜悦蚂蚁为你收集整理的[原创]python下通过modbus_tk实现modbus主机的全部内容,希望文章能够帮你解决[原创]python下通过modbus_tk实现modbus主机所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复