Mercurial > avr_jtag
annotate tests/cp_ping.py @ 1:f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 21 Feb 2009 20:20:06 +0800 |
parents | |
children | e410832c3280 |
rev | line source |
---|---|
1
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
1 import struct |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
2 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 CP_CMD_PING = 1 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
4 CP_CMD_PONG = 2 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
5 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
6 def csum_add(csum, c): |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
7 return (((csum << 3) | (csum >> 5)) ^ c) & 0xff |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
8 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
9 def cp_ping(seq, data): |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
10 csum = 0 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
11 csum = csum_add(0, CP_CMD_PING) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 for c in data: |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 csum = csum_add(csum, ord(c)) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 pass |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
15 csum = csum_add(csum, 0) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
16 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
17 cmd_str = struct.pack('BBBBB', ord('J'), ord('C'), seq, |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 len(data) + 1, CP_CMD_PING) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 cmd_str = cmd_str + data + chr(csum) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
20 return cmd_str |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
22 def get_reply(fo): |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
23 import fcntl, os |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
24 import time |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
25 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 reply = '' |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
27 fcntl.fcntl(fo.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
28 while True: |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
29 try: |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
30 s = os.read(fo.fileno(), 256) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
31 except OSError: |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
32 time.sleep(0.5) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 try: |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 s = os.read(fo.fileno(), 256) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 except OSError: |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 break |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 pass |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 reply = reply + s |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 pass |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 return reply |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 if __name__ == '__main__': |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 import sys |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
45 if len(sys.argv) != 2: |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 print >> sys.stderr, 'Usage: prog <port device>' |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
47 sys.exit(1) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
48 pass |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
50 port = sys.argv[1] |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
51 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
52 fo = open(port, 'r+b') |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 cmd_str = cp_ping(0, 'hello') |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 fo.write(cmd_str) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
55 fo.flush() |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 reply = get_reply(fo) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
58 print repr(reply) |
f7c60e525801
cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
59 pass |