annotate tests/cp_ping.py @ 3:e410832c3280

Issue of avr-ld. - For some unknown reason, avr-ld can not handle multiple objects well. - Fixed by compile all source at once.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 22 Feb 2009 01:40:36 +0800
parents f7c60e525801
children
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
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 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
6 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
7
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 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
9 csum = 0
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 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
11 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
12 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
13 pass
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 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
15
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 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
17 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
18 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
19 return cmd_str
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 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
22 import fcntl, os
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 import time
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 reply = ''
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 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
27 while True:
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 try:
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 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
30 except OSError:
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 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
32 try:
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 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
34 except OSError:
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 break
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 pass
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 reply = reply + s
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 pass
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 return reply
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 if __name__ == '__main__':
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 import sys
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 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
45 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
46 sys.exit(1)
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 pass
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 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
50
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 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
52 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
53 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
54 fo.flush()
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 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
57 print repr(reply)
f7c60e525801 cptest and cp_ping.py to test cmd_proto.c.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 pass