当前位置:   article > 正文

[原创]python下通过modbus_tk实现modbus主机_python modbus_tk

python modbus_tk

如果你的开发环境是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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 摘出几个要点:
  1. 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等设备

  2. master.execute(35,cst.READ_HOLDING_REGISTERS,102,10))
    #slaveAddr : 35(0x23)
    #funCode : cst.READ_HOLDING_REGISTERS (0x03)
    #startAddr : 102(0x66)
    #regNum : 10(0x0A)

  3. 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)

  4. 更一般地,操作方法如下:

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)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. python实现的是主机,从机是飞思卡尔K60,从机移植了freeModbus,具体细节可参考
    https://blog.csdn.net/ysgjiangsu/article/details/81512310

  2. 调试建议:从机先用Modbus Poll调通,再写上位机,以便定位问题。此前,从机用Modbus Poll通信30000+次,0错误,结果如下:
    在这里插入图片描述
    而我用Python写的主机,调试结果如下:

D:\Python35\python.exe "D:\Program Files\JetBrains\PyCharm 2018.3\helpers\pydev\pydevd.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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

下次写个界面友好的QT吧,方便些

---------------------------------------------------本篇结束-------------------------------------------

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/75894
推荐阅读
相关标签
  

闽ICP备14008679号