-
Notifications
You must be signed in to change notification settings - Fork 2
/
logit.py
107 lines (89 loc) · 2.54 KB
/
logit.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Simple read/write wrapper
robotika.cz
"""
import sys
import serial
import datetime
import ctypes
import math
class LogEnd(Exception):
"End of log notification"
pass
class LogIt():
"Log communication via serial com"
def __init__( self, com, prefix='logs/hy' ):
self._com = com
self._logFile = None
self.relog( prefix )
def relog( self, prefix ):
dt = datetime.datetime.now()
filename = prefix + dt.strftime("%y%m%d_%H%M%S.log")
self._logFile = open( filename, "wb" )
print "LogIt:", filename
return filename
def read( self, numChars=1 ):
s = self._com.read( numChars )
for ch in s:
self._logFile.write( chr(0x01) )
self._logFile.write( ch )
self._logFile.flush()
return s
def write( self, chars ):
for ch in chars:
self._logFile.write( chr(0x00) )
self._logFile.write( ch )
self._logFile.flush()
self._com.write( chars )
def close( self ):
pass # dummy
#-------------------------------------------------------------------
class ReplayLog():
"Read & verify log"
def __init__( self, filename, assertWrite=True ):
print "ReplayLog", filename
self._logFile = open( filename, "rb" )
self.assertWrite = assertWrite
def read( self, numChars ):
s = []
for i in range(numChars):
marker = self._logFile.read(1)
if not marker:
raise LogEnd()
assert( marker == chr(0x01) )
s.append(self._logFile.read(1))
if not s[-1]:
raise LogEnd()
return ''.join(s)
def write( self, chars ):
for ch in chars:
marker = self._logFile.read(1)
if not marker:
raise LogEnd()
assert( marker == chr(0x00) )
verifyCh = self._logFile.read(1)
if not verifyCh:
raise LogEnd()
if self.assertWrite:
assert( verifyCh == ch )
#-------------------------------------------------------------------
class ReplyLogInputsOnly():
"Read & verify log"
def __init__( self, filename ):
print "ReplyLogInputOnly", filename
self._logFile = open( filename, "rb" )
def read( self, numChars ):
s = []
for i in range(numChars):
while( self._logFile.read(1) not in [chr(0x01), ''] ):
c = self._logFile.read(1) # skip write output
if not c:
raise LogEnd()
assert( i == 0 ) # the packets should be complete
s.append(self._logFile.read(1))
if not s[-1]:
raise LogEnd()
return ''.join(s)
def write( self, chars ):
pass
#-------------------------------------------------------------------