old.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. # -*- coding: utf-8 -*-
  2. from demeter.core import *
  3. import modbus_tk
  4. import modbus_tk.modbus_tcp as modbus_tcp
  5. import modbus_tk.defines as mdef
  6. import time
  7. import random
  8. import struct
  9. import serial
  10. class Old(object):
  11. def server(self):
  12. # 启动服务
  13. server = modbus_tcp.TcpServer(port=8010)
  14. server.start()
  15. slave_1 = server.add_slave(1)
  16. slave_1.add_block('block1', modbus_tk.defines.HOLDING_REGISTERS, 0, 16)
  17. while 1:
  18. tempr = round(((random.random()*1000)), 2)
  19. value = struct.unpack('>HH', struct.pack('>f', tempr))
  20. #value = tempr
  21. Demeter.echo('tempr:' + str(tempr))
  22. Demeter.echo('value:' + str(value))
  23. slave_1.set_values('block1', 0, value)
  24. shidu = round(((random.random()*1000)), 2)
  25. value = struct.unpack('>HH', struct.pack('>f', shidu))
  26. #value = shidu
  27. Demeter.echo('shidu:' + str(shidu))
  28. Demeter.echo('value:' + str(value))
  29. slave_1.set_values('block1', 2, value)
  30. famen = random.randint(1,2)
  31. value = struct.unpack('>HH', struct.pack('>f', famen))
  32. #Demeter.echo('famen:' + str(famen))
  33. #Demeter.echo('value:' + str(value))
  34. #slave_1.set_values('block1', 4, value)
  35. Demeter.echo('=========')
  36. time.sleep(3)
  37. def test(self):
  38. port = 'COM3'
  39. #master = modbus_rtu.RtuMaster(serial.Serial(port=port, baudrate=38400, bytesize=8, parity='O', stopbits=1, xonxoff=0))
  40. master = modbus_tcp.TcpMaster(host='192.168.33.10', port=int(1152))
  41. master.set_timeout(30) #timeout表示若超过3秒没有连接上slave就会自动断开
  42. value = master.execute(1, modbus_tk.defines.READ_HOLDING_REGISTERS, 0, 2)
  43. print(value)
  44. value = master.execute(1, modbus_tk.defines.READ_HOLDING_REGISTERS, 2, 2)
  45. print(value)
  46. # 获取数据
  47. def getData(self, type, slave_id):
  48. pass
  49. def server_s(self):
  50. server = modbus_tk.modbus_tcp.TcpServer(port=1152, address='0.0.0.0', timeout_in_sec=3)
  51. server.start()
  52. slave_1 = server.add_slave(1)
  53. slave_3 = server.add_slave(3)
  54. slave_2 = server.add_slave(2)
  55. slave_5 = server.add_slave(5)
  56. # 创建寄存器,寄存器的别名为block1,寄存器为保持寄存器(Holding_Register),寄存器起始地址为0,寄存器个数为...
  57. slave_1.add_block('block1', modbus_tk.defines.HOLDING_REGISTERS, 0, 10)
  58. slave_3.add_block('block1', modbus_tk.defines.HOLDING_REGISTERS, 0, 2)
  59. slave_2.add_block('block1', modbus_tk.defines.READ_INPUT_REGISTERS, 0, 44)
  60. slave_5.add_block('block1', modbus_tk.defines.READ_INPUT_REGISTERS, 0, 28)
  61. #给slave_1添加一个模块(模块名,只读,地址,长度)
  62. valueAll_1 = slave_1.get_values('block1', 0, 10)
  63. print('valueAll--1: ', valueAll_1)
  64. valueAll_3 = slave_3.get_values('block1', 0, 2)
  65. print('valueAll--3: ', valueAll_3)
  66. valueAll_2 = slave_2.get_values('block1', 0, 44)
  67. print('valueAll--2: ', valueAll_2)
  68. valueAll_5 = slave_5.get_values('block1', 0, 28)
  69. print('valueAll--5: ', valueAll_5)
  70. # value0 = slave_1.get_values('block1', 0, 1) # 寄存器0000 -- 状态位初始值为全0
  71. # print 'value0: ', value0
  72. while True:
  73. # slave只set数据,不再读取数据,以防端口被占用,master读不上来
  74. #value_all = slave_1.get_values('block1', 0, 10)
  75. #sys.stdout.write('done: values read: %s\r\n' % (str(value_all)))
  76. # 室内温度
  77. # 小数点后保留2位
  78. tempr = round(((random.random()*1000)+10000), 2)
  79. # 室内湿度
  80. humid = round(((random.random()*1000)+10000), 2)
  81. # 光照
  82. sunlight = round(((random.random()*900)+10000), 2)
  83. # CO2
  84. co2 = round(((random.random()*1000)+10000), 2)
  85. # 营养液温度
  86. nuTempr = round(((random.random()*1000)+10000), 2)
  87. # 含氧量
  88. oxyg = round((random.uniform(200, 600)), 2)
  89. # 室内温度1
  90. tempr1 = round(random.uniform(10000, 20000), 2)
  91. # 室内湿度1
  92. humid1 = round(random.uniform(10000, 12000), 2)
  93. # CO2_1
  94. co2_1 = round(random.uniform(10000, 15000), 2)
  95. # 瓶内温度1
  96. bo_tempr1 = round(random.uniform(10000, 20000), 2)
  97. # 室内温度2
  98. tempr2 = round(random.uniform(10000, 20000), 2)
  99. # 室内湿度2
  100. humid2 = round(random.uniform(10000, 13000), 2)
  101. # CO2
  102. co2_2 = round(random.uniform(10000, 20000), 2)
  103. # 瓶内温度2
  104. bo_tempr2 = round(random.uniform(10000, 14000), 2)
  105. # slave_1
  106. slave_1.set_values('block1', 1, tempr)
  107. slave_1.set_values('block1', 3, humid)
  108. slave_1.set_values('block1', 5, sunlight)
  109. slave_1.set_values('block1', 7, co2)
  110. slave_1.set_values('block1', 9, nuTempr)
  111. #slave_3
  112. slave_3.set_values('block1', 1, oxyg)
  113. # slave_2
  114. slave_2.set_values('block1', 0, tempr1)
  115. slave_2.set_values('block1', 1, humid1)
  116. slave_2.set_values('block1', 2, co2_1)
  117. slave_2.set_values('block1', 3, bo_tempr1)
  118. slave_2.set_values('block1', 8, tempr2)
  119. slave_2.set_values('block1', 9, humid2)
  120. slave_2.set_values('block1', 10, co2_2)
  121. slave_2.set_values('block1', 11, bo_tempr2)
  122. slave_2.set_values('block1', 16, tempr1)
  123. slave_2.set_values('block1', 17, humid1)
  124. slave_2.set_values('block1', 18, co2_1)
  125. slave_2.set_values('block1', 19, bo_tempr1)
  126. slave_2.set_values('block1', 24, tempr2)
  127. slave_2.set_values('block1', 25, humid2)
  128. slave_2.set_values('block1', 26, co2_2)
  129. slave_2.set_values('block1', 27, bo_tempr2)
  130. slave_2.set_values('block1', 32, tempr1)
  131. slave_2.set_values('block1', 33, humid1)
  132. slave_2.set_values('block1', 34, co2_1)
  133. slave_2.set_values('block1', 35, bo_tempr1)
  134. slave_2.set_values('block1', 40, tempr2)
  135. slave_2.set_values('block1', 41, humid2)
  136. slave_2.set_values('block1', 42, co2_2)
  137. slave_2.set_values('block1', 43, bo_tempr2)
  138. # slave_5
  139. slave_5.set_values('block1', 0, tempr1)
  140. slave_5.set_values('block1', 1, humid1)
  141. slave_5.set_values('block1', 2, co2_1)
  142. slave_5.set_values('block1', 3, bo_tempr1)
  143. slave_5.set_values('block1', 8, tempr2)
  144. slave_5.set_values('block1', 9, humid2)
  145. slave_5.set_values('block1', 10, co2_2)
  146. slave_5.set_values('block1', 11, bo_tempr2)
  147. slave_5.set_values('block1', 16, tempr1)
  148. slave_5.set_values('block1', 17, humid1)
  149. slave_5.set_values('block1', 18, co2_1)
  150. slave_5.set_values('block1', 19, bo_tempr1)
  151. slave_5.set_values('block1', 24, tempr2)
  152. slave_5.set_values('block1', 25, humid2)
  153. slave_5.set_values('block1', 26, co2_2)
  154. slave_5.set_values('block1', 27, bo_tempr2)
  155. print('======================')
  156. time.sleep(1)
  157. def master(self, master, id):
  158. info = Demeter.service('common').list('device_info', search={'server_id':id})
  159. if info:
  160. for v in info:
  161. if v['product_id']:
  162. code = Demeter.service('common').one('setting_modbus_code', id=v['product_id'])
  163. if code['oper_type'] == 1:
  164. self.setValue(code, master, v)
  165. def setValue(self, code, master, info):
  166. config = {}
  167. gateway = Demeter.service('common').one('device_gateway', id=info['gateway_id'])
  168. hardware = Demeter.service('common').one('hardware_type', id=info['hardware_type'])
  169. config['parent'] = gateway['hardware_id']
  170. config['method'] = hardware['key']
  171. config['child'] = info['hardware_id']
  172. address = code['register_address'].split("\r\n")
  173. device_type_key = code['device_type_key'].split("\r\n")
  174. exp = code['exp'].split("\r\n")
  175. name = info['name']
  176. j = 0
  177. for i in address:
  178. (code['register_address'],code['register_length']) = i.split(',')
  179. code['exp'] = exp[j]
  180. value = self.getValue(code, master, info)
  181. device_type = device_type_key[j]
  182. device_type = Demeter.service('common').one('setting_device_type', key=device_type)
  183. config['type'] = device_type['key']
  184. j = j + 1
  185. Demeter.service('device').save(config, value, name, device_type)
  186. def getValue(self, code, master, info):
  187. if code['register_type'] == 1:
  188. # 线圈
  189. modbus_register_type = mdef.READ_COILS
  190. elif code['register_type'] == 2:
  191. modbus_register_type = mdef.READ_DISCRETE_INPUTS
  192. elif code['register_type'] == 3:
  193. modbus_register_type = mdef.READ_HOLDING_REGISTERS
  194. elif code['register_type'] == 4:
  195. modbus_register_type = mdef.READ_INPUT_REGISTERS
  196. temp = master.execute(int(code['slave_id']), modbus_register_type, int(code['register_address']), int(code['register_length']))
  197. #Demeter.echo('temp:' + str(temp) + 'size:' + str(len(temp)))
  198. value = struct.unpack('>f', struct.pack('>HH', temp[0], temp[1]))
  199. value = str(round(value[0], 2))
  200. if 'exp' in code:
  201. value = Demeter.exp(code['exp'], value)
  202. if 'exp' in info:
  203. value = Demeter.exp(info['exp'], value)
  204. return value
  205. def send(self, server, type_info, info, value):
  206. master = modbus_tcp.TcpMaster(host=server['host'], port=int(server['port']))
  207. master.set_timeout(6.0)
  208. master.set_verbose(True)
  209. if 'exp' in device:
  210. value = Demeter.exp(device['exp'], value)
  211. if 'exp' in info:
  212. value = Demeter.exp(info['exp'], value)
  213. if ',' in value:
  214. value = value.split(',')
  215. modbus_register_type = ''
  216. if device['modbus_register_type'] == 1:
  217. modbus_register_type = mdef.WRITE_SINGLE_COIL
  218. elif device['modbus_register_type'] == 2:
  219. modbus_register_type = mdef.WRITE_MULTIPLE_COILS
  220. elif device['modbus_register_type'] == 3:
  221. modbus_register_type = mdef.WRITE_SINGLE_REGISTER
  222. elif device['modbus_register_type'] == 4:
  223. modbus_register_type = mdef.WRITE_MULTIPLE_REGISTERS
  224. result = ''
  225. if modbus_register_type:
  226. master.execute(device['modbus_slave_id'], modbus_register_type, device['modbus_register_address'], output_value=value)
  227. result = master.execute(device['modbus_slave_id'], modbus_register_type, device['modbus_register_address'], len(value))
  228. return result
  229. def start(self):
  230. server = Demeter.service('common').list('setting_server', search={'server_type':2})
  231. if server:
  232. modbus = Demeter.path + 'modbus.py'
  233. for v in server:
  234. Shell.popen('python3 ' + modbus + ' -m client -i ' + v['id'])
  235. def client(self, id):
  236. server = Demeter.service('common').one('setting_server', id=id)
  237. master = modbus_tcp.TcpMaster(host=server['server_host'], port=int(server['server_port']))
  238. master.set_timeout(6.0)
  239. while(True):
  240. self.master(master, server['id'])
  241. def test2(self):
  242. master = modbus_tcp.TcpMaster(host="192.168.33.10", port=1152)
  243. master.set_timeout(3) #timeout表示若超过3秒没有连接上slave就会自动断开
  244. tempr = master.execute(1, modbus_tk.defines.READ_HOLDING_REGISTERS, 0, 2)
  245. #(slave id,只读,block地址,长度:即字节乘个数)
  246. Demeter.echo('tempr:' + str(tempr) + 'size:' + str(len(tempr)))
  247. value = struct.unpack('>f', struct.pack('>HH', tempr[0], tempr[1]))
  248. #在slave中,是先打包成'>f'在以'>HH'解包的,在master中刚好相反
  249. #struct.unpack里解出来的是一个元组,可以用bb, = 或者输出bb[0]
  250. print(value)
  251. Demeter.echo('value:' + str(round(value[0], 2)))
  252. time.sleep(3)
  253. def test1(self):
  254. master = modbus_tcp.TcpMaster(host="192.168.33.10", port=1152)
  255. master.set_timeout(3) #timeout表示若超过3秒没有连接上slave就会自动断开
  256. aa = master.execute(1, modbus_tk.defines.ANALOG_INPUTS, 0, 2)
  257. #(slave id,只读,block地址,长度:即字节乘个数)
  258. Demeter.echo('aa:' + str(aa) + 'size:' + str(len(aa)))
  259. bb = struct.unpack('>f', struct.pack('>HH', aa[0], aa[1]))
  260. #在slave中,是先打包成'>f'在以'>HH'解包的,在master中刚好相反
  261. #struct.unpack里解出来的是一个元组,可以用bb, = 或者输出bb[0]
  262. Demeter.echo('bb:' + str(bb[0]))
  263. time.sleep(3)
  264. slave = 1
  265. # 读线圈寄存器
  266. master.execute(slave, mdef.READ_COILS, 0, 16)
  267. # 读离散输入寄存器
  268. master.execute(slave, mdef.READ_DISCRETE_INPUTS, 0, 16)
  269. # 读保持寄存器
  270. master.execute(slave, mdef.READ_HOLDING_REGISTERS, 0, 16)
  271. # 读输入寄存器
  272. master.execute(slave, mdef.READ_INPUT_REGISTERS, 0, 16)
  273. # 单个读写寄存器操作
  274. # 写单个线圈 写寄存器地址为0的线圈寄存器,写入内容为0(位操作)
  275. master.execute(slave, mdef.WRITE_SINGLE_COIL, 0, output_value=0)
  276. master.execute(slave, mdef.READ_COILS, 0, 1)
  277. # 写单个保持寄存器 写寄存器地址为0的保持寄存器
  278. master.execute(slave, mdef.WRITE_SINGLE_REGISTER, 0, output_value=21)
  279. master.execute(slave, mdef.READ_HOLDING_REGISTERS, 0, 1)
  280. # 多个寄存器读写操作
  281. # 写多个保持寄存器 写寄存器起始地址为0的保持寄存器,操作寄存器个数为4
  282. master.execute(slave, mdef.WRITE_MULTIPLE_REGISTERS, 0, output_value=[20,21,22,23])
  283. master.execute(slave, mdef.READ_HOLDING_REGISTERS, 0, 4)
  284. # 写寄存器起始地址为0的线圈寄存器
  285. master.execute(slave, mdef.WRITE_MULTIPLE_COILS, 0, output_value=[0,0,0,0])
  286. master.execute(slave, mdef.READ_COILS, 0, 4)
  287. # slave_id,类型,地址,长度(或者值)、读取还是写入、计算公式