概述
import sys
import modbus_tk
import struct
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import time
import serial
import threading
PORT = 'com3'
#PORT = '/dev/ptyp5'
def main():
"""main"""
global slave_1
logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")
#Create the server
server = 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)
t = threading.Thread(target=updateData)
t.setDaemon(True)
t.start()
while True:
cmd = sys.stdin.readline()
args = cmd.split(' ')
if cmd.find('quit') == 0:
sys.stdout.write('bye-byern')
break
elif args[0] == 'add_slave':
slave_id = int(args[1])
server.add_slave(slave_id)
sys.stdout.write('done: slave %d addedrn' % (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 addedrn' % (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: %srn' % (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: %srn' % (str(values)))
else:
sys.stdout.write("unknown command %srn" % (args[0]))
finally:
server.stop()
#更新值
def updateData():
global slave_1
while True:
slave_1.set_values('0', 3, 88)
slave_1.set_values('0', 4, 99)
# print(slave_1.get_values('0',3,2))
time.sleep(1)
if __name__ == "__main__":
main()
最后
以上就是过时钥匙为你收集整理的python modbus控制plc_python 玩转 modbus的全部内容,希望文章能够帮你解决python modbus控制plc_python 玩转 modbus所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复