SP2000TR, update from a script, dynamic attributes

The SP2000TR laser interferometer continuously sends data. A script captures the data, parses them and updates dynamic attributes of a VcExecutor.

The VcExecutor code:

 
#!/bin/env python
"""
this VcExecutor exports some variables read from a SP2000TR interferometer
by the script SP2000TRScript.py
"""
import PyTango

class VC:
    def __init__(self):
	print " VC.init"
        self.pos_ch1 = 0
        self.pos_ch2 = 0
        self.pos_ch3 = 0
        self.sig_ch1 = 0
        self.sig_ch2 = 0
        self.sig_ch3 = 0
    #
    # dev_state
    #
    def dev_state( self):
	return PyTango.DevState.ON
    #
    # Counts
    #
    def read_Counts( self):
	return 1

    def write_Counts( self, argin):
	return 1
    #
    # reset
    #
    def Reset(self):
	return True

    def read_DynamicAttr(self, name):
        if name == "pos_ch1":
            return self.pos_ch1
        elif name == "pos_ch2":
            return self.pos_ch2
        elif name == "pos_ch3":
            return self.pos_ch3
        elif name == "sig_ch1":
            return self.sig_ch1
        elif name == "sig_ch2":
            return self.sig_ch2
        elif name == "sig_ch3":
            return self.sig_ch3
        else: 
            raise ValueError( "SP200TK: failed to identify attribute name, %s" % name)
        
    def write_DynamicAttr(self, name, value):
        if name == "pos_ch1":
            self.pos_ch1 = value
        elif name == "pos_ch2":
            self.pos_ch2 = value
        elif name == "pos_ch3":
            self.pos_ch3 = value
        elif name == "sig_ch1":
            self.sig_ch1 = value
        elif name == "sig_ch2":
            self.sig_ch2 = value
        elif name == "sig_ch3":
            self.sig_ch3 = value
        else: 
            raise ValueError( "SP200TK: failed to identify attribute name, %s" % name)

        return

The script that feeds the VcExecutor

 
#!/usr/bin/env python
"""
this scripts reads data from a SP2000TR and writes them to the
attributes of a VcExecutor
DynamicAttributes (Property) 
  pos_ch1,double,rw
  pos_ch2,double,rw
  pos_ch3,double,rw
  sig_ch1,double,rw
  sig_ch2,double,rw
  sig_ch3,double,rw
"""
import socket, select, PyTango, time

DEVICE = "p99/vcexecutor/eh.06"
HOST = "hasep99varex01"
PORT = 35320

def main(): 

    proxy = PyTango.DeviceProxy( DEVICE)

    sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)

    server_address = ( HOST, PORT)
    sock.settimeout( 3)
    try: 
        sock.connect(server_address)
    except Exception as e: 
        print( "SP2000TRScript: failed to connect to %s, %s " % ( HOST, PORT))
        return 

    sock.sendall( "\n")

    inout = [ sock]

    argout = ""
    count = 0
    while 1: 
        infds, outfds, errfds = select.select( inout, [], [], 0.12)
        if len( infds) == 0:
            break
        argout = sock.recv( 1024)
        lines = argout.split( '\r\n')
        for line in lines:
            count += 1
            words = line.split( ' ')
            if len( words) < 4:
                break
            try:
                # 
                # expecting these strings
                #   VAL003 pos_ch1 pos_ch2 pos_ch3
                #   STA003 sig_ch1 sig_ch2 sig_ch3
                #
                # ignore other input
                #
                if words[0] == 'VAL003':
                    proxy.pos_ch1 = float( words[1])
                    proxy.pos_ch2 = float( words[2])
                    proxy.pos_ch3 = float( words[3])
                    #print( "VAL003: %g, %g, %g" % ( float( words[1]), float( words[2]), float( words[3])))
                if words[0] == 'STA003': 
                    proxy.sig_ch1 = float( words[1])
                    proxy.sig_ch2 = float( words[2])
                    proxy.sig_ch3 = float( words[3])
                    #print( "STA003: %g, %g, %g" % ( float( words[1]), float( words[2]), float( words[3])))
            except Exception as e:
                pass
                #print( repr(e))
                #print( "--- %s" % line)
            #
            # tell the world that we are alive
            #
            if count % 1000 == 0: 
                print( "SP2000TRScript, count %d, line %s" % (count, line))
    sock.close()

    return

if __name__ == "__main__":
    main()