prepared for modbus

This commit is contained in:
Philipp Rauch 2013-12-13 12:34:24 +01:00
parent 149b702e05
commit 18b80015a8
6 changed files with 50 additions and 60 deletions

View file

@ -4,7 +4,6 @@ Created on 13.11.2013
@author: Philipp Rauch @author: Philipp Rauch
@version: 0.1 @version: 0.1
''' '''
from CanMessage import CanMessage from CanMessage import CanMessage
from CanSignal import CanSignal from CanSignal import CanSignal
from PCan import PcanAdapter from PCan import PcanAdapter

View file

@ -4,6 +4,7 @@ Created on 21.11.2013
@author: Philipp Rauch @author: Philipp Rauch
''' '''
from sys import stderr from sys import stderr
config = "config\ems.conf" config = "config\ems.conf"
class Config(): class Config():
@ -94,6 +95,8 @@ class Config():
line = line.lstrip('\'') line = line.lstrip('\'')
# if master is not None: # if master is not None:
key, arg = self.getElement(line) key, arg = self.getElement(line)
if key == None:
continue #skip errors
res = [] res = []
for a in arg: for a in arg:
tmp = a.split(':') tmp = a.split(':')
@ -109,7 +112,7 @@ class Config():
elif line.find("=") != -1: elif line.find("=") != -1:
key, arg = self.getElement(line) key, arg = self.getElement(line)
if key == None: if key == None:
continue continue #skip errors
self.makeList(arg) self.makeList(arg)
arg = arg if len(arg) > 1 else arg[0] arg = arg if len(arg) > 1 else arg[0]

View file

@ -4,7 +4,6 @@ Created on 15.11.2013
@author: Philipp Rauch @author: Philipp Rauch
@version: 0.02 @version: 0.02
''' '''
# from sys import stderr
from threading import Thread from threading import Thread
from switch import Switch, MYSQL from switch import Switch, MYSQL
from config import Config from config import Config
@ -55,41 +54,6 @@ class ems(Thread):
tmp = self.queue.get() tmp = self.queue.get()
return tmp 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): def getRequest(self):
#TODO: get Request from buffer #TODO: get Request from buffer
#TODO: define a request in buffer #TODO: define a request in buffer

View file

@ -3,7 +3,6 @@ Created on 26.11.2013
@author: Philipp Rauch @author: Philipp Rauch
''' '''
from MySQLdb import connect from MySQLdb import connect
def setup(conf): def setup(conf):
@ -41,3 +40,6 @@ def loop(cursor, item):
result = {item : result} result = {item : result}
return result return result
def close(cursor):
pass

View file

@ -3,24 +3,39 @@ Created on 05.12.2013
@author: Philipp Rauch @author: Philipp Rauch
''' '''
from pymodbus.client.sync import ModbusTcpClient from pymodbus.client.sync import ModbusTcpClient
PAC01 = ModbusTcpClient('10.2.6.5') PAC01, PAC02, PAC03, PAC04 = None, None, None, None
PAC02 = ModbusTcpClient('10.2.6.6')
PAC03 = ModbusTcpClient('10.2.6.7')
PAC04 = ModbusTcpClient('10.2.6.8')
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) ### CONNECT ###
# result = PAC01.read_coils(213,1,unit=2) PAC01.connect()
PAC02.connect()
PAC03.connect()
PAC04.connect()
a = PAC01.write_registers(213,(1,0),unit=2)
print "Antwort: %s" % a def loop(cursor, item):
res = PAC01.read_holding_registers(213,4,unit=2) # PAC01.write_coil(213, 100,2)
print "Werte: %s" % res.registers # result = PAC01.read_coils(213,1,unit=2)
# print result.bits[0] ans = PAC01.write_registers(213,(1,0),unit=2)
PAC01.close() 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()

View file

@ -4,7 +4,6 @@ Created on 15.11.2013
@author: Philipp Rauch @author: Philipp Rauch
@version: 0.1 @version: 0.1
''' '''
from threading import Thread from threading import Thread
from Queue import Queue from Queue import Queue
from time import sleep from time import sleep
@ -12,11 +11,10 @@ from config import Config
#import Module #import Module
import database import database
import modbus
MYSQL = 0 MYSQL = 0
CSV = 1 MODBUS = 1
XML = 2
JSON = 3
### LOAD CONFIG ### ### LOAD CONFIG ###
c = Config() c = Config()
@ -33,7 +31,10 @@ class Switch(Thread):
self.source = source self.source = source
def initialisiere(self): def initialisiere(self):
if self.source == MYSQL:
self.cursor = database.setup(conf) self.cursor = database.setup(conf)
elif self.source == MODBUS:
self.cursor = modbus.setup(conf)
### init Query ### ### init Query ###
self.query = Queue() self.query = Queue()
@ -55,12 +56,18 @@ class Switch(Thread):
# query should be included in the result # query should be included in the result
item = 'dc_labor/device/battery' item = 'dc_labor/device/battery'
if self.source == MYSQL:
result = database.loop(self.cursor, item) result = database.loop(self.cursor, item)
elif self.source == MODBUS:
result = modbus.loop(self.cursor, item)
#print 'PUT:\t%s' % result #print 'PUT:\t%s' % result
self.queue.put(result) self.queue.put(result)
if self.source == MYSQL:
sleep(float(conf['mySQL_speed'])) sleep(float(conf['mySQL_speed']))
elif self.source == MODBUS:
sleep(0.1)
def getItem(self, block = False): def getItem(self, block = False):
return self.queue.get(block) return self.queue.get(block)