view py_avrjtag/cp_ping.py @ 13:1ea479d26fce tip

Make sure shifting phase and add bypass.py. - shifting phase is started after entering SHIFT state Transition from CAP to SHIFT does not induce shifting. - shifting phase is stoped after leaving SHIFT state. Transition from SHIFT to EXIT1 also induce a bit of shifting.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 25 Feb 2009 20:08:29 +0800
parents cc106f278d7d
children
line wrap: on
line source

import cmd_proto

def cp_ping(seq, data):
    cmd = cmd_proto.cmd(seq, cmd_proto.CPCMD_PING, data)
    frame = cmd.to_frame()
    return frame

def get_reply(fo):
    import fcntl, os
    import time
    
    reply = ''
    fcntl.fcntl(fo.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
    while True:
        try:
            s = os.read(fo.fileno(), 256)
        except OSError:
            time.sleep(0.5)
            try:
                s = os.read(fo.fileno(), 256)
            except OSError:
                break
            pass
        reply = reply + s
        pass
    return reply

if __name__ == '__main__':
    import sys

    if len(sys.argv) != 2:
        print >> sys.stderr, 'Usage: prog <port device>'
        sys.exit(1)
        pass
    
    port = sys.argv[1]
    
    fo = open(port, 'r+b')
    cmd_str = cp_ping(20, 'hello')
    fo.write(cmd_str)
    fo.flush()
    
    frame = get_reply(fo)
    cmd = cmd_proto.cmd()
    cmd.from_frame(frame)
    print repr(cmd)