#! /usr/bin/env python

# Optode serial string capture program
# 10 aug 2016
#   assumes Optode will auto- broadcast once/5secs when powered up
#   tips/usage of functions   >>> help(serial.Serial); help(time)
#
#   s.poulos
#
# typical output - 229 length char string
# MEASUREMENT  4330F 726 O2Concentration(uM) 236.341  AirSaturation(%) 91.508  Temperature(Deg.C) 24.874 CalPhase(Deg) 27.711  TCPhase(Deg)  28.163  C1RPh(Deg)	32.847  C2RPh(Deg) 4.684  C1Amp(mV)  964.6  C2Amp(mV)  937.9  RawTemp(mV)  -7.7	
#
# ('\x13\x11MEASUREMENT\t4330F\t726\tO2Concentration(uM)\t239.854\tAirSaturation(%)\t91.689\tTemperature(Deg.C)\t24.187\tCalPhase(Deg)\t27.813\tTCPhase(Deg)\t28.261\tC1RPh(Deg)\t32.957\tC2RPh(Deg)\t4.696\tC1Amp(mV)\t958.8\tC2Amp(mV)\t940.4\tRawTemp(mV)\t14.6\t\r\n',)
# could have 'tab' separators 
# 

import serial 
import os
import sys
import time

#ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=10)  # setup ref class to serial port
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=5)  # setup ref class to  serial port
ser.close()        # if left open due to CNTL C, first close it.
ser.open()         # open the serial port defined
#ser.reset_input_buffer()  # idea is to flush partials or initial buffer of input
#ser.is_open       # Is the serial port open =True?
#print(ser.name)    # check which port was really used
line = ser.readline()             # read a '\n' terminated line
print line[2:]     # length of string, comma suppresses the added newline

while 1:
# ...     x = ser.read()          # read one byte
# ...     s = ser.read(10)        # read up to ten bytes (timeout)
  line = ser.readline()             # read a '\n' terminated line
  today = time.localtime()
  ss = len(line)
#  if ss >= 5:
#	  print ss,line[2:],     # length of string, comma suppresses the added newline
#	  print line[2:],     # length of string, comma suppresses the added newline

#          col   24-\ 
  where = line.find('O2Concentration')
#              24 + 20 to get to O2 concentration uM
  O2 = where + 20
#  print O2,line[O2:O2+7]
#       col    
  where = line.find('AirSaturation')
  AirS = where + 17
#  print AirS,line[AirS:AirS+6]

  where = line.find('Temperature')
  TdegC = where + 19
#  print TdegC,line[TdegC:TdegC+6]

  where = line.find('CalPhase')
  CalP = where + 14
#  print CalP,line[CalP:CalP+6]

  where = line.find('TCPhase')
  TCP = where + 13
#  print TCP,line[TCP:TCP+6]

  where = line.find('C1RPh')
  C1R = where + 11
#  print C1R,line[C1R:C1R+6]

  where = line.find('C2RPh')
  C2R = where + 11
#  print C2R,line[C2R:C2R+5]

  where = line.find('C1Amp')
  C1A = where + 10
#  print C1A,line[C1A:C1A+5]

  where = line.find('C2Amp')
  C2A = where + 10
#  print C2A,line[C2A:C2A+5]

  where = line.find('RawTemp')
  RawT = where + 12
#  print RawT,line[RawT:RawT+4]
  
#  print today.tm_year, today.tm_yday, today.tm_mon, today.tm_mday, today.tm_hour, today.tm_min, today.tm_sec, new_rec

#  either %s or %d etc works, but with %d can use the '0' to maintain column
  new_rec = "%4d %03d %02d %02d %02d Optode %s %s %s %s %s %s %s %s %s %s" % (today.tm_year, today.tm_yday, today.tm_hour, today.tm_min, today.tm_sec, line[O2:O2+7], line[AirS:AirS+6], line[TdegC:TdegC+6], line[CalP:CalP+6], line[TCP:TCP+6], line[C1R:C1R+6], line[C2R:C2R+5], line[C1A:C1A+5], line[C2A:C2A+5], line[RawT:RawT+4])

#  print line[O2:O2+7], line[AirS:AirS+6], line[TdegC:TdegC+6], line[CalP:CalP+6], line[TCP:TCP+6], line[C1R:C1R+6], line[C2R:C2R+5], line[C1A:C1A+5], line[C2A:C2A+5], line[RawT:RawT+4]

  print new_rec

#  s = ser.read(100)              # read up to one hundred bytes
# ...                               # or as much is in the buffer
ser.close()
 
