一、modbusSIM软件介绍
1.界面
2.参数设置
链接设置,选择链接设置/Modbus/TCP服务器
二、python 代码编写
1.python程序编写 Modbus_read.py
# -*- coding: utf-8 -*- import modbus_tk.modbus_tcp as mt import modbus_tk.defines as md import time #创建TCPMASTER对象 master = mt.TcpMaster('192.168.6.24',502) # master = mt.TcpMaster('127.0.0.1',502) #设置超时时间 master.set_timeout(5.5) #读取寄存器数值 while True: db2 = master.execute(slave=1,function_code=md.READ_HOLDING_REGISTERS,starting_address=0,quantity_of_x=30) print(db2) time.sleep(2)
打印结果如下图:
2.更改数据,查看打印结果
3.读写整数及布尔代码如下,原理相同,modbus_02.py
import modbus_tk.modbus_tcp as mt import modbus_tk.defines as md #创建TCPMASTER对象 master = mt.TcpMaster('127.0.0.1',502) #设置超时时间 master.set_timeout(5.5) #写入保持寄存地址1-10,值对应时 # values = (100,200,300,400,500,600,700,800,900,1000,1100) # master.execute(slave=1,function_code=md.WRITE_MULTIPLE_REGISTERS,starting_address=0,quantity_of_x=20,output_value=value) #写入单个寄存器 # master.execute(slave=1,function_code=md.WRITE_SINGLE_REGISTER,starting_address=0,output_value=888) # 读取线圈 # data = master.execute(slave=1,function_code=md.READ_COILS,starting_address=0,quantity_of_x=20) # print(data) #写入一个线圈 # master.execute(slave=1,function_code=md.WRITE_SINGLE_COIL,starting_address=0,output_value= True) #写入多个线圈 values = (True,False,True,False,True,False,True,False,True,False) master.execute(slave=1,function_code=md.WRITE_MULTIPLE_COILS,starting_address=0,output_value= values)