Mercurial > code > home > repos > homeauto
comparison service/arduinoNode/loggingserial.py @ 969:70a5392b24d3
start arduinonode
Ignore-this: 6ddc4d3af9ab8468e25b346bddf15835
darcs-hash:20150406091339-312f9-bcbca91eff6dc85a402d341248e7ce6128e71723
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 06 Apr 2015 02:13:39 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
968:d68cb03ce575 | 969:70a5392b24d3 |
---|---|
1 # forked from /my/proj/house/frontdoor/loggingserial.py | |
2 | |
3 import serial, logging | |
4 | |
5 log = logging.getLogger('serial') | |
6 | |
7 class LoggingSerial(object): | |
8 """like serial.Serial, but logs all data""" | |
9 | |
10 def __init__(self, port=None, ports=None, baudrate=9600, timeout=10): | |
11 if ports is None: | |
12 ports = [port] | |
13 | |
14 for port in ports: | |
15 try: | |
16 log.info("trying port: %s" % port) | |
17 self.ser = serial.Serial(port=port, baudrate=baudrate, | |
18 timeout=timeout, | |
19 xonxoff=0, rtscts=0) | |
20 except serial.SerialException: | |
21 pass | |
22 if not hasattr(self, 'ser'): | |
23 raise IOError("no port found") | |
24 | |
25 def flush(self): | |
26 self.ser.flush() | |
27 | |
28 def close(self): | |
29 self.ser.close() | |
30 | |
31 def write(self, s): | |
32 log.info("Serial write: %r" % s) | |
33 self.ser.write(s) | |
34 | |
35 def read(self, n, errorOnTimeout=True): | |
36 buf = self.ser.read(n) | |
37 log.info("Serial read: %r" % buf) | |
38 if errorOnTimeout and n > 0 and len(buf) == 0: | |
39 raise ValueError("timed out") | |
40 return buf |