1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# debugio.py - module for debugging an I/O stream
#
# Copyright (C) 2008, 2009 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
class DebugIO():
"""This class is a file-like object that writes from one file and
writes to another. It is mainly used for debugging the serial protocol
without a serial connection."""
def __init__(self, name):
import os
if not os.path.exists(name+'.in'): os.mkfifo(name+'.in')
if not os.path.exists(name+'.out'): os.mkfifo(name+'.out')
r = open(name+'.in', 'r', 0)
w = open(name+'.out', 'w', 0)
self._r = r
self._w = w
self.write = w.write
self.portstr = 'debuging to %s.in and %s.out' % ( name, name )
self._timeout = None
def close(self):
self._r.close()
self._w.close()
def inWaiting(self):
# we are never out of data and 100 should be enough for everybody
return 100
def setTimeout(self, seconds):
self._timeout = seconds
def getTimeout(self):
return self._timeout
def read(self, size):
import select
read = ''
if size > 0:
while len(read) < size:
ready, _, _ = select.select([self._r.fileno()], [], [], self._timeout)
if not ready:
break #timeout
buf = self._r.read(size-len(read))
read = read + buf
if self._timeout >= 0 and not buf:
break #early abort on timeout
return read
|