#!/usr/bin/python # $Id: tsl.py,v 1.5 2006/01/02 11:47:50 ambolt Exp $ # Temperature data logger. # It is designed to be run by init(8), for instance like this: # S1:345:respawn:/usr/local/bin/tsl.py /dev/ttyS1 /var/spool # Copyright (C) 2005 Asgeir S. Nilsen # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. import serial import os import time import sys port = sys.argv[1] dir = sys.argv[2] if not os.path.isdir(dir): raise Exception("Argument is not directory: "+dir) def COUNT(): return 5 while True: ser = serial.Serial(port,2400,timeout=10) ser.readline() try: list = {} sum = {} for i in ('1','2','3','4'): list[i] = [0.0] * COUNT() sum[i] = 0.0 while True: data = ser.readline().split() idx = data[0] temp = float(data[1]) old = list[idx].pop(0) sum[idx] -= old sum[idx] += temp list[idx].append(temp) f = file(dir+'/tsl.tmp','w') f.write(str(sum[idx]/COUNT())+'\n') f.close() os.rename(dir+'/tsl.tmp', dir+'/tsl.'+idx) finally: ser.close() time.sleep(1)