Mercurial > code > home > repos > homeauto
view service/arduinoNode/loggingserial.py @ 1019:93cc6238f5ce
support a dir of config files
Ignore-this: 729d0aea065af5a0fac6b07653b1d3d2
darcs-hash:bc13d26d7496710125a1dd512ab2c25a0bd47a01
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 03 Jan 2016 02:27:17 -0800 |
parents | 70a5392b24d3 |
children |
line wrap: on
line source
# forked from /my/proj/house/frontdoor/loggingserial.py import serial, logging log = logging.getLogger('serial') class LoggingSerial(object): """like serial.Serial, but logs all data""" def __init__(self, port=None, ports=None, baudrate=9600, timeout=10): if ports is None: ports = [port] for port in ports: try: log.info("trying port: %s" % port) self.ser = serial.Serial(port=port, baudrate=baudrate, timeout=timeout, xonxoff=0, rtscts=0) except serial.SerialException: pass if not hasattr(self, 'ser'): raise IOError("no port found") def flush(self): self.ser.flush() def close(self): self.ser.close() def write(self, s): log.info("Serial write: %r" % s) self.ser.write(s) def read(self, n, errorOnTimeout=True): buf = self.ser.read(n) log.info("Serial read: %r" % buf) if errorOnTimeout and n > 0 and len(buf) == 0: raise ValueError("timed out") return buf