listing serial ports, cleaned up exception return in threaded reader and proper port closing when the loop breaks
This commit is contained in:
parent
5e6a2d3f09
commit
3b3bee8c87
41
temp_log.py
41
temp_log.py
|
@ -1,5 +1,7 @@
|
||||||
#! /usr/bin/env python
|
#! /usr/bin/env python
|
||||||
|
import glob
|
||||||
import io
|
import io
|
||||||
|
import platform
|
||||||
import Queue
|
import Queue
|
||||||
import serial
|
import serial
|
||||||
import sys
|
import sys
|
||||||
|
@ -8,6 +10,28 @@ import time
|
||||||
defaultSerialPort = '/dev/ttyACM0'
|
defaultSerialPort = '/dev/ttyACM0'
|
||||||
defaultVersion = 0
|
defaultVersion = 0
|
||||||
|
|
||||||
|
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*')
|
||||||
|
|
||||||
|
|
||||||
class ParseException(Exception):
|
class ParseException(Exception):
|
||||||
"""Parser Exceptions"""
|
"""Parser Exceptions"""
|
||||||
pass
|
pass
|
||||||
|
@ -29,10 +53,13 @@ def parse_line(line, version = defaultVersion):
|
||||||
|
|
||||||
|
|
||||||
def threaded_reader(source, dataQueue, commandQueue):
|
def threaded_reader(source, dataQueue, commandQueue):
|
||||||
import kivy.logger
|
try:
|
||||||
ser = serial.Serial(source, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
|
ser = serial.Serial(source, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
|
||||||
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
|
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
|
||||||
ser.isOpen()
|
ser.isOpen()
|
||||||
|
except Exception, e:
|
||||||
|
dataQueue.put({'exception' : str(e)})
|
||||||
|
return
|
||||||
buffer = ''
|
buffer = ''
|
||||||
runTime = ''
|
runTime = ''
|
||||||
cmd = ''
|
cmd = ''
|
||||||
|
@ -47,12 +74,12 @@ def threaded_reader(source, dataQueue, commandQueue):
|
||||||
data = parse_line(ser.readline(), defaultVersion)
|
data = parse_line(ser.readline(), defaultVersion)
|
||||||
runTime = time.time()
|
runTime = time.time()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
kivy.logger.Logger.exception(e)
|
dataQueue.put({'exception' : str(e)})
|
||||||
continue
|
|
||||||
if data and runTime:
|
if data and runTime:
|
||||||
item = {'time' : runTime, 'data' : data}
|
item = {'time' : runTime, 'data' : data}
|
||||||
kivy.logger.Logger.debug(str(item))
|
|
||||||
dataQueue.put(item)
|
dataQueue.put(item)
|
||||||
|
# close out the fds
|
||||||
|
ser.close()
|
||||||
|
|
||||||
|
|
||||||
def main(args = sys.argv):
|
def main(args = sys.argv):
|
||||||
|
|
Loading…
Reference in New Issue