refactored temp_log.py for versioned line formats, more extendable data return
This commit is contained in:
parent
1985c99fbd
commit
ea1ee9c386
58
temp_log.py
58
temp_log.py
|
@ -1,21 +1,47 @@
|
||||||
#! /usr/bin/env python
|
#! /usr/bin/env python
|
||||||
import serial, sys, time, io
|
import serial, sys, time, io
|
||||||
serialPort = '/dev/ttyACM0'
|
defaultSerialPort = '/dev/ttyACM0'
|
||||||
try:
|
defaultVersion = 0
|
||||||
serialPort = sys.argv[1]
|
|
||||||
except Exception, e:
|
class ParseException(Exception):
|
||||||
|
"""Parser Exceptions"""
|
||||||
pass
|
pass
|
||||||
ser = serial.Serial(port=serialPort, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
|
|
||||||
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
|
|
||||||
ser.isOpen()
|
def parse_line(line, version = defaultVersion):
|
||||||
buffer = ''
|
"""Parses a line and returns an array index by name of the data available"""
|
||||||
while True:
|
# @TODO This could be a class based versioning?
|
||||||
|
data = {}
|
||||||
|
if (version == 0):
|
||||||
|
line = line.split(' ')
|
||||||
|
data['temperature'] = float(line[-2])
|
||||||
|
else:
|
||||||
|
raise ParseException('Unimplemented line version', version)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def main(args = sys.argv):
|
||||||
|
# @TODO switch to parse args or similar
|
||||||
|
# @TODO parse of temperature logger line version
|
||||||
|
serialPort = defaultSerialPort
|
||||||
|
lineVersion = defaultVersion
|
||||||
try:
|
try:
|
||||||
buffer = ser.readline()
|
serialPort = sys.argv[1]
|
||||||
buffer_split = buffer.split(' ')
|
|
||||||
temp = float(buffer_split[-2])
|
|
||||||
print unicode(time.time()) + ',' + unicode(temp)
|
|
||||||
sys.stdout.flush()
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
sys.stderr.write(str(e) + '\n')
|
pass
|
||||||
continue
|
ser = serial.Serial(port=serialPort, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
|
||||||
|
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
|
||||||
|
ser.isOpen()
|
||||||
|
buffer = ''
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
buffer = ser.readline()
|
||||||
|
data = parse_line(buffer, lineVersion)
|
||||||
|
print unicode(time.time()) + ',' + unicode(data['temperature'])
|
||||||
|
sys.stdout.flush()
|
||||||
|
except Exception, e:
|
||||||
|
sys.stderr.write(str(e) + '\n')
|
||||||
|
continue
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(sys.argv)
|
||||||
|
|
Loading…
Reference in New Issue