prepared for modbus
This commit is contained in:
parent
149b702e05
commit
18b80015a8
6 changed files with 50 additions and 60 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
||||
|
|
|
|||
36
src/ems.py
36
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue