[Tutor] Hello! Questions

Marco Soldavini magyar1886 at gmail.com
Fri Feb 19 02:51:30 EST 2016


Sorry, Here my code in plaintext

#While loop - scanning and storing OPC values at scan rate
while (abort == 0):

    # ESC pressed?
    if msvcrt.kbhit() and ord(msvcrt.getch()) == 27:
        abort = 1
        break

    # Server up
    if opc.ping():


        if opc['.run_batch'] == True and rec_started == False:
            # Setting arrays for variables
            bool1 = []
            ana1 = []
            ana2 = []
            ana3 = []
            ana4 = []
            rec_started = True

        if opc['.run_batch'] == True and rec_started == True:
            # scan time
            time2 = time.time()
            dtime = time2 - time1

            if dtime > 2 and comm_alarm == False:
                dt = datetime.datetime.now()
                bool1.append((opc.read('.watchdog')[0],opc.read('.watchdog')[1],dt))
                ana1.append((opc.read('.analog2')[0],opc.read('.analog2')[1],dt))
                time1 = time2
                

    else:
        # scan time
        time2 = time.time()
        dtime = time2 - time1
        if dtime > 2:
            print "ERROR: OPC Server is down”



Il giorno 19 febbraio 2016 @ 02:18:05, Alan Gauld (alan.gauld at btinternet.com) ha scritto:
> Let's say I can have 30-40 variables (so 30-40 append instructions at every 
> cycle, with same scan rate). 

Its not clear what the scan rate actually is. 
How many scans per second? 


yes scan per second. For example a new fetch every 2 second, so a new append to array every 5 second



> shall run for 2 hours and gather let's say 2000 elements in 40 arrays, 
> could this be a problem in term of computation? 

Yes it could, but it depends on what size the data is. 
You need to do some basic math to calculate it out. 

40 * 2000 = 80k items. If they are integers then its 
4 bytes per item so 320Kbytes. Not too bad. If they 
are 100 character strings then its into MB but on a 
modern PC still not too bad. But if it's intended to 
run in an embedded controller with only 1M of RAM 
it might be a big issue. 


Pc for sure, I think with 8GB RAM. I’ll do some detailed calculations about that.

> Second question is I want the arguments in the opc.read command not to be 
> hard coded but coming from a configuration files. 

OK. You might want to think about what that file 
format would look like. 


I’d like to have an xml file or csv file to parse (another topic I wanna learn) and in this file I have a table defining my variables with a name and another field for description


> You see the names of my arrays? bool1, ana1, ana2, etc... Is it possible to 
> derive number and names of these variables from an external files. 

It is but its usually a bad idea. 
Better is to use a dictionary with your "variables" 
as keys and your arrays as values. So your append 
looks like 

data = dict() 
keyName = readFromFile() 
value = readFromFile() 

data[keyName].append(value) 
Ok so I will look more into dictionaries, it is like hash tables?





> Let's say in this configuration file I can say I have to read 10 arrays or 
> 20 arrays and then my program adjust the while cycle consequently. 

Yes that's doable. 

> Maybe an array of array where the second dimension is coming from the 
> config file. 

I'd use the dictionary approach rather than arrays of arrays. 
(Which are probably lists of lists in Python.) 


Thanks!
Marco


More information about the Tutor mailing list