Revision 219
some fixed to manualtron, NEED TO PORT TO TOOLTRON
manualtron.py | ||
---|---|---|
31 | 31 |
keypadTimeout = 11 #in seconds |
32 | 32 |
|
33 | 33 |
TT_GET_KEY = 'k' |
34 |
TT_SEND_KEY = 's' ############ put this in tooltron.py |
|
34 | 35 |
TT_ACK = 'a' |
35 | 36 |
TT_NACK = 'n' |
36 | 37 |
TT_TO = 'f' |
... | ... | |
102 | 103 |
def sendNack(toolNum): |
103 | 104 |
print "seding ACK to",toolNum |
104 | 105 |
sendMessage(toolNum, TT_NACK, "") |
106 |
|
|
107 |
# This function eats the rest of the packet where data is what we have so far |
|
108 |
def flushPacket(data): |
|
109 |
while len(data) < 6: |
|
110 |
data += bus.read(1) |
|
105 | 111 |
|
106 |
#returns key if the key_send packet is recived from tool ID 2 |
|
107 |
# otherwise returns 0 |
|
108 |
def readKey(): |
|
109 |
startDelim = bus.read(1) |
|
110 |
if startDelim == '^': |
|
112 |
plen = data[4] |
|
113 |
if plen > 0: |
|
114 |
bus.read(ord(plen)) |
|
115 |
|
|
116 |
|
|
117 |
# Returns [src, dest, cmd, data] |
|
118 |
def readMessage(): |
|
119 |
if bus.read(1) == '^': |
|
111 | 120 |
src = bus.read(1) |
112 |
if src == 0: |
|
113 |
return 0 |
|
121 |
if src == chr(1): #reflection |
|
122 |
print "(reflection)" |
|
123 |
flushPacket('^' + src) |
|
124 |
return readMessage() |
|
125 |
|
|
114 | 126 |
dest = bus.read(1) |
115 |
if dest == 0: |
|
116 |
return 0 |
|
117 |
packetType = bus.read(1) |
|
118 |
if packetType == 0: |
|
119 |
return 0 |
|
120 |
payloadLen = bus.read(1) |
|
121 |
if payloadLen != 1: |
|
122 |
print "ERROR: payload length wrong! Should be 1, was",payloadLen |
|
123 |
return 0 |
|
124 |
key = bus.read(1) |
|
125 |
if key == 0: |
|
126 |
return 0 |
|
127 |
crc = bus.read(1) |
|
127 |
cmd = bus.read(1) |
|
128 |
plen = bus.read(1) |
|
129 |
data = bus.read(ord(plen)) |
|
130 |
x = bus.read(1) |
|
128 | 131 |
|
129 | 132 |
print "got packet" |
130 |
if src != 2 or dest != 1 or packetType != TT_KEY_SEND: |
|
131 |
print "Bad packet! ^",src,dest,packetType,key,crc |
|
133 |
printMsg('^' + src + dest + cmd + plen + data + x) |
|
132 | 134 |
|
133 |
if chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)) == crc: |
|
134 |
return key |
|
135 |
crc = ord(src) ^ ord(dest) ^ ord(cmd) ^ ord(plen) |
|
136 |
for d in data: |
|
137 |
crc ^= ord(d) |
|
138 |
|
|
139 |
if crc == ord(x): |
|
140 |
return [src, dest, cmd, data] |
|
135 | 141 |
else: |
136 |
print "xor fail. got", crc, "should have been got",(chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key))) |
|
142 |
print "xor fail. got", str(ord(x)), "should have been got",crc |
|
143 |
return 0 #TODO: error |
|
137 | 144 |
|
145 |
def readKey(): |
|
146 |
m = readMessage() |
|
147 |
if m == 0: |
|
138 | 148 |
return 0 |
139 |
|
|
149 |
[src, dest, cmd, data] = m |
|
150 |
if cmd == TT_SEND_KEY: |
|
151 |
return data[0] |
|
140 | 152 |
else: |
141 |
print "did not get start delim!: ", ord(startDelim) |
|
142 | 153 |
return 0 |
143 |
|
|
154 |
|
|
144 | 155 |
#returns [src, dest, command] or [] on error |
156 |
# because of reflection, this will quietly ignore packets send by the server |
|
145 | 157 |
def readTool(): |
146 |
ret = [0,0,0] |
|
158 |
m = readMessage() |
|
159 |
if m == 0: |
|
160 |
return [] |
|
161 |
[src, dest, cmd, data] = m |
|
162 |
return [src, dest, cmd] |
|
147 | 163 |
|
148 |
if bus.read(1) == '^': |
|
149 |
ret[0] = bus.read(1) |
|
150 |
ret[1] = bus.read(1) |
|
151 |
ret[2] = bus.read(1) |
|
152 |
plen = bus.read(1) |
|
153 |
if plen != chr(0): |
|
154 |
print "ERROR: tool responded with payload len",str(ord(plen)) |
|
155 |
bus.flushInput() |
|
156 |
x = bus.read(1) |
|
157 |
|
|
158 |
print "got packet",ret |
|
159 |
|
|
160 |
if chr(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) == x: |
|
161 |
return ret |
|
162 |
else: |
|
163 |
print "xor fail. got", x, "should have been got",(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) |
|
164 |
|
|
165 |
return [] |
|
166 |
|
|
167 | 164 |
def checkAck(t): |
168 | 165 |
tn = t |
169 | 166 |
m = readTool() |
... | ... | |
173 | 170 |
|
174 | 171 |
|
175 | 172 |
return m[0] == chr(tn) and m[1] == chr(1) and m[2] == TT_ACK |
173 |
|
|
174 |
def key(): |
|
175 |
sendMessage(2,TT_GET_KEY,"") |
|
176 |
print readKey() |
|
177 |
|
Also available in: Unified diff