From 18b80015a89293d992193c13c7d4442ab454a2c1 Mon Sep 17 00:00:00 2001 From: Philipp Rauch Date: Fri, 13 Dec 2013 12:34:24 +0100 Subject: [PATCH] prepared for modbus --- src/CANFilter.py | 1 - src/config.py | 5 ++++- src/ems.py | 36 ---------------------------------- src/modules/database.py | 4 +++- src/modules/modbus.py | 43 +++++++++++++++++++++++++++-------------- src/switch.py | 21 +++++++++++++------- 6 files changed, 50 insertions(+), 60 deletions(-) diff --git a/src/CANFilter.py b/src/CANFilter.py index 21802a4..eae9a5f 100644 --- a/src/CANFilter.py +++ b/src/CANFilter.py @@ -4,7 +4,6 @@ Created on 13.11.2013 @author: Philipp Rauch @version: 0.1 ''' - from CanMessage import CanMessage from CanSignal import CanSignal from PCan import PcanAdapter diff --git a/src/config.py b/src/config.py index cd40fc1..5ab52dd 100644 --- a/src/config.py +++ b/src/config.py @@ -4,6 +4,7 @@ Created on 21.11.2013 @author: Philipp Rauch ''' from sys import stderr + config = "config\ems.conf" class Config(): @@ -94,6 +95,8 @@ class Config(): line = line.lstrip('\'') # if master is not None: key, arg = self.getElement(line) + if key == None: + continue #skip errors res = [] for a in arg: tmp = a.split(':') @@ -109,7 +112,7 @@ class Config(): elif line.find("=") != -1: key, arg = self.getElement(line) if key == None: - continue + continue #skip errors self.makeList(arg) arg = arg if len(arg) > 1 else arg[0] diff --git a/src/ems.py b/src/ems.py index af80b63..f229eb1 100644 --- a/src/ems.py +++ b/src/ems.py @@ -4,7 +4,6 @@ Created on 15.11.2013 @author: Philipp Rauch @version: 0.02 ''' -# from sys import stderr from threading import Thread from switch import Switch, MYSQL from config import Config @@ -55,41 +54,6 @@ class ems(Thread): tmp = self.queue.get() return tmp -# def update_buffer(self, push): -# ''' -# Method to update the Buffer on the given path -# @param push: message to push in the buffer -# construction: key is the path -# value is the dict -# ''' -# -# ## Test of valid push message ## -# if not isinstance(push, dict): -# stderr.write('error wrong parameter: Type is %s expect dict' % push.__class__.__name__) -# return -# if len(push.keys()) not in [1]: -# stderr.write('error wrong number of arguments: %s expect 1' % len(push.keys())) -# return -# if not isinstance(push.get(push.keys()[0]) ,dict): -# stderr.write('error value is not dict') -# return -# -# key = push.keys()[0] -# value = push[key] -# path = key.split('/') -# if path[0] == '': -# path.remove('') -# -# sys = self.buffer.system -# for key in path: -# try: -# sys = sys[key] -# except KeyError: -# stderr.write('error wrong path: %s' % key) -# return -# -# sys.update(value) - def getRequest(self): #TODO: get Request from buffer #TODO: define a request in buffer diff --git a/src/modules/database.py b/src/modules/database.py index 0de2e0a..364d1cc 100644 --- a/src/modules/database.py +++ b/src/modules/database.py @@ -3,7 +3,6 @@ Created on 26.11.2013 @author: Philipp Rauch ''' - from MySQLdb import connect def setup(conf): @@ -41,3 +40,6 @@ def loop(cursor, item): result = {item : result} return result + +def close(cursor): + pass diff --git a/src/modules/modbus.py b/src/modules/modbus.py index a5fe344..8e223fb 100644 --- a/src/modules/modbus.py +++ b/src/modules/modbus.py @@ -3,24 +3,39 @@ Created on 05.12.2013 @author: Philipp Rauch ''' - from pymodbus.client.sync import ModbusTcpClient -PAC01 = ModbusTcpClient('10.2.6.5') -PAC02 = ModbusTcpClient('10.2.6.6') -PAC03 = ModbusTcpClient('10.2.6.7') -PAC04 = ModbusTcpClient('10.2.6.8') +PAC01, PAC02, PAC03, PAC04 = None, None, None, None -PAC01.connect() +def setup(conf): + ### INITIALIZE ### + PAC01 = ModbusTcpClient('10.2.6.5') + PAC02 = ModbusTcpClient('10.2.6.6') + PAC03 = ModbusTcpClient('10.2.6.7') + PAC04 = ModbusTcpClient('10.2.6.8') -# PAC01.write_coil(213, 100,2) -# result = PAC01.read_coils(213,1,unit=2) + ### CONNECT ### + PAC01.connect() + PAC02.connect() + PAC03.connect() + PAC04.connect() -a = PAC01.write_registers(213,(1,0),unit=2) -print "Antwort: %s" % a -res = PAC01.read_holding_registers(213,4,unit=2) -print "Werte: %s" % res.registers +def loop(cursor, item): + # PAC01.write_coil(213, 100,2) + # result = PAC01.read_coils(213,1,unit=2) -# print result.bits[0] -PAC01.close() + ans = PAC01.write_registers(213,(1,0),unit=2) + print "Antwort: %s" % ans + + res = PAC01.read_holding_registers(213,4,unit=2) + print "Werte: %s" % res.registers + + # print result.bits[0] + +def close(): + ### CLOSE ### + PAC01.close() + PAC02.close() + PAC03.close() + PAC04.close() diff --git a/src/switch.py b/src/switch.py index 8167983..1a821dd 100644 --- a/src/switch.py +++ b/src/switch.py @@ -4,7 +4,6 @@ Created on 15.11.2013 @author: Philipp Rauch @version: 0.1 ''' - from threading import Thread from Queue import Queue from time import sleep @@ -12,11 +11,10 @@ from config import Config #import Module import database +import modbus MYSQL = 0 -CSV = 1 -XML = 2 -JSON = 3 +MODBUS = 1 ### LOAD CONFIG ### c = Config() @@ -33,7 +31,10 @@ class Switch(Thread): self.source = source def initialisiere(self): - self.cursor = database.setup(conf) + if self.source == MYSQL: + self.cursor = database.setup(conf) + elif self.source == MODBUS: + self.cursor = modbus.setup(conf) ### init Query ### self.query = Queue() @@ -55,12 +56,18 @@ class Switch(Thread): # query should be included in the result item = 'dc_labor/device/battery' - result = database.loop(self.cursor, item) + if self.source == MYSQL: + result = database.loop(self.cursor, item) + elif self.source == MODBUS: + result = modbus.loop(self.cursor, item) #print 'PUT:\t%s' % result self.queue.put(result) - sleep(float(conf['mySQL_speed'])) + if self.source == MYSQL: + sleep(float(conf['mySQL_speed'])) + elif self.source == MODBUS: + sleep(0.1) def getItem(self, block = False): return self.queue.get(block)