2014-12-08 21:35:55 +00:00
#! /usr/bin/env python
2014-12-13 17:58:34 +00:00
import glob
2014-12-10 22:22:52 +00:00
import io
2014-12-13 17:58:34 +00:00
import platform
2014-12-10 22:22:52 +00:00
import Queue
import serial
import sys
import threading
import time
2014-12-09 03:11:28 +00:00
defaultSerialPort = ' /dev/ttyACM0 '
defaultVersion = 0
2014-12-13 17:58:34 +00:00
def list_serial_ports ( ) :
""" Lists serial ports, taken from http://stackoverflow.com/questions/11303850/what-is-the-cross-platform-method-of-enumerating-serial-ports-in-python-includi """
system_name = platform . system ( )
if system_name == " Windows " :
# Scan for available ports.
available = [ ]
for i in range ( 256 ) :
try :
s = serial . Serial ( i )
available . append ( i )
s . close ( )
except serial . SerialException :
pass
return available
elif system_name == " Darwin " :
# Mac
return glob . glob ( ' /dev/tty* ' ) + glob . glob ( ' /dev/cu* ' )
else :
# Assume Linux or something else
return glob . glob ( ' /dev/ttyS* ' ) + glob . glob ( ' /dev/ttyUSB* ' ) + glob . glob ( ' /dev/ttyACM* ' )
2014-12-09 03:11:28 +00:00
class ParseException ( Exception ) :
""" Parser Exceptions """
2014-12-08 21:35:55 +00:00
pass
2014-12-09 03:11:28 +00:00
def parse_line ( line , version = defaultVersion ) :
""" Parses a line and returns an array index by name of the data available """
# @TODO This could be a class based versioning?
2015-08-12 22:28:08 +00:00
data = dict ( )
2014-12-09 03:11:28 +00:00
if ( version == 0 ) :
line = line . split ( ' ' )
2014-12-10 22:22:52 +00:00
try :
data [ ' temperature ' ] = float ( line [ - 2 ] )
except Exception , e :
pass
2014-12-09 03:11:28 +00:00
else :
raise ParseException ( ' Unimplemented line version ' , version )
return data
2014-12-10 22:22:52 +00:00
def threaded_reader ( source , dataQueue , commandQueue ) :
2014-12-13 17:58:34 +00:00
try :
ser = serial . Serial ( source , baudrate = 9600 , parity = serial . PARITY_NONE , stopbits = serial . STOPBITS_ONE , bytesize = serial . EIGHTBITS )
sio = io . TextIOWrapper ( io . BufferedRWPair ( ser , ser ) )
ser . isOpen ( )
except Exception , e :
dataQueue . put ( { ' exception ' : str ( e ) } )
return
2014-12-10 22:22:52 +00:00
buffer = ' '
runTime = ' '
cmd = ' '
while True :
try :
cmd = commandQueue . get_nowait ( )
except Queue . Empty :
pass
if cmd == ' stop ' :
break
try :
data = parse_line ( ser . readline ( ) , defaultVersion )
runTime = time . time ( )
except Exception , e :
2014-12-13 17:58:34 +00:00
dataQueue . put ( { ' exception ' : str ( e ) } )
2014-12-10 22:22:52 +00:00
if data and runTime :
item = { ' time ' : runTime , ' data ' : data }
dataQueue . put ( item )
2014-12-13 17:58:34 +00:00
# close out the fds
ser . close ( )
2014-12-10 22:22:52 +00:00
2014-12-09 03:11:28 +00:00
def main ( args = sys . argv ) :
# @TODO switch to parse args or similar
# @TODO parse of temperature logger line version
serialPort = defaultSerialPort
lineVersion = defaultVersion
2014-12-08 21:35:55 +00:00
try :
2014-12-09 03:11:28 +00:00
serialPort = sys . argv [ 1 ]
2014-12-08 21:35:55 +00:00
except Exception , e :
2014-12-09 03:11:28 +00:00
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 ( )
buffer = ' '
while True :
try :
buffer = ser . readline ( )
data = parse_line ( buffer , lineVersion )
2014-12-10 22:22:52 +00:00
if data :
print unicode ( time . time ( ) ) + ' , ' + unicode ( data [ ' temperature ' ] )
sys . stdout . flush ( )
2014-12-09 03:11:28 +00:00
except Exception , e :
sys . stderr . write ( str ( e ) + ' \n ' )
continue
if __name__ == ' __main__ ' :
main ( sys . argv )