diff --git a/datalogger.py b/datalogger.py index 723566f..3fcbb53 100755 --- a/datalogger.py +++ b/datalogger.py @@ -22,189 +22,189 @@ VAL_TYPE = None #============================================================================== def parse(): - """ - Specific parsing procedure for transfering data from any abstract instrument. - :returns: populated namespace (parser) - """ - - parser = argparse.ArgumentParser(description = 'Acquire data from INSTRUMENT', - epilog = 'Example: \'./datalogger.py -i myInstrument -st 10\' logs myInstrument every 10 seconds to output file YYYYMMDD-HHMMSS-INSTRUMENT.dat') - - parser.add_argument('-l', - action='store_true', - dest='list', - default=False, - help='List all available instruments') - - parser.add_argument('-I', - action='store', - dest='instLog', - default=INSTRUMENT, - help='Instrument to log (default '+str(INSTRUMENT)+')') - - parser.add_argument('-ip', - action='store', - dest='adress', - default=ADRESS, - help='Adress of instrument (IP, USB...) (default '+str(ADRESS)+')') - - parser.add_argument('-v', - action='store', - dest='vtype', - default=VAL_TYPE, - help='Value type to measure (default '+str(VAL_TYPE)+')') - - parser.add_argument('-st', - action='store', - dest='samplingtime', - default=SAMPLING_TIME, - help='Sampling time acquistion (default '+str(SAMPLING_TIME)+' second)') - - parser.add_argument('-fd', - action='store', - dest='fileduration', - default=FILE_DURATION, - help='File duration (infinite : \'-fd -1\') (default '+str(FILE_DURATION)+' second)') - - parser.add_argument('-p', - action='store', - dest='path', - default=PATH, - help='Absolute path (default '+PATH+')') - - args = parser.parse_args() - return args + """ + Specific parsing procedure for transfering data from any abstract instrument. + :returns: populated namespace (parser) + """ + + parser = argparse.ArgumentParser(description = 'Acquire data from INSTRUMENT', + epilog = 'Example: \'./datalogger.py -i myInstrument -st 10\' logs myInstrument every 10 seconds to output file YYYYMMDD-HHMMSS-INSTRUMENT.dat') + + parser.add_argument('-l', + action='store_true', + dest='list', + default=False, + help='List all available instruments') + + parser.add_argument('-I', + action='store', + dest='instLog', + default=INSTRUMENT, + help='Instrument to log (default '+str(INSTRUMENT)+')') + + parser.add_argument('-ip', + action='store', + dest='adress', + default=ADRESS, + help='Adress of instrument (IP, USB...) (default '+str(ADRESS)+')') + + parser.add_argument('-v', + action='store', + dest='vtype', + default=VAL_TYPE, + help='Value type to measure (default '+str(VAL_TYPE)+')') + + parser.add_argument('-st', + action='store', + dest='samplingtime', + default=SAMPLING_TIME, + help='Sampling time acquistion (default '+str(SAMPLING_TIME)+' second)') + + parser.add_argument('-fd', + action='store', + dest='fileduration', + default=FILE_DURATION, + help='File duration (infinite : \'-fd -1\') (default '+str(FILE_DURATION)+' second)') + + parser.add_argument('-p', + action='store', + dest='path', + default=PATH, + help='Absolute path (default '+PATH+')') + + args = parser.parse_args() + return args #============================================================================== def acq_routine(instrument, path, samplingtime, fileduration): - instrument.connect() - t0 = time.time() - filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat' - print('Opening %s' %filename) - try: - year = time.strftime("%Y", time.gmtime(t0)) - month = time.strftime("%Y-%m", time.gmtime(t0)) - os.chdir(path + '/' + year + '/' + month) - except: - try: - os.chdir(path + '/' + year) - os.mkdir(month) - os.chdir(path + '/' + year + '/' + month) - except: - os.chdir(path) - os.mkdir(year) - os.chdir(path + '/' + year) - os.mkdir(month) - os.chdir(path + '/' + year + '/' + month) - - data_file = open(filename, 'wr', 0) - - # Infinite loop - while True: - # tic - tic = time.time() - - if time.time() - t0 >= fileduration: - t0 = time.time() - print('Closing %s\n' %filename) - data_file.close() - - try: - year = time.strftime("%Y", time.gmtime(t0)) - month = time.strftime("%Y-%m", time.gmtime(t0)) - os.chdir(path + '/' + year + '/' + month) - except: - try: - os.chdir(path + '/' + year) - os.mkdir(month) - os.chdir(path + '/' + year + '/' + month) - except: - os.chdir(path) - os.mkdir(year) - os.chdir(path + '/' + year) - os.mkdir(month) - os.chdir(path + '/' + year + '/' + month) - - filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat' - print('Opening %s\n' %filename) - data_file = open(filename, 'wr', 0) - - try: - try: - #epoch time - epoch = time.time() - #MJD time - mjd = epoch / 86400.0 + 40587 - # Meas values - meas = instrument.getValue() - meas = meas.replace(",", "\t") - meas = meas.replace(";", "\t") - meas = meas.replace("+", "") - - string = "%f\t%f\t%s" % (epoch, mjd, meas) - data_file.write(string) # Write in a file - print(string) - - # Sleep until sampletime - time.sleep(samplingtime - (time.time() - tic)) - - except Exception as ex: - print 'Exception during controler data reading: ' + str(ex) - - except KeyboardInterrupt: - print '\n --> Disconnected' - instrument.disconnect() - data_file.close() - - # To stop the loop in a clean way - break + instrument.connect() + t0 = time.time() + filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat' + print('Opening %s' %filename) + try: + year = time.strftime("%Y", time.gmtime(t0)) + month = time.strftime("%Y-%m", time.gmtime(t0)) + os.chdir(path + '/' + year + '/' + month) + except: + try: + os.chdir(path + '/' + year) + os.mkdir(month) + os.chdir(path + '/' + year + '/' + month) + except: + os.chdir(path) + os.mkdir(year) + os.chdir(path + '/' + year) + os.mkdir(month) + os.chdir(path + '/' + year + '/' + month) + + data_file = open(filename, 'wr', 0) + + # Infinite loop + while True: + # tic + tic = time.time() + + if time.time() - t0 >= fileduration: + t0 = time.time() + print('Closing %s\n' %filename) + data_file.close() + + try: + year = time.strftime("%Y", time.gmtime(t0)) + month = time.strftime("%Y-%m", time.gmtime(t0)) + os.chdir(path + '/' + year + '/' + month) + except: + try: + os.chdir(path + '/' + year) + os.mkdir(month) + os.chdir(path + '/' + year + '/' + month) + except: + os.chdir(path) + os.mkdir(year) + os.chdir(path + '/' + year) + os.mkdir(month) + os.chdir(path + '/' + year + '/' + month) + + filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + instrument.model() + '.dat' + print('Opening %s\n' %filename) + data_file = open(filename, 'wr', 0) + + try: + try: + #epoch time + epoch = time.time() + #MJD time + mjd = epoch / 86400.0 + 40587 + # Meas values + meas = instrument.getValue() + meas = meas.replace(",", "\t") + meas = meas.replace(";", "\t") + meas = meas.replace("+", "") + + string = "%f\t%f\t%s" % (epoch, mjd, meas) + data_file.write(string) # Write in a file + print(string) + + # Sleep until sampletime + time.sleep(samplingtime - (time.time() - tic)) + + except Exception as ex: + print 'Exception during controler data reading: ' + str(ex) + + except KeyboardInterrupt: + print '\n --> Disconnected' + instrument.disconnect() + data_file.close() + + # To stop the loop in a clean way + break #============================================================================== def main(): - """ - Main script - """ - # Parse command line - args = parse() - # path - path = args.path - # Sampling time - samplingtime=float(args.samplingtime) - # File duration - fileduration=int(args.fileduration) - # Instrument to log - instLog = args.instLog - # instrument adress - adress = args.adress - # val type - vtype = args.vtype - - try: - if args.list: - print('\nInstruments:') - for name, obj in inspect.getmembers(instruments): - if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False: - print('\n' + name) - exec('print("\t" + instruments.%s.ALL_VAL_TYPE)'%name) - - else: - if instLog == None: - raise Exception('No instrument selected !') - - if adress == None and vtype == None: - exec('myInstrument = instruments.%s.%s()'%(instLog, instLog)) - elif adress == None and vtype != None: - exec('myInstrument = instruments.%s.%s(vtype="%s")'%(instLog, instLog, vtype)) - elif adress != None and vtype != None: - exec('myInstrument = instruments.%s.%s(adress="%s", vtype="%s")'%(instLog, instLog, adress, vtype)) - acq_routine(myInstrument, path, samplingtime, fileduration) - - except Exception as ex: - print 'Oops: '+str(ex) + """ + Main script + """ + # Parse command line + args = parse() + # path + path = args.path + # Sampling time + samplingtime=float(args.samplingtime) + # File duration + fileduration=int(args.fileduration) + # Instrument to log + instLog = args.instLog + # instrument adress + adress = args.adress + # val type + vtype = args.vtype + + try: + if args.list: + print('\nInstruments:') + for name, obj in inspect.getmembers(instruments): + if inspect.ismodule(obj) and name.startswith('__') == False and name.startswith('abstract') == False: + print('\n' + name) + exec('print("\t" + instruments.%s.ALL_VAL_TYPE)'%name) + + else: + if instLog == None: + raise Exception('No instrument selected !') + + if adress == None and vtype == None: + exec('myInstrument = instruments.%s.%s()'%(instLog, instLog)) + elif adress == None and vtype != None: + exec('myInstrument = instruments.%s.%s(vtype="%s")'%(instLog, instLog, vtype)) + elif adress != None and vtype != None: + exec('myInstrument = instruments.%s.%s(adress="%s", vtype="%s")'%(instLog, instLog, adress, vtype)) + acq_routine(myInstrument, path, samplingtime, fileduration) + + except Exception as ex: + print 'Oops: '+str(ex) #============================================================================== if __name__ == "__main__": - main() + main() diff --git a/instruments/AG34461A.py b/instruments/AG34461A.py index 6cef3cf..d94eb22 100644 --- a/instruments/AG34461A.py +++ b/instruments/AG34461A.py @@ -8,78 +8,78 @@ ALL_VAL_TYPE = "DCV, ACV, DCI, ACI, RES2W, RES4W, FREQ" #============================================================================== class AG34461A(abstract_instrument): - def __init__(self, adress="192.168.0.61", vtype="DCV"): - self.adress = adress - self.port = 5025 - self.vtype = vtype + def __init__(self, adress="192.168.0.61", vtype="DCV"): + self.adress = adress + self.port = 5025 + self.vtype = vtype - def model(self): - self.send("*IDN?") - return self.read() + def model(self): + self.send("*IDN?") + return self.read() - def connect(self): - try: - print('Connecting to device @%s:%s...' %(self.adress, self.port)) - self.sock = socket.socket(socket.AF_INET, - socket.SOCK_STREAM, - socket.IPPROTO_TCP) - self.sock.settimeout(10.0) # Don't hang around forever - self.sock.connect((self.adress, self.port)) - print(' --> Ok') + def connect(self): + try: + print('Connecting to device @%s:%s...' %(self.adress, self.port)) + self.sock = socket.socket(socket.AF_INET, + socket.SOCK_STREAM, + socket.IPPROTO_TCP) + self.sock.settimeout(10.0) # Don't hang around forever + self.sock.connect((self.adress, self.port)) + print(' --> Ok') - print(self.model()) + print(self.model()) - if self.vtype == "DCV": - self.send("CONF:VOLT:DC") - elif self.vtype == "ACV": - self.send("CONF:VOLT:AC") - elif self.vtype == "DCI": - self.send("CONF:CURR:DC") - elif self.vtype == "ACI": - self.send("CONF:CURR:AC") - elif self.vtype == "RES2W": - self.send("CONF:RES") - elif self.vtype == "RES4W": - self.send("CONF:FRES") - elif self.vtype == "FREQ": - self.send("CONF:FREQ") - else: - print("Wrong -v argument") - raise + if self.vtype == "DCV": + self.send("CONF:VOLT:DC") + elif self.vtype == "ACV": + self.send("CONF:VOLT:AC") + elif self.vtype == "DCI": + self.send("CONF:CURR:DC") + elif self.vtype == "ACI": + self.send("CONF:CURR:AC") + elif self.vtype == "RES2W": + self.send("CONF:RES") + elif self.vtype == "RES4W": + self.send("CONF:FRES") + elif self.vtype == "FREQ": + self.send("CONF:FREQ") + else: + print("Wrong -v argument") + raise - except socket.timeout: - print("Socket timeout error during connection.") - raise - except socket.error as er: - print("Socket error during connection: " + str(er)) - raise - except Exception as er: - print("Unexpected error during connection: " + str(er)) - raise + except socket.timeout: + print("Socket timeout error during connection.") + raise + except socket.error as er: + print("Socket error during connection: " + str(er)) + raise + except Exception as er: + print("Unexpected error during connection: " + str(er)) + raise - def getValue(self): - self.send("READ?") - return self.read() + def getValue(self): + self.send("READ?") + return self.read() - def read(self): - ans = '' - nb_data_list = [] - nb_data = '' - try: - while ans != '\n': - ans = self.sock.recv(1) - nb_data_list.append(ans) # Return the number of data - list_size = len(nb_data_list) - for j in range (0, list_size): - nb_data = nb_data+nb_data_list[j] - return nb_data - except socket.timeout: - print "Socket timeout error when reading." - raise + def read(self): + ans = '' + nb_data_list = [] + nb_data = '' + try: + while ans != '\n': + ans = self.sock.recv(1) + nb_data_list.append(ans) # Return the number of data + list_size = len(nb_data_list) + for j in range (0, list_size): + nb_data = nb_data+nb_data_list[j] + return nb_data + except socket.timeout: + print "Socket timeout error when reading." + raise - def disconnect(self): - self.send('*RST') - self.sock.close() + def disconnect(self): + self.send('*RST') + self.sock.close() - def send(self, command): - self.sock.send("%s\n"%command) + def send(self, command): + self.sock.send("%s\n"%command) diff --git a/instruments/AG34972A.py b/instruments/AG34972A.py index a30206c..7cf093c 100644 --- a/instruments/AG34972A.py +++ b/instruments/AG34972A.py @@ -8,78 +8,78 @@ ALL_VAL_TYPE = "DCV, ACV, DCI, ACI, RES2W, RES4W, FREQ" #============================================================================== class AG34972A(abstract_instrument): - def __init__(self, adress="192.168.0.72", vtype="DCV"): - self.adress = adress - self.port = 5025 - self.vtype = vtype + def __init__(self, adress="192.168.0.72", vtype="DCV"): + self.adress = adress + self.port = 5025 + self.vtype = vtype - def model(self): - self.send("*IDN?") - return self.read() + def model(self): + self.send("*IDN?") + return self.read() - def connect(self): - try: - print('Connecting to device @%s:%s...' %(self.adress, self.port)) - self.sock = socket.socket(socket.AF_INET, - socket.SOCK_STREAM, - socket.IPPROTO_TCP) - self.sock.settimeout(2.0) # Don't hang around forever - self.sock.connect((self.adress, self.port)) - print(' --> Ok') + def connect(self): + try: + print('Connecting to device @%s:%s...' %(self.adress, self.port)) + self.sock = socket.socket(socket.AF_INET, + socket.SOCK_STREAM, + socket.IPPROTO_TCP) + self.sock.settimeout(2.0) # Don't hang around forever + self.sock.connect((self.adress, self.port)) + print(' --> Ok') - print(self.model()) + print(self.model()) - if self.vtype == "DCV": - self.send("CONF:VOLT:DC (@102:103)") - elif self.vtype == "ACV": - self.send("CONF:VOLT:AC (@102:103)") - elif self.vtype == "DCI": - self.send("CONF:CURR:DC (@102:103)") - elif self.vtype == "ACI": - self.send("CONF:CURR:AC (@102:103)") - elif self.vtype == "RES2W": - self.send("CONF:RES (@102:103)") - elif self.vtype == "RES4W": - self.send("CONF:FRES (@102:103)") - elif self.vtype == "FREQ": - self.send("CONF:FREQ (@102:103)") - else: - print("Wrong -v argument") - raise + if self.vtype == "DCV": + self.send("CONF:VOLT:DC (@102:103)") + elif self.vtype == "ACV": + self.send("CONF:VOLT:AC (@102:103)") + elif self.vtype == "DCI": + self.send("CONF:CURR:DC (@102:103)") + elif self.vtype == "ACI": + self.send("CONF:CURR:AC (@102:103)") + elif self.vtype == "RES2W": + self.send("CONF:RES (@102:103)") + elif self.vtype == "RES4W": + self.send("CONF:FRES (@102:103)") + elif self.vtype == "FREQ": + self.send("CONF:FREQ (@102:103)") + else: + print("Wrong -v argument") + raise - except socket.timeout: - print("Socket timeout error during connection.") - raise - except socket.error as er: - print("Socket error during connection: " + str(er)) - raise - except Exception as er: - print("Unexpected error during connection: " + str(er)) - raise + except socket.timeout: + print("Socket timeout error during connection.") + raise + except socket.error as er: + print("Socket error during connection: " + str(er)) + raise + except Exception as er: + print("Unexpected error during connection: " + str(er)) + raise - def getValue(self): - self.send("MEAS? AUTO,DEF,(@102:103)") - return self.read() + def getValue(self): + self.send("MEAS? AUTO,DEF,(@102:103)") + return self.read() - def read(self): - ans = '' - nb_data_list = [] - nb_data = '' - try: - while ans != '\n': - ans = self.sock.recv(1) - nb_data_list.append(ans) # Return the number of data - list_size = len(nb_data_list) - for j in range (0, list_size): - nb_data = nb_data+nb_data_list[j] - return nb_data - except socket.timeout: - print "Socket timeout error when reading." - raise + def read(self): + ans = '' + nb_data_list = [] + nb_data = '' + try: + while ans != '\n': + ans = self.sock.recv(1) + nb_data_list.append(ans) # Return the number of data + list_size = len(nb_data_list) + for j in range (0, list_size): + nb_data = nb_data+nb_data_list[j] + return nb_data + except socket.timeout: + print "Socket timeout error when reading." + raise - def disconnect(self): - self.send('*RST') - self.sock.close() + def disconnect(self): + self.send('*RST') + self.sock.close() - def send(self, command): - self.sock.send("%s\n"%command) + def send(self, command): + self.sock.send("%s\n"%command) diff --git a/instruments/LS350.py b/instruments/LS350.py index 5a4dc71..f30bd58 100644 --- a/instruments/LS350.py +++ b/instruments/LS350.py @@ -8,73 +8,73 @@ ALL_VAL_TYPE = "TEMP, RES" #============================================================================== class LS350(abstract_instrument): - def __init__(self, adress="192.168.0.12", vtype="TEMP"): - self.adress = adress - self.port = 7777 - self.vtype = vtype + def __init__(self, adress="192.168.0.12", vtype="TEMP"): + self.adress = adress + self.port = 7777 + self.vtype = vtype - def model(self): - self.send("*IDN?") - return self.read() + def model(self): + self.send("*IDN?") + return self.read() - def connect(self): - try: - print('Connecting to device @%s:%s...' %(self.adress, self.port)) - self.sock = socket.socket(socket.AF_INET, - socket.SOCK_STREAM, - socket.IPPROTO_TCP) - self.sock.settimeout(10.0) # Don't hang around forever - self.sock.connect((self.adress, self.port)) - print(' --> Ok') + def connect(self): + try: + print('Connecting to device @%s:%s...' %(self.adress, self.port)) + self.sock = socket.socket(socket.AF_INET, + socket.SOCK_STREAM, + socket.IPPROTO_TCP) + self.sock.settimeout(10.0) # Don't hang around forever + self.sock.connect((self.adress, self.port)) + print(' --> Ok') - print(self.model()) + print(self.model()) - if self.vtype == "TEMP": - 1 - elif self.vtype == "RES": - 1 - else: - print("Wrong -v argument") - raise + if self.vtype == "TEMP": + 1 + elif self.vtype == "RES": + 1 + else: + print("Wrong -v argument") + raise - except socket.timeout: - print("Socket timeout error during connection.") - raise - except socket.error as er: - print("Socket error during connection: " + str(er)) - raise - except Exception as er: - print("Unexpected error during connection: " + str(er)) - raise + except socket.timeout: + print("Socket timeout error during connection.") + raise + except socket.error as er: + print("Socket error during connection: " + str(er)) + raise + except Exception as er: + print("Unexpected error during connection: " + str(er)) + raise - def getValue(self): - if self.vtype == 'TEMP': - self.send('krdg? a;krdg? b;krdg? c;krdg? d') - return self.read() - elif self.vtype == 'RES': - self.send('srdg? a;srdg? b;srdg? c;srdg? d') - return self.read() + def getValue(self): + if self.vtype == 'TEMP': + self.send('krdg? a;krdg? b;krdg? c;krdg? d') + return self.read() + elif self.vtype == 'RES': + self.send('srdg? a;srdg? b;srdg? c;srdg? d') + return self.read() - def read(self): - self.send("++read eoi") - ans = '' - nb_data_list = [] - nb_data = '' - try: - while ans != '\n': - ans = self.sock.recv(1) - nb_data_list.append(ans) # Return the number of data - list_size = len(nb_data_list) - for j in range (0, list_size): - nb_data = nb_data+nb_data_list[j] - return nb_data - except socket.timeout: - print "Socket timeout error when reading." - raise + def read(self): + self.send("++read eoi") + ans = '' + nb_data_list = [] + nb_data = '' + try: + while ans != '\n': + ans = self.sock.recv(1) + nb_data_list.append(ans) # Return the number of data + list_size = len(nb_data_list) + for j in range (0, list_size): + nb_data = nb_data+nb_data_list[j] + return nb_data + except socket.timeout: + print "Socket timeout error when reading." + raise - def disconnect(self): - self.send('MODE0') - self.sock.close() + def disconnect(self): + self.send('MODE0') + self.sock.close() - def send(self, command): - self.sock.send("%s\n"%command) + def send(self, command): + self.sock.send("%s\n"%command) diff --git a/instruments/PM100D.py b/instruments/PM100D.py index 9d9c69a..42438fe 100644 --- a/instruments/PM100D.py +++ b/instruments/PM100D.py @@ -8,41 +8,41 @@ ALL_VAL_TYPE = "PWR" #============================================================================== class PM100D(abstract_instrument): - def __init__(self, adress="/dev/usbtmc0", vtype="PWR"): - self.adress = adress - self.vtype = vtype + def __init__(self, adress="/dev/usbtmc0", vtype="PWR"): + self.adress = adress + self.vtype = vtype - def model(self): - self.send("*IDN?") - return self.read() + def model(self): + self.send("*IDN?") + return self.read() - def connect(self): - try: - print('Connecting to device @%s...' %(self.adress)) - self.FILE = os.open(self.adress, os.O_RDWR) - print(' --> Ok') + def connect(self): + try: + print('Connecting to device @%s...' %(self.adress)) + self.FILE = os.open(self.adress, os.O_RDWR) + print(' --> Ok') - print(self.model()) + print(self.model()) - if self.vtype == "PWR": - 1 - else: - print("Wrong -v argument") - raise + if self.vtype == "PWR": + 1 + else: + print("Wrong -v argument") + raise - except Exception as er: - print("Unexpected error during connection: " + str(er)) - raise + except Exception as er: + print("Unexpected error during connection: " + str(er)) + raise - def getValue(self): - self.send("READ?") - return self.read() + def getValue(self): + self.send("READ?") + return self.read() - def read(self): - return os.read(self.FILE, 300) + def read(self): + return os.read(self.FILE, 300) - def disconnect(self): - self.send('*RST') + def disconnect(self): + self.send('*RST') - def send(self, command): - os.write(self.FILE, command) + def send(self, command): + os.write(self.FILE, command) diff --git a/instruments/T7Pro.py b/instruments/T7Pro.py index 6dad733..e187a90 100644 --- a/instruments/T7Pro.py +++ b/instruments/T7Pro.py @@ -9,147 +9,147 @@ ALL_VAL_TYPE = "TEMP, RES" #============================================================================== class T7Pro(abstract_instrument): - def __init__(self, adress="192.168.0.26", vtype="TEMP"): - self.adress = adress - self.vtype = vtype - self.names = ["TEMPERATURE_AIR_K", - "CURRENT_SOURCE_200UA_CAL_VALUE", - "AIN0",# "AIN1", - "AIN2",# "AIN3", - "AIN4",# "AIN5", - "AIN6",# "AIN7" - ] - - def model(self): - return 'T7Pro' - - def connect(self): - try: - print('Connecting to device @%s...' %(self.adress)) - self.handle = ljm.openS("T7", "ETHERNET", self.adress) - self.configureAINs() - print(' --> Ok') - - print(self.model()) - - if self.vtype == "TEMP": - 1 - elif self.vtype == "RES": - 1 - else: - print("Wrong -v argument") - raise - - except Exception as er: - print("Unexpected error during connection: " + str(er)) - raise - - def getValue(self): - results = self.read(self.names) - temp = results[0] - res1 = results[2]/results[1] - res2 = results[3]/results[1] - res3 = results[4]/results[1] - res4 = results[5]/results[1] - - if self.vtype == 'TEMP': - # Temperature calculation - temp1 = self.res2tempSensor(628, res1) - temp2 = self.res2tempSensor(16947, res2) - temp3 = self.res2tempSensor(625, res3) - temp4 = self.res2tempSensor(100, res4) - string = '%f\t%f\t%f\t%f\t%f\n'%(temp, temp1, temp2, temp3, temp4) - elif self.vtype == 'RES': - string = '%f\t%f\t%f\t%f\t%f\n'%(temp, res1, res2, res3, res4) - else: - string = '' - - return string - - def read(self, names): - return ljm.eReadNames(self.handle, len(names), names) - - def disconnect(self): - ljm.close(self.handle) - - def send(self, command): - pass - - def configureAINs(self): - # Setup and call eWriteNames to configure AINs on the LabJack. - names = ["AIN0_NEGATIVE_CH", "AIN0_RANGE", "AIN0_RESOLUTION_INDEX", - #"AIN1_NEGATIVE_CH", "AIN1_RANGE", "AIN1_RESOLUTION_INDEX", - "AIN2_NEGATIVE_CH", "AIN2_RANGE", "AIN2_RESOLUTION_INDEX", - #"AIN3_NEGATIVE_CH", "AIN3_RANGE", "AIN3_RESOLUTION_INDEX", - "AIN4_NEGATIVE_CH", "AIN4_RANGE", "AIN4_RESOLUTION_INDEX", - #"AIN5_NEGATIVE_CH", "AIN5_RANGE", "AIN5_RESOLUTION_INDEX", - "AIN6_NEGATIVE_CH", "AIN6_RANGE", "AIN6_RESOLUTION_INDEX", - #"AIN7_NEGATIVE_CH", "AIN7_RANGE", "AIN7_RESOLUTION_INDEX" - ] - l_names = len(names) - aValues = [1, 1, 12, - #199, 0.01, 0, - 3, 1, 12, - #199, 0.01, 0, - 5, 1, 12, - #199, 0.01, 0, - 7, 0.1, 12, - #199, 0.01, 0 - ] - ljm.eWriteNames(self.handle, l_names, names, aValues) -# print("\nSet configuration:") -# for i in range(len(names)): -# print(" %s : %f" % (names[i], aValues[i])) - - def res2tempSensor(self, sensor, res): - if sensor==627: - K = [0.399341181655472610, - 10.8420092277810909, - -26.4597939187660813, - 245.9828566655493379, - -668.069876596331596, - 1001.69882618263364, - -267.272089680656791] - if sensor==625: - K = [0.333548856582638109, - 11.7361551595386118, - -31.32988932320903987, - 262.878643524833024, - -704.163538021035492, - 1056.6040485650301, - -307.057196729816496] - if sensor==628: - K = [0.463200932294057566, - 13.5049710820894688, - -30.5191222755238414, - 231.098593852017075, - -550.122691885568202, - 806.038547554984689, - -198.510489917360246] - if sensor==16945: - K = [3.2497, 5.1777, 2.499] - if sensor==16943: - K = [3.4738, 5.1198, 2.3681] - if sensor==16944: - K = [3.3674, 5.2874, 2.5165] - if sensor==16941: - K = [2.9486, 4.5862, 2.266] - if sensor==16947: - K = [3.4597, 5.2422, 2.4169] - if sensor==100: - K = [0.003850] - return self.res2temp(K, res) - - def res2temp(K, res): - temp = 0 - tmp = 1000./res - if len(K)==7: - for i in range(len(K)): - temp += K[i]*tmp**i - if len(K)==3: - for i in range(len(K)): - temp += K[i]*numpy.log10(tmp)**(2-i) - temp = 10**temp - if len(K)==1: - temp = (res/100.-1)/K[0]+273.15 - return temp + def __init__(self, adress="192.168.0.26", vtype="TEMP"): + self.adress = adress + self.vtype = vtype + self.names = ["TEMPERATURE_AIR_K", + "CURRENT_SOURCE_200UA_CAL_VALUE", + "AIN0",# "AIN1", + "AIN2",# "AIN3", + "AIN4",# "AIN5", + "AIN6",# "AIN7" + ] + + def model(self): + return 'T7Pro' + + def connect(self): + try: + print('Connecting to device @%s...' %(self.adress)) + self.handle = ljm.openS("T7", "ETHERNET", self.adress) + self.configureAINs() + print(' --> Ok') + + print(self.model()) + + if self.vtype == "TEMP": + 1 + elif self.vtype == "RES": + 1 + else: + print("Wrong -v argument") + raise + + except Exception as er: + print("Unexpected error during connection: " + str(er)) + raise + + def getValue(self): + results = self.read(self.names) + temp = results[0] + res1 = results[2]/results[1] + res2 = results[3]/results[1] + res3 = results[4]/results[1] + res4 = results[5]/results[1] + + if self.vtype == 'TEMP': + # Temperature calculation + temp1 = self.res2tempSensor(628, res1) + temp2 = self.res2tempSensor(16947, res2) + temp3 = self.res2tempSensor(625, res3) + temp4 = self.res2tempSensor(100, res4) + string = '%f\t%f\t%f\t%f\t%f\n'%(temp, temp1, temp2, temp3, temp4) + elif self.vtype == 'RES': + string = '%f\t%f\t%f\t%f\t%f\n'%(temp, res1, res2, res3, res4) + else: + string = '' + + return string + + def read(self, names): + return ljm.eReadNames(self.handle, len(names), names) + + def disconnect(self): + ljm.close(self.handle) + + def send(self, command): + pass + + def configureAINs(self): + # Setup and call eWriteNames to configure AINs on the LabJack. + names = ["AIN0_NEGATIVE_CH", "AIN0_RANGE", "AIN0_RESOLUTION_INDEX", + #"AIN1_NEGATIVE_CH", "AIN1_RANGE", "AIN1_RESOLUTION_INDEX", + "AIN2_NEGATIVE_CH", "AIN2_RANGE", "AIN2_RESOLUTION_INDEX", + #"AIN3_NEGATIVE_CH", "AIN3_RANGE", "AIN3_RESOLUTION_INDEX", + "AIN4_NEGATIVE_CH", "AIN4_RANGE", "AIN4_RESOLUTION_INDEX", + #"AIN5_NEGATIVE_CH", "AIN5_RANGE", "AIN5_RESOLUTION_INDEX", + "AIN6_NEGATIVE_CH", "AIN6_RANGE", "AIN6_RESOLUTION_INDEX", + #"AIN7_NEGATIVE_CH", "AIN7_RANGE", "AIN7_RESOLUTION_INDEX" + ] + l_names = len(names) + aValues = [1, 1, 12, + #199, 0.01, 0, + 3, 1, 12, + #199, 0.01, 0, + 5, 1, 12, + #199, 0.01, 0, + 7, 0.1, 12, + #199, 0.01, 0 + ] + ljm.eWriteNames(self.handle, l_names, names, aValues) +# print("\nSet configuration:") +# for i in range(len(names)): +# print(" %s : %f" % (names[i], aValues[i])) + + def res2tempSensor(self, sensor, res): + if sensor==627: + K = [0.399341181655472610, + 10.8420092277810909, + -26.4597939187660813, + 245.9828566655493379, + -668.069876596331596, + 1001.69882618263364, + -267.272089680656791] + if sensor==625: + K = [0.333548856582638109, + 11.7361551595386118, + -31.32988932320903987, + 262.878643524833024, + -704.163538021035492, + 1056.6040485650301, + -307.057196729816496] + if sensor==628: + K = [0.463200932294057566, + 13.5049710820894688, + -30.5191222755238414, + 231.098593852017075, + -550.122691885568202, + 806.038547554984689, + -198.510489917360246] + if sensor==16945: + K = [3.2497, 5.1777, 2.499] + if sensor==16943: + K = [3.4738, 5.1198, 2.3681] + if sensor==16944: + K = [3.3674, 5.2874, 2.5165] + if sensor==16941: + K = [2.9486, 4.5862, 2.266] + if sensor==16947: + K = [3.4597, 5.2422, 2.4169] + if sensor==100: + K = [0.003850] + return self.res2temp(K, res) + + def res2temp(K, res): + temp = 0 + tmp = 1000./res + if len(K)==7: + for i in range(len(K)): + temp += K[i]*tmp**i + if len(K)==3: + for i in range(len(K)): + temp += K[i]*numpy.log10(tmp)**(2-i) + temp = 10**temp + if len(K)==1: + temp = (res/100.-1)/K[0]+273.15 + return temp diff --git a/instruments/__init__.py b/instruments/__init__.py index fbbc134..1f78f0b 100644 --- a/instruments/__init__.py +++ b/instruments/__init__.py @@ -2,8 +2,8 @@ from os import listdir from os.path import dirname for module in listdir(dirname(__file__)): - if module == '__init__.py' or module == 'abstract_instrument.py' or module[-3:] != '.py': - continue - __import__(module[:-3], locals(), globals()) + if module == '__init__.py' or module == 'abstract_instrument.py' or module[-3:] != '.py': + continue + __import__(module[:-3], locals(), globals()) del module, listdir, dirname \ No newline at end of file diff --git a/instruments/abstract_instrument.py b/instruments/abstract_instrument.py index 23dfc51..166c55b 100644 --- a/instruments/abstract_instrument.py +++ b/instruments/abstract_instrument.py @@ -1,39 +1,39 @@ import abc class abstract_instrument(object): - __metaclass__ = abc.ABCMeta - - @abc.abstractmethod - def __init__(self, adress, vtype): - """Build the class""" - return - - @abc.abstractmethod - def model(self): - """return the instrument model""" - return - - @abc.abstractmethod - def connect(self): - """Create a connection with the instrument""" - return - - @abc.abstractmethod - def disconnect(self): - """Disconnect the instrument""" - return - - @abc.abstractmethod - def read(self): - """read the buffer""" - return - - @abc.abstractmethod - def send(self): - """send a command""" - return - - @abc.abstractmethod - def getValue(self): - """return the value of measurment""" - return + __metaclass__ = abc.ABCMeta + + @abc.abstractmethod + def __init__(self, adress, vtype): + """Build the class""" + return + + @abc.abstractmethod + def model(self): + """return the instrument model""" + return + + @abc.abstractmethod + def connect(self): + """Create a connection with the instrument""" + return + + @abc.abstractmethod + def disconnect(self): + """Disconnect the instrument""" + return + + @abc.abstractmethod + def read(self): + """read the buffer""" + return + + @abc.abstractmethod + def send(self): + """send a command""" + return + + @abc.abstractmethod + def getValue(self): + """return the value of measurment""" + return diff --git a/instruments/testDevice.py b/instruments/testDevice.py index 28499ef..79b9bf0 100644 --- a/instruments/testDevice.py +++ b/instruments/testDevice.py @@ -8,48 +8,48 @@ ALL_VAL_TYPE = "DCV, ACV, DCI, ACI, RES2W, RES4W, FREQ" #============================================================================== class testDevice(abstract_instrument): - def __init__(self, adress="123.456.789.123", vtype="DCV"): - self.adress = adress - self.port = 9999 - self.vtype = vtype - - def model(self): - return 'test device' - - def connect(self): - print('Connecting to device @%s:%s...' %(self.adress, self.port)) - time.sleep(1) - print(' --> Ok') - - print(self.model()) - - if self.vtype == "DCV": - print("CONF:VOLT:DC") - elif self.vtype == "ACV": - print("CONF:VOLT:AC") - elif self.vtype == "DCI": - print("CONF:CURR:DC") - elif self.vtype == "ACI": - print("CONF:CURR:AC") - elif self.vtype == "RES2W": - print("CONF:RES") - elif self.vtype == "RES4W": - print("CONF:FRES") - elif self.vtype == "FREQ": - print("CONF:FREQ") - else: - print("Wrong -v argument") - raise - - def getValue(self): - return str(numpy.random.rand()) - - def read(self): - print('reading') - return 1 - - def disconnect(self): - print('reset') - - def send(self, command): - print('send %s'%command) + def __init__(self, adress="123.456.789.123", vtype="DCV"): + self.adress = adress + self.port = 9999 + self.vtype = vtype + + def model(self): + return 'test device' + + def connect(self): + print('Connecting to device @%s:%s...' %(self.adress, self.port)) + time.sleep(1) + print(' --> Ok') + + print(self.model()) + + if self.vtype == "DCV": + print("CONF:VOLT:DC") + elif self.vtype == "ACV": + print("CONF:VOLT:AC") + elif self.vtype == "DCI": + print("CONF:CURR:DC") + elif self.vtype == "ACI": + print("CONF:CURR:AC") + elif self.vtype == "RES2W": + print("CONF:RES") + elif self.vtype == "RES4W": + print("CONF:FRES") + elif self.vtype == "FREQ": + print("CONF:FREQ") + else: + print("Wrong -v argument") + raise + + def getValue(self): + return str(numpy.random.rand()) + + def read(self): + print('reading') + return 1 + + def disconnect(self): + print('reset') + + def send(self, command): + print('send %s'%command)