[Tutor] Hello! Questions

Peter Otten __peter__ at web.de
Fri Feb 19 09:32:09 EST 2016


Marco Soldavini wrote:

Random remarks about your code:

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

The loop continues to work if you change it to

while True:
 
>     # 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:

In idiomatic Python you don't compare boolean values, you test:

          if opc['.run_batch'] and not rec_started:

>             # 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))

Invoking opc.read(".watchdog") twice looks wrong, the two values might be 
out of sync. I suspect that you want

                  result = opc.read(".watchdog")
                  bool1.append((result[0], result[1], dt))

Also, after reading http://openopc.sourceforge.net/api.html I wonder if it 
wouldn't be better to go with the timestamp provided by the server

                  bool1.append(opc.read(".watchdog"))

With a slight modification of Alan's suggestion you could write to a list of 
dicts instead of a dict of lists like so:

# outside the loop
WANTED = [".watchdog", ".analog1", ".analog2]
data = []

# in the loop:
                 data.append({r[0]:r[1:] for r in opc.read(WANTED)})

>                 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”
> 




More information about the Tutor mailing list