CDB Python

Hello,

I try to read some information from the cdbase using python.
The first requests already work. rpar, p,x,y and z are output correctly. Now I want to read the data from my HIST graphs. In the database it looks like this for LC 201 for example:

This is my code:

# +============================================================================+
# | Company:   SOFiSTiK AG                                                     |
# | Version:   SOFiSTIK 2020                                                   |
# +============================================================================+

# import all types from sofistik_daten.py, original file can be found by following
# --> examples/python/sofistik_daten.py
from sofistik_daten import *
import os             # for the environment variable necessary
import platform       # checks the python platform
from ctypes import *  # read the functions from the cdb

########################################################### import:
sofPlatform = str(platform.architecture())

os.add_dll_directory(r"C:\Program Files\SOFiSTiK\2020\SOFiSTiK 2020\interfaces\64bit")
os.add_dll_directory(r"C:\Program Files\SOFiSTiK\2020\SOFiSTiK 2020")

# Get the DLL functions
myDLL = cdll.LoadLibrary("sof_cdb_w-70.dll")
py_sof_cdb_get = cdll.LoadLibrary("sof_cdb_w-70.dll").sof_cdb_get
py_sof_cdb_get.restype = c_int

py_sof_cdb_kenq = cdll.LoadLibrary("sof_cdb_w-70.dll").sof_cdb_kenq_ex
py_sof_cdb_kexist = cdll.LoadLibrary("sof_cdb_w-70.dll").sof_cdb_kexist

# Connect to CDB
Index = c_int()
cdbIndex = 99

# input the cdb path here
fileName = r"C:\Users\...\Zugriff cdb\Modell 1_MF_2-0.cdb" # hier fileName ändern

# important: Unicode call!
Index.value = myDLL.sof_cdb_init(fileName.encode('utf8'), cdbIndex)
#print(Index.value) # 1

# get the CDB status
cdbStat = c_int()
cdbStat.value = myDLL.sof_cdb_status(Index.value)
print ("CDB Status:", cdbStat.value)

########################################################### get data:
# Lastverlauf LZUG

# Zeitpunkt [sec]
rpar = []
for LC in range(10000,10233+1):
    datalen = c_int(0)
    ie = c_int(0)
    datalen.value = sizeof(CLC_CTRL)
    RecLen = c_int(sizeof(clc_ctrl))
    ie.value = py_sof_cdb_get(Index, 12, LC, byref(clc_ctrl), byref(RecLen), 1)
    rpar.append(clc_ctrl.m_rpar)

#print('rpar:')
#print(rpar)

p = []
x = []
y = []
z = []
for LC in range(10000,10233+1):
    if py_sof_cdb_kexist(12, LC) == 2: # the key exists and contains data 12:global load 10000: LC (ggf. ändern oder variabel gestalten) 10000-10233
        ie = c_int(0)
        RecLen = c_int(sizeof(clc_poin))
        ie.value = py_sof_cdb_get(Index, 12, LC, byref(clc_poin), byref(RecLen), 1)
        p.append(clc_poin.m_lpt.m_p)      
        x.append(clc_poin.m_lpt.m_x)
        y.append(clc_poin.m_lpt.m_y)
        z.append(clc_poin.m_lpt.m_z)
        RecLen = c_int(sizeof(clc_poin_lpt))

#print('p:')
#print(p)
#print('x:')
#print(x)
#print('y:')
#print(y)
#print('z:')
#print(z)

# Spannungen an konkreten Punkten auslesen (HIST)
sig_tt = []
sig = []
for LC in range(201,209+1):
    if py_sof_cdb_kexist(80, LC) == 2: # the key exists and contains data 12:global load 201: LC (ggf. ändern oder variabel gestalten) 201-209
        ie = c_int(0)
        RecLen = c_int(sizeof(chist_val))
        ie.value = py_sof_cdb_get(Index, 80, LC, byref(chist_val), byref(RecLen), 4)
        sig_tt.append(chist_val.m_tt)
        sig.append(chist_val.m_vb[0])
        RecLen = c_int(sizeof(chist_val))

print('sig_tt:')
print(sig_tt)
print('sig:')
print(sig)

########################################################### close cdb:
# Close the CDB, 0 - will close all the files
myDLL.sof_cdb_close(0)

# Print again the status of the CDB, if status = 0 -> CDB Closed successfully
cdbStat.value = myDLL.sof_cdb_status(Index.value)
if cdbStat.value == 0:
    print ("CDB closed successfully, status = 0")

I get CDB Status: 3 sig_tt: [-100.0, -100.0, -100.0, -100.0, -100.0, -100.0, -100.0, -100.0, -100.0] sig: [1.064986832886861e-43, 1.064986832886861e-43, 1.064986832886861e-43, 1.064986832886861e-43, 1.064986832886861e-43, 1.064986832886861e-43, 1.064986832886861e-43, 1.064986832886861e-43, 1.064986832886861e-43] CDB closed successfully, status = 0

even though this is not the data I want or that is written in the database. What does that tell me? Is it calling c_hist even though I am calling chist_val? Thanks for any help?

The cdb reading adjust mainly according to the kwh/kwl keys (e.g. 80/201 in your picture).

If you the try to get data and your struct has the wrong format → the return structure (chist_val) is adjusted automatically.
The next time you read the same kwh/kwl it will read the next line (-5 ...)

So if you want to find something further done the list, you should loop over
ie.value = py_sof_cdb_get(Index, 80, LC,...
until you get what you want.
E.g. that tt is >0 or a specific value

Or you loop through the entire thing (if you want all the values) until the ie.value is 2 (end of file)

Thanks a lot. Now I understood and it works well!