def pack_srec(type_, address, size, data):
    """Create a Motorola S-Record record of given data.

    """

    if type_ in '0159':
        line = '{:02X}{:04X}'.format(size + 2 + 1, address)
    elif type_ in '268':
        line = '{:02X}{:06X}'.format(size + 3 + 1, address)
    elif type_ in '37':
        line = '{:02X}{:08X}'.format(size + 4 + 1, address)
    else:
        raise Error(
            "expected record type 0..3 or 5..9, but got '{}'".format(type_))

    if data:
        line += binascii.hexlify(data).decode('ascii').upper()

    return 'S{}{}{:02X}'.format(type_, line, crc_srec(line))
Create a Motorola S-Record record of given data.