本文主要是介绍python modbus_tk模块学习笔记(rtu slaver例程),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
GIT地址:https://github.com/ljean/modbus-tk
找到modbus rtu slaver 例程
用宝塔将例程复制到centos目录
#!/usr/bin/env python
# -*- coding: utf_8 -*-
"""Modbus TestKit: Implementation of Modbus protocol in python(C)2009 - Luc Jean - luc.jean@gmail.com(C)2009 - Apidev - http://www.apidev.frThis is distributed under GNU LGPL license, see license.txt
"""import sysimport modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serialPORT = 0
#PORT = '/dev/ptyp5'def main():"""main"""logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")#Create the serverserver = modbus_rtu.RtuServer(serial.Serial(PORT))try:logger.info("running...")logger.info("enter 'quit' for closing the server")server.start()slave_1 = server.add_slave(1)slave_1.add_block('0', cst.HOLDING_REGISTERS, 0, 100)while True:cmd = sys.stdin.readline()args = cmd.split(' ')if cmd.find('quit') == 0:sys.stdout.write('bye-bye\r\n')breakelif args[0] == 'add_slave':slave_id = int(args[1])server.add_slave(slave_id)sys.stdout.write('done: slave %d added\r\n' % (slave_id))elif args[0] == 'add_block':slave_id = int(args[1])name = args[2]block_type = int(args[3])starting_address = int(args[4])length = int(args[5])slave = server.get_slave(slave_id)slave.add_block(name, block_type, starting_address, length)sys.stdout.write('done: block %s added\r\n' % (name))elif args[0] == 'set_values':slave_id = int(args[1])name = args[2]address = int(args[3])values = []for val in args[4:]:values.append(int(val))slave = server.get_slave(slave_id)slave.set_values(name, address, values)values = slave.get_values(name, address, len(values))sys.stdout.write('done: values written: %s\r\n' % (str(values)))elif args[0] == 'get_values':slave_id = int(args[1])name = args[2]address = int(args[3])length = int(args[4])slave = server.get_slave(slave_id)values = slave.get_values(name, address, length)sys.stdout.write('done: values read: %s\r\n' % (str(values)))else:sys.stdout.write("unknown command %s\r\n" % (args[0]))finally:server.stop()if __name__ == "__main__":main()
查看centos串口号
[root@bogon /]# cat /proc/tty/driver/serial
serinfo:1.0 driver revision:
0: uart:16550A port:000003F8 irq:4 tx:0 rx:0 CTS|DSR|CD
1: uart:16550A port:000002F8 irq:3 tx:40867 rx:13240 DSR|CD
2: uart:unknown port:000003E8 irq:4
3: uart:unknown port:000002E8 irq:3
[root@bogon /]# ls -l /dev/ttyS*
crw-rw---- 1 root dialout 4, 64 12月 18 11:29 /dev/ttyS0
crw-rw---- 1 root dialout 4, 65 12月 18 12:39 /dev/ttyS1
crw-rw---- 1 root dialout 4, 66 12月 18 11:29 /dev/ttyS2
crw-rw---- 1 root dialout 4, 67 12月 18 11:29 /dev/ttyS3
[root@bogon /]#
这里我用的
/dev/ttyS1
刚才复制的例程
修改
PORT = 0
#PORT = '/dev/ptyp5'
为
#PORT = 0
PORT = '/dev/ttyS1'
centos下运行例程
python licheng.py
输出
RtuServer /dev/ttyS1 is opened
running...
enter 'quit' for closing the server
输入
set_values 1 0 0 1 2 3 4 5 6 7 8 9 10
返回
done: values written: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
modbus poll
这篇关于python modbus_tk模块学习笔记(rtu slaver例程)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!