Ejemplo n.º 1
0
def burn_bin_file(serialport, filename, address):
    if not os.path.exists(filename):
        print("file \"%s\" is not existed." % filename)
        return False

    #get address
    if address == "0" or address == "0x0" or address == "0x00":
        # get flash address
        match = send_and_match(serialport, b'1',
                               b'Backup part addr:([0-9a-fxA-F]*)', 5)
        if not match:
            print("Can not get flash address")
            return False
        address = match.group(1)
    else:
        serialport.write(b'1')
        address = address.encode()

    # set flash address
    match = send_and_match(serialport, address + b'\r\n', b'CCCC', 30)
    if not match:
        print("Can not enter into ymodem mode")
        return False

    # send binary file
    def sender_getc(size):
        return serialport.read(size) or None

    def sender_putc(data, timeout=15):
        return serialport.write(data)

    sender = YModem(sender_getc, sender_putc)
    sent = sender.send_file(filename)
    return True
Ejemplo n.º 2
0
def burn_bin_file(serialport, filename, address):
    if not os.path.exists(filename):
        print("file \"%s\" is not existed." % filename)
        return False
    for i in range(3):
        # ymodem update
        serialport.write(b'1')
        time.sleep(0.1)

        # get flash address
        bmatched, buff = send_cmd_check_recv_data(serialport, b'',
                                                  b'Please input flash addr:',
                                                  2)
        try:
            buff_str = buff.decode('UTF-8', errors='ignore')
            print(buff_str)
        except IOError:
            pass
        if bmatched:
            break
    if address == "0":
        if bmatched:
            pattern = re.compile(b'Backup part addr:([0-9a-fxA-F]*)')
            match = pattern.search(buff)
            if match:
                address = match.group(1)
            else:
                print("can not get flash address")
                return False
        else:
            print("can not get flash address")
            return False
    else:
        address = address.encode()

    # set flash address
    serialport.write(address)
    serialport.write(b'\r\n')
    time.sleep(0.1)
    bmatched, buff = send_cmd_check_recv_data(serialport, b'', b'CCCCC', 5)
    try:
        buff_str = buff.decode('UTF-8', errors='ignore')
        print(buff_str)
    except IOError:
        pass
    if not bmatched:
        print("can not enter into ymodem mode")
        return False

    # send binary file
    def sender_getc(size):
        return serialport.read(size) or None

    def sender_putc(data, timeout=15):
        return serialport.write(data)

    sender = YModem(sender_getc, sender_putc)
    sent = sender.send_file(filename)
    return True