#!/usr/bin/python2 ''' Grab and save data from CEILO. In output file messages are separated with empty lines and first line is started with message arrival datetime. CHR(1..3) are stripped from the messages. Msg format: \0x01 ... \0x02 # first line data lines (ascii hex && numbers) \0x03 ''' import sys import datetime from time import sleep try: import cSerial except: sys.path.extend(['epylib','../epylib']) import cSerial DTNOW=datetime.datetime.now chr_MSGSTART=chr(1) chr_MSGFIRSTLINEEND=chr(2) chr_MSGEND=chr(3) OMASK="/home/data/ceilo/raw/krova_ceilo_%Y-%m-%d.dat" SERPORT="/dev/ttyMI12,2400,E,7,1" def main(): ''' ''' ser=None while True: try: if ser: try: ser.close_port() del ser ser=None except: pass ser=cSerial.cSerial(SERPORT) ser.open_port() stime=None while True: try: if ser.inWaiting(): if not stime: stime=DTNOW() buf=[] sleep(.5) sin=ser.read_port() buf.append(sin) if sin.find(chr_MSGEND)>=0: sout=''.join(buf) if sout.find(chr_MSGSTART)>=0: sout=sout.replace(chr_MSGSTART,'').replace(chr_MSGEND,'').replace(chr_MSGFIRSTLINEEND,'') s=sout.split('\n') ofs=open(stime.strftime(OMASK),'a') ofs.write('%19.19s,'%stime) for ln in s: so=ln.strip() if so: ofs.write('%s\n'%so) ofs.write('\n') ofs.close() print " %19.19s, %s"%(stime,s[0].strip()) stime=None sleep(.2) except KeyboardInterrupt: raise except: print "inner serial error, opening port again" raise except KeyboardInterrupt: print "Ctrl-C caught, exiting program" if ser: ser.close_port() break except: print "serport error, trying again in a few seconds" sleep(2) raise if __name__ == '__main__': main()