Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Read data from a Licor 820 and/or 840
- Written by Laurent Fournier, October 2016
- Thanks to David H. Hagan's work, May 2016
- To-do :
- - Devices switch
- - For 820: celltemp, cellpres, co2
- - For 840: celltemp, cellpres, co2, h2o, h2odewpoint
- '''
- import os, sys, subprocess
- import time, datetime
- import serial
- import string
- from bs4 import BeautifulSoup as bs
- #-------------------------------------------------------------
- #------------------ Open configurations ----------------------
- #-------------------------------------------------------------
- ############
- # Settings #
- ############
- DEBUG = True
- LOG = True
- FREQ = 1
- PORT = '/dev/ttyUSB0'
- BAUD = 9600
- PARITY = 'N'
- STOPBIT = 1
- BYTE_SZ = 8
- TIMEOUT = 5.0
- LOG_DIR = 'logs/'
- isLooping = 20 # Nr of data extractions
- isStarted = False
- device_nr = [820, 840] # List of devices's models
- #-------------------------------------------------------------
- #----- Better know what you are doing from this point --------
- #-------------------------------------------------------------
- ##################
- # Initialisation #
- ##################
- class Licor:
- def __init__(self, **kwargs):
- self.port = kwargs.pop('port', '/dev/ttyUSB0')
- self.baud = kwargs.pop('baud', BAUD)
- self.timeout = kwargs.pop('timeout', TIMEOUT)
- self.debug = kwargs.pop('debug', DEBUG)
- self._header = [ 'timestamp',
- 'cell_temp',
- 'cell_pressure',
- 'co2',
- 'co2_abs',
- 'ivolt',
- 'raw']
- def connect(self):
- try:
- self.con = serial.Serial(self.port, self.baud, timeout=self.timeout) # Connect to serial device
- except Exception as e:
- self.con = None
- return e
- return True
- def read(self):
- raw = bs(self.con.readline(), 'lxml') # Read a complete row from IO stream
- raw = raw.li820.data # Define data structure
- res = [ datetime.datetime.now().isoformat(' '),
- raw.celltemp.string,
- raw.cellpres.string,
- raw.co2.string,
- raw.co2abs.string,
- raw.ivolt.string,
- raw.raw.string, ]
- if self.debug:
- print ("\nNew Data Point")
- for each in zip(self._header, res):
- print (each[0], each[1])
- return res
- def __repr__(self):
- return "Licor Model Li-820/840"
- ################
- # Main program #
- ################
- filename = 'licor{}-data-{}.csv'.format(device_nr[0], datetime.datetime.now())
- if LOG_DIR: # If LOG_DIR is set, add it to filename
- filename = os.path.join(LOG_DIR, filename)
- try: # Connect to device
- licor = Licor(port = PORT, baud = BAUD, timeout = TIMEOUT, debug = DEBUG)
- licor.connect()
- except Exception as e:
- if DEBUG:
- print ("ERROR: {}".format(e))
- sys.exit("Could not connect to the device")
- if LOG: # If logging enabled
- with open(filename, 'w') as fp:
- fp.write(';'.join(licor._header)) # Write headers
- fp.write('\n')
- while True:
- try:
- data = licor.read() # Read from device
- fp.write(';'.join(data)) # Write data
- fp.write('\n')
- except Exception as e:
- if DEBUG:
- print ("ERROR: {}".format(e))
- time.sleep(FREQ) # Sleep for FREQ seconds
- fp.close()
- else: # If logging Disabled
- while True:
- data = licor.read()
- time.sleep(FREQ)
Advertisement
Add Comment
Please, Sign In to add comment