comparison 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
comparison
equal deleted inserted replaced
0:a0ce8ebf2f18 1:f7c60e525801
1 import struct
2
3 CP_CMD_PING = 1
4 CP_CMD_PONG = 2
5
6 def csum_add(csum, c):
7 return (((csum << 3) | (csum >> 5)) ^ c) & 0xff
8
9 def cp_ping(seq, data):
10 csum = 0
11 csum = csum_add(0, CP_CMD_PING)
12 for c in data:
13 csum = csum_add(csum, ord(c))
14 pass
15 csum = csum_add(csum, 0)
16
17 cmd_str = struct.pack('BBBBB', ord('J'), ord('C'), seq,
18 len(data) + 1, CP_CMD_PING)
19 cmd_str = cmd_str + data + chr(csum)
20 return cmd_str
21
22 def get_reply(fo):
23 import fcntl, os
24 import time
25
26 reply = ''
27 fcntl.fcntl(fo.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
28 while True:
29 try:
30 s = os.read(fo.fileno(), 256)
31 except OSError:
32 time.sleep(0.5)
33 try:
34 s = os.read(fo.fileno(), 256)
35 except OSError:
36 break
37 pass
38 reply = reply + s
39 pass
40 return reply
41
42 if __name__ == '__main__':
43 import sys
44
45 if len(sys.argv) != 2:
46 print >> sys.stderr, 'Usage: prog <port device>'
47 sys.exit(1)
48 pass
49
50 port = sys.argv[1]
51
52 fo = open(port, 'r+b')
53 cmd_str = cp_ping(0, 'hello')
54 fo.write(cmd_str)
55 fo.flush()
56
57 reply = get_reply(fo)
58 print repr(reply)
59 pass