OkTekk

RS232 Reader / XML to CSV Converter

Oct 12th, 2016
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. '''
  2.    Read data from a Licor 820 and/or 840
  3.    
  4.    Written by Laurent Fournier, October 2016
  5.    Thanks to David H. Hagan's work, May 2016
  6.  
  7.    To-do :
  8.    - Devices switch
  9.    - For 820: celltemp, cellpres, co2
  10.    - For 840: celltemp, cellpres, co2, h2o, h2odewpoint
  11. '''
  12.  
  13. import os, sys, subprocess
  14. import time, datetime
  15. import serial
  16. import string
  17.  
  18. from bs4 import BeautifulSoup as bs
  19.  
  20. #-------------------------------------------------------------
  21. #------------------ Open configurations ----------------------
  22. #-------------------------------------------------------------
  23.  
  24.   ############
  25.   # Settings #
  26.   ############
  27.  
  28. DEBUG     = True
  29. LOG       = True
  30. FREQ      = 1
  31. PORT      = '/dev/ttyUSB0'
  32. BAUD      = 9600
  33. PARITY    = 'N'
  34. STOPBIT   = 1
  35. BYTE_SZ   = 8
  36. TIMEOUT   = 5.0
  37. LOG_DIR   = 'logs/'
  38.  
  39. isLooping = 20                                                                          # Nr of data extractions
  40. isStarted = False
  41.  
  42. device_nr = [820, 840]                                                                  # List of devices's models
  43.  
  44.  
  45. #-------------------------------------------------------------
  46. #----- Better know what you are doing from this point --------
  47. #-------------------------------------------------------------
  48.  
  49.   ##################
  50.   # Initialisation #
  51.   ##################
  52.  
  53. class Licor:
  54.     def __init__(self, **kwargs):
  55.         self.port       = kwargs.pop('port',    '/dev/ttyUSB0')
  56.         self.baud       = kwargs.pop('baud',    BAUD)
  57.         self.timeout    = kwargs.pop('timeout', TIMEOUT)
  58.         self.debug      = kwargs.pop('debug',   DEBUG)
  59.  
  60.         self._header    = [ 'timestamp',
  61.                             'cell_temp',
  62.                             'cell_pressure',
  63.                             'co2',
  64.                             'co2_abs',
  65.                             'ivolt',
  66.                             'raw']
  67.  
  68.     def connect(self):
  69.         try:
  70.             self.con = serial.Serial(self.port, self.baud, timeout=self.timeout)        # Connect to serial device
  71.            
  72.         except Exception as e:
  73.             self.con = None
  74.             return e
  75.        
  76.         return True
  77.  
  78.     def read(self):
  79.         raw = bs(self.con.readline(), 'lxml')                                           # Read a complete row from IO stream
  80.  
  81.         raw = raw.li820.data                                                            # Define data structure
  82.         res = [ datetime.datetime.now().isoformat(' '),
  83.                 raw.celltemp.string,
  84.                 raw.cellpres.string,
  85.                 raw.co2.string,
  86.                 raw.co2abs.string,
  87.                 raw.ivolt.string,
  88.                 raw.raw.string, ]
  89.  
  90.         if self.debug:
  91.             print ("\nNew Data Point")
  92.             for each in zip(self._header, res):
  93.                 print (each[0], each[1])
  94.                
  95.         return res
  96.  
  97.     def __repr__(self):
  98.         return "Licor Model Li-820/840"
  99.  
  100.  
  101.   ################
  102.   # Main program #
  103.   ################
  104.  
  105. filename = 'licor{}-data-{}.csv'.format(device_nr[0], datetime.datetime.now())
  106.  
  107. if LOG_DIR:                                                                             # If LOG_DIR is set, add it to filename
  108.     filename = os.path.join(LOG_DIR, filename)
  109.  
  110. try:                                                                                    # Connect to device
  111.     licor = Licor(port = PORT, baud = BAUD, timeout = TIMEOUT, debug = DEBUG)
  112.     licor.connect()
  113.    
  114. except Exception as e:
  115.     if DEBUG:
  116.         print ("ERROR: {}".format(e))
  117.        
  118.     sys.exit("Could not connect to the device")
  119.  
  120. if LOG:                                                                                 # If logging enabled
  121.     with open(filename, 'w') as fp:
  122.         fp.write(';'.join(licor._header))                                               # Write headers
  123.         fp.write('\n')
  124.  
  125.         while True:
  126.             try:                
  127.                 data = licor.read()                                                     # Read from device
  128.  
  129.                 fp.write(';'.join(data))                                                # Write data
  130.                 fp.write('\n')
  131.                
  132.             except Exception as e:
  133.                 if DEBUG:
  134.                     print ("ERROR: {}".format(e))
  135.                    
  136.             time.sleep(FREQ)                                                            # Sleep for FREQ seconds
  137.         fp.close()
  138.        
  139. else:                                                                                   # If logging Disabled
  140.     while True:
  141.         data = licor.read()
  142.         time.sleep(FREQ)
Advertisement
Add Comment
Please, Sign In to add comment