modbus spalten lookup
This commit is contained in:
parent
d7fec1cb76
commit
02cd58b640
6 changed files with 45 additions and 35 deletions
|
|
@ -7,7 +7,7 @@ Created on 13.11.2013
|
|||
@version: 0.1
|
||||
'''
|
||||
from PCan import PcanAdapter
|
||||
from Sym2Lib import Add2Adapter, get_DataFrameDict
|
||||
from Sym2Lib import Add2Adapter, _get_easy_Dict_, readSym
|
||||
from datetime import datetime
|
||||
from time import sleep
|
||||
from threading import Thread
|
||||
|
|
@ -23,6 +23,14 @@ def CAN_start(conf):
|
|||
can = CANFilter(conf)
|
||||
can.start()
|
||||
|
||||
def get_DataFrameDict(symfile):
|
||||
mes = readSym(symfile)
|
||||
symDict = _get_easy_Dict_(mes)
|
||||
dfDict = {}
|
||||
for i in symDict.keys():
|
||||
dfDict.update( { i : DataFrame(columns = symDict[i]) } )
|
||||
return dfDict
|
||||
|
||||
class CANFilter(Thread):
|
||||
|
||||
### Lookup für CAN --> DB ###
|
||||
|
|
@ -66,16 +74,16 @@ class CANFilter(Thread):
|
|||
|
||||
def run(self):
|
||||
while True:
|
||||
receiveMessageName = self.pcan.receiveMessage(block = True)##
|
||||
|
||||
receiveMessageName = self.pcan.receiveMessage(block = True)
|
||||
|
||||
try:
|
||||
tablename = self.lookup[receiveMessageName]
|
||||
except:
|
||||
tablename = receiveMessageName
|
||||
|
||||
if receiveMessageName is None:
|
||||
continue
|
||||
|
||||
|
||||
# if receiveMessageName is None or self.pcan.Messages[receiveMessageName].getFlag('RECEIVE'):
|
||||
# continue
|
||||
|
||||
#print "%s - %s" % (datetime.now(), tablename)
|
||||
|
||||
for sym in self.symList:
|
||||
|
|
@ -103,4 +111,4 @@ class CANFilter(Thread):
|
|||
sym[receiveMessageName] = sym[receiveMessageName].drop(sym[receiveMessageName].index[:])
|
||||
print "%s - Write to DB - %s" % (res['DateTime'][0], tablename)
|
||||
|
||||
#sleep(0.01)
|
||||
#sleep(0.01)
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@ Created on 26.11.2013
|
|||
from profile.database import Database
|
||||
|
||||
_sort_lookup = { 'EA_Last' : 'DateTime',
|
||||
'I_IST_WERT' : 'DateTime',
|
||||
'I_SOLL_WERT' : 'DateTime',
|
||||
'GRIDManager_I_IST' : 'DateTime',
|
||||
'GRIDManager_I_PH' : 'DateTime',
|
||||
'GRIDManager_Status' : 'DateTime',
|
||||
'GRIDManager_Temperatur': 'DateTime',
|
||||
'GRIDManager_U_IST': 'DateTime',
|
||||
'PAC' : 'date_/_time',
|
||||
'STATUS_FPGA' : 'DateTime',
|
||||
'U_IST_WERT': 'DateTime',
|
||||
'pCharger': 'DateTime'}
|
||||
|
||||
def setup(conf):
|
||||
|
|
|
|||
|
|
@ -10,6 +10,13 @@ from profile.datasheet import Datasheet
|
|||
import struct
|
||||
import numpy
|
||||
|
||||
_csv_lookup = { 'offset' : 'offset',
|
||||
'type' : 'value - type',
|
||||
'size' : 'size (Byte)',
|
||||
'name' : 'Value name'
|
||||
'time_offset' : 797}
|
||||
|
||||
|
||||
def setup(conf):
|
||||
PACS = []
|
||||
csv = '%s/%s' % (conf['config_dictionary'], conf['pac_csvfile'])
|
||||
|
|
@ -29,18 +36,18 @@ def setup(conf):
|
|||
### CONNECT ###
|
||||
for PAC in PACS[1::]:
|
||||
PAC.connect()
|
||||
_set_time(PAC)
|
||||
_set_time(PAC, _csv_lookup['time_offset'])
|
||||
|
||||
return PACS
|
||||
|
||||
def loop(PACS, item):
|
||||
|
||||
row = PACS[0][PACS[0]['offset'] == item[1]]
|
||||
row = PACS[0][PACS[0][_csv_lookup['offset']] == item[1]]
|
||||
inx = row.index.values[0]
|
||||
|
||||
#print row.get('value - type')[inx]
|
||||
valType = row.get('value - type')[inx]
|
||||
reg = row.get('size (Byte)')[inx]/2
|
||||
valType = row.get(_csv_lookup['type'])[inx]
|
||||
reg = row.get(_csv_lookup['size'])[inx]/2
|
||||
|
||||
type_picker = {'double' : _to_double,
|
||||
'float' : _to_float,
|
||||
|
|
@ -55,7 +62,7 @@ def loop(PACS, item):
|
|||
raise NotImplementedError
|
||||
|
||||
res = PACS[item[0]].read_holding_registers(item[1], reg, unit=1)
|
||||
return { row.get('Value name')[inx] : func(res.registers) }
|
||||
return { row.get(_csv_lookup['name'])[inx] : func(res.registers) }
|
||||
|
||||
|
||||
def _to_float_time(reg):
|
||||
|
|
@ -89,11 +96,11 @@ def _to_float(reg):
|
|||
s = struct.pack('=i', tmp)
|
||||
return numpy.float32(struct.unpack('=f', s)[0])
|
||||
|
||||
def _set_time(PAC):
|
||||
def _set_time(PAC, offset):
|
||||
stamp = time.time()
|
||||
high = int(int(stamp) >> 16)
|
||||
low = int(int(stamp) & int(0xFFFF))
|
||||
print PAC.write_registers(797, (high, low, 0, 0), unit=1)
|
||||
print PAC.write_registers(offset, (high, low, 0, 0), unit=1)
|
||||
|
||||
def close(PACS):
|
||||
### CLOSE ###
|
||||
|
|
|
|||
|
|
@ -13,8 +13,9 @@ from Config.parser import config
|
|||
import database
|
||||
import modbus
|
||||
|
||||
MYSQL = 0
|
||||
MODBUS = 1
|
||||
#set source modules
|
||||
MYSQL = database
|
||||
MODBUS = modbus
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = config()
|
||||
|
|
@ -39,11 +40,9 @@ class Switch(Thread):
|
|||
|
||||
@return: sql_queue and sql_query
|
||||
'''
|
||||
if self.source == MYSQL:
|
||||
self.cursor = database.setup(conf)
|
||||
elif self.source == MODBUS:
|
||||
self.cursor = modbus.setup(conf)
|
||||
else:
|
||||
try:
|
||||
self.cursor = self.source.setup(conf)
|
||||
except:
|
||||
raise NotImplementedError
|
||||
|
||||
### init Query ###
|
||||
|
|
@ -70,10 +69,7 @@ class Switch(Thread):
|
|||
|
||||
item = self.query.get(block = True)
|
||||
|
||||
if self.source == MYSQL:
|
||||
result = database.loop(self.cursor, item)
|
||||
elif self.source == MODBUS:
|
||||
result = modbus.loop(self.cursor, item)
|
||||
result = self.source.loop(self.cursor, item)
|
||||
|
||||
#print 'PUT:\t%s' % result
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ mySQL_user = stud_EMS
|
|||
mySQL_pass = sql13
|
||||
mySQL_database = ems_testdb # photodb
|
||||
mySQL_speed = 0.02 # time between two query's in sec # default 0.02
|
||||
#sdgsgssth
|
||||
|
||||
#### FLASK ####
|
||||
flask_server = 0.0.0.0 # 0.0.0.0 for public access # default 0.0.0.0
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ for item in conf['pac_messages']:
|
|||
|
||||
### CREATE DATAFRAME ###
|
||||
tab = DataFrame(columns = col)
|
||||
|
||||
### Modbus ###
|
||||
modbus = Switch(MODBUS)
|
||||
modbus.initialisiere()
|
||||
|
|
@ -52,9 +51,9 @@ while True:
|
|||
for mes in conf['pac_messages']:
|
||||
modbus.query.put([1, int(mes)])
|
||||
res.update(modbus.queue.get())
|
||||
|
||||
print datetime.now() + " " + res.keys()
|
||||
|
||||
print "%s - %s" % (datetime.now(), res.keys())
|
||||
res_data = tab.append(res, ignore_index=True)
|
||||
res_data[col[1::]] = res_data[col[1::]].astype(numpy.float) #till now only float support
|
||||
db.writeDatabase('PAC', res_data, bClear = False)
|
||||
sleep(1)
|
||||
#sleep(0.6)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue