1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- # -*- coding: utf-8 -*-
- from demeter.core import *
- import modbus_tk
- import modbus_tk.modbus_rtu as modbus_rtu
- import modbus_tk.defines as mdef
- import time
- import random
- import struct
- import serial
- class Rtu(object):
- # 启动客户端
- def start(self):
- while(True):
- self.run()
- time.sleep(100)
- def run(self):
- server = Demeter.service('common').list('setting_server', search={'server_type':3})
- if server:
- modbus = Demeter.path + 'modbus.py'
- for v in server:
- command = 'python3 ' + modbus + ' -m rtu_client -i ' + str(v['id'])
- check = "ps aux|grep '"+command+"'|grep -v entrypoint|grep -v grep|grep -v process|awk '{print $1}'"
- check = Shell.popen(check)
- if not check:
- Shell.popen(command, sub=True, bg=True)
- def client(self, id):
- server = Demeter.service('common').one('setting_server', id=id)
- port,baudrate = self.port(server['server_port'])
- master = modbus_rtu.RtuMaster(serial.Serial(port, baudrate=baudrate, bytesize=8, parity='N', stopbits=1, xonxoff=0))
- Demeter.service('handle', 'modbus').set(master, server)
- def send(self, server, type_info, info, value):
- master = modbus_rtu.RtuMaster(serial.Serial(port, baudrate=baudrate, bytesize=8, parity='N', stopbits=1, xonxoff=0))
- Demeter.service('handle', 'modbus').send(master, server, type_info, info, value)
- def port(self, port):
- baudrate = 4800
- if ':' in port:
- data = port.split(':')
- port = data[0]
- baudrate = int(data[1])
- return (port, baudrate)
|