领导又发话了,需要在服务器的虚拟机上搭建一套测试环境,提供给开发做传感器的测试。虚拟机只有一个IP, 如果套用之前写的脚本,每套传感器设备配置需要一个IP,如果都对应这台虚拟机,表面上看起来一次只能测试一套传感器,但是我们目前就有4套,需要写4个脚本,还不能同时运行。后来发现slave id的用处在这里体现出来了,同一个IP下,可以同时配置若干slave id,连接同一个socket,同时启动若干个实验获取modbus slave设置的传感器数据。
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 29 13:43:59 2017
@author: Administrator
"""
import sys
import struct
import modbus_tk
import modbus_tk.defines as mtk
import modbus_tk.modbus
import modbus_tk.modbus_tcp
import time
import random
import string
logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")
try:
#server = modbus_tk.modbus_tcp.TcpServer()
#这里的端口和地址都是默认的,地址是本地:
#server = modbus_tk.modbus_tcp.TcpServer(port=502, address='127.0.0.1', timeout_in_sec=3)
server = modbus_tk.modbus_tcp.TcpServer(port=502, address='0.0.0.0', timeout_in_sec=3)
server.start()
slave_1 = server.add_slave(1)
slave_3 = server.add_slave(3)
slave_2 = server.add_slave(2)
slave_5 = server.add_slave(5)
# 创建寄存器,寄存器的别名为block1,寄存器为保持寄存器(Holding_Register),寄存器起始地址为0,寄存器个数为...
slave_1.add_block('block1', modbus_tk.defines.HOLDING_REGISTERS, 0, 10)
slave_3.add_block('block1', modbus_tk.defines.HOLDING_REGISTERS, 0, 2)
slave_2.add_block('block1', modbus_tk.defines.READ_INPUT_REGISTERS, 0, 44)
slave_5.add_block('block1', modbus_tk.defines.READ_INPUT_REGISTERS, 0, 28)
#给slave_1添加一个模块(模块名,只读,地址,长度)
valueAll_1 = slave_1.get_values('block1', 0, 10)
print 'valueAll--1: ', valueAll_1
valueAll_3 = slave_3.get_values('block1', 0, 2)
print 'valueAll--3: ', valueAll_3
valueAll_2 = slave_2.get_values('block1', 0, 44)
print 'valueAll--2: ', valueAll_2
valueAll_5 = slave_5.get_values('block1', 0, 28)
print 'valueAll--5: ', valueAll_5
# value0 = slave_1.get_values('block1', 0, 1) # 寄存器0000 -- 状态位初始值为全0
# print 'value0: ', value0
while True:
# slave只set数据,不再读取数据,以防端口被占用,master读不上来
#value_all = slave_1.get_values('block1', 0, 10)
#sys.stdout.write('done: values read: %s\r\n' % (str(value_all)))
# 室内温度
# 小数点后保留2位
tempr = round(((random.random()*1000)+10000), 2)
# 室内湿度
humid = round(((random.random()*1000)+10000), 2)
# 光照
sunlight = round(((random.random()*900)+10000), 2)
# CO2
co2 = round(((random.random()*1000)+10000), 2)
# 营养液温度
nuTempr = round(((random.random()*1000)+10000), 2)
# 含氧量
oxyg = round((random.uniform(200, 600)), 2)
# 室内温度1
tempr1 = round(random.uniform(10000, 20000), 2)
# 室内湿度1
humid1 = round(random.uniform(10000, 12000), 2)
# CO2_1
co2_1 = round(random.uniform(10000, 15000), 2)
# 瓶内温度1
bo_tempr1 = round(random.uniform(10000, 20000), 2)
# 室内温度2
tempr2 = round(random.uniform(10000, 20000), 2)
# 室内湿度2
humid2 = round(random.uniform(10000, 13000), 2)
# CO2
co2_2 = round(random.uniform(10000, 20000), 2)
# 瓶内温度2
bo_tempr2 = round(random.uniform(10000, 14000), 2)
# slave_1
slave_1.set_values('block1', 1, tempr)
slave_1.set_values('block1', 3, humid)
slave_1.set_values('block1', 5, sunlight)
slave_1.set_values('block1', 7, co2)
slave_1.set_values('block1', 9, nuTempr)
#slave_3
slave_3.set_values('block1', 1, oxyg)
# slave_2
slave_2.set_values('block1', 0, tempr1)
slave_2.set_values('block1', 1, humid1)
slave_2.set_values('block1', 2, co2_1)
slave_2.set_values('block1', 3, bo_tempr1)
slave_2.set_values('block1', 8, tempr2)
slave_2.set_values('block1', 9, humid2)
slave_2.set_values('block1', 10, co2_2)
slave_2.set_values('block1', 11, bo_tempr2)
slave_2.set_values('block1', 16, tempr1)
slave_2.set_values('block1', 17, humid1)
slave_2.set_values('block1', 18, co2_1)
slave_2.set_values('block1', 19, bo_tempr1)
slave_2.set_values('block1', 24, tempr2)
slave_2.set_values('block1', 25, humid2)
slave_2.set_values('block1', 26, co2_2)
slave_2.set_values('block1', 27, bo_tempr2)
slave_2.set_values('block1', 32, tempr1)
slave_2.set_values('block1', 33, humid1)
slave_2.set_values('block1', 34, co2_1)
slave_2.set_values('block1', 35, bo_tempr1)
slave_2.set_values('block1', 40, tempr2)
slave_2.set_values('block1', 41, humid2)
slave_2.set_values('block1', 42, co2_2)
slave_2.set_values('block1', 43, bo_tempr2)
# slave_5
slave_5.set_values('block1', 0, tempr1)
slave_5.set_values('block1', 1, humid1)
slave_5.set_values('block1', 2, co2_1)
slave_5.set_values('block1', 3, bo_tempr1)
slave_5.set_values('block1', 8, tempr2)
slave_5.set_values('block1', 9, humid2)
slave_5.set_values('block1', 10, co2_2)
slave_5.set_values('block1', 11, bo_tempr2)
slave_5.set_values('block1', 16, tempr1)
slave_5.set_values('block1', 17, humid1)
slave_5.set_values('block1', 18, co2_1)
slave_5.set_values('block1', 19, bo_tempr1)
slave_5.set_values('block1', 24, tempr2)
slave_5.set_values('block1', 25, humid2)
slave_5.set_values('block1', 26, co2_2)
slave_5.set_values('block1', 27, bo_tempr2)
print '======================'
time.sleep(1)
except:
print '============error==========='
finally:
print '=========stop========'
server.stop()