python hex、s19文件读取脚本

个人微信: wzdhh991
邮箱: wzd991@126.com
源码路径:https://gitee.com/wzd991/embedded-system-tools
淘宝链接:https://item.taobao.com/item.htm?id=961194992593&spm=a213gs.v2success.0.0.7dd14831PQcboJ
个人比较懒,优快云更新缓慢,有相关问题可发邮箱或个人微信交流!

# coding=utf-8
import os
import sys
import time
import argparse
import binascii
from ctypes import *

class hex_head(BigEndianStructure):
    _pack_ = 1
    _fields_ = [
        ('len', c_uint8),
        ('address', c_uint16),
        ('type', c_uint8),
    ]

def printf_except_info(e):
    msg = '{}:{} {} '.format(e.__traceback__.tb_frame.f_globals["__file__"], e.__traceback__.tb_lineno, e)
    # QMessageBox.information('错误',msg)
    print(msg)
    return msg
(
    TYPE_DATA_RECORD, #0
    TYPE_END_RECORD, #1
    TYPE_EXT_SEG_ADDR_RECORD, #2
    TYPE_START_SEG_ADDR_RECORD,#3
    TYPE_EXT_LINEAR_ADDR_RECORD,#4
    TYPE_START_LINEAR_ADDR_RECORD
) = range(0,6)

(
    ERR_NONE,
    ERR_FORMAT,
    ERR_LEN,
    ERR_EXCEPT,
) = range(0,4)

def get_sum(data):
    sum = 0
    for i in range(0, len(data)):
        sum += data[i]
    return (0x100 - sum) & 0xff

def get_sum_s19(data):
    sum = 0
    for i in range(0, len(data)):
        sum += data[i]
    return (0xff - sum) &0xff

def get_hex_info(src_name:str):
    try:
        hd = open(src_name,'r')

        if hd == None:
            return (ERR_NONE,'not file.')

        data_lines = hd.readlines()
        hd.close()
        temp = {}
        idx = 0
        offset_addr = None
        base_addr = None

        for line in data_lines:
            idx += 1
            if line[0] != ':' or line[-1] != '\n':
                return (ERR_FORMAT,'format err.{}.'.format(line))
            bin_data = bytearray(binascii.a2b_hex(line[1:-1])) 

            if len(bin_data) < len(bytearray(hex_head())) + 1:
                return (ERR_FORMAT, 'format {}'.format(line))

            frame = hex_head.from_buffer(bin_data)
            # print('len:%d address:0x%x type:%x'%(frame.len, frame.address, frame.type))
            if len(bin_data) != frame.len + len(bytearray(hex_head())) + 1:
                return (ERR_FORMAT, 'format {}'.format(line))
            sum = get_sum(bin_data[:-1])

            if bin_data[-1] != sum:
                return (ERR_FORMAT, 'format {} sum:{} {}'.format(line, sum, bin_data[-1]))

            if frame.type > TYPE_START_LINEAR_ADDR_RECORD:
                return (ERR_FORMAT, 'format err:type:{}'.format(frame.type))

            if frame.type == TYPE_EXT_LINEAR_ADDR_RECORD:
                base_addr = int('%02X%02X'%(bin_data[-3],bin_data[-2]), 16)<<16
                offset_addr = None
            elif frame.type == TYPE_DATA_RECORD and base_addr != None:
                temp_address = base_addr + frame.address
                find_key = None
                for key in temp.keys():
                    if key + len(temp[key]) == temp_address:
                        find_key = key
                        break
                if find_key == None:
                    find_key = temp_address
                    temp[find_key] = bytearray(0)
                temp[find_key].extend(bin_data[len(bytearray(hex_head())):len(bytearray(hex_head()))+ frame.len])  

        return (ERR_NONE, temp)

    except Exception as e:
        return (ERR_EXCEPT, '{}:{} {} '.format(e.__traceback__.tb_frame.f_globals["__file__"], e.__traceback__.tb_lineno, e))

def bin_to_hex(src_name:str,address:int, out_name:str):
    if os.path.exists(src_name) is False:
        return False


def get_s19_data(src_name):
    try:
        hd = open(src_name,'r')

        if hd == None:
            return (ERR_NONE,'not file.')

        data_lines = hd.readlines()
        hd.close()
        temp = {}
        idx = 0
        offset_addr = None
        base_addr = None

        for line in data_lines:
            idx += 1
            cmd = line[:2]
            size = int(line[2:4], 16)
            sum_data =  get_sum_s19(binascii.a2b_hex(line[2:-3]))
            # print('cmd:%s size:%d data:%s sum:0x%x 0x%x'%(cmd, size, line[4:-1], int(line[-3:-1],16), sum_data))
            addr_size = 0
            size_map = {'S1':2,'S2':3,'S3':4}
            if sum_data != int(line[-3:-1],16):
                return (ERR_FORMAT, 'sum err:{}'.format(line))

            if cmd in size_map.keys():
                addr_size = size_map[cmd]
                address = 0
                if addr_size == 2:
                    address = int(line[4:8],16)
                elif addr_size == 3:
                    address = int(line[4:10],16)
                elif addr_size == 4:
                    address = int(line[4:12],16)
                # print('addr_size:%d address:0x%x'%(addr_size, address))
                find_key = None
                for key in temp.keys():
                    if key + len(temp[key]) == address:
                        find_key = key
                        break
                if find_key == None:
                    find_key = address
                    temp[find_key] = bytearray(0)
                temp[find_key].extend(binascii.a2b_hex(line[4+ addr_size*2:-3]))  

        return (ERR_NONE, temp)

    except Exception as e:
        return (ERR_EXCEPT, '{}:{} {} '.format(e.__traceback__.tb_frame.f_globals["__file__"], e.__traceback__.tb_lineno, e))

if __name__ == '__main__':
    ret = get_hex_info('./test.hex')
    if ret[0] == ERR_NONE:
        for key, val in ret[1].items():
            print('address:0x%x len:%d data:%s'%(key, len(val), binascii.hexlify(val)))
    ret = get_s19_data('./tes.s19')
    if ret[0] == ERR_NONE:
        for key, val in ret[1].items():
            print('addr:0x%x size:%d data:%s'%(key,len(val),binascii.hexlify(val)))

    pass
将S19格式的文件转换为Hex文件,通常是指将包含汇编代码的Motorola S记录(S19或S-record)格式转换为二进制的Hex文件格式。以下是一个简单的Python脚本,用于实现这一转换过程: ```python def s19_to_hex(s19_file_path, hex_file_path): # 打开S19格式文件进行读取 with open(s19_file_path, 'r') as s19_file: # 打开一个新文件用于写入Hex格式数据 with open(hex_file_path, 'wb') as hex_file: # 逐行读取S19文件内容 for line in s19_file: # 去除行尾的换行符并进行检查 stripped_line = line.rstrip('\n') if stripped_line[0] != 'S': continue # 忽略非记录行 # S记录行的格式为 'S<type><address><data><checksum>\n' # 需要提取其中的地址和数据部分 if len(stripped_line) < 11: continue # 跳过不符合长度要求的记录 # S记录类型(例如:S0, S1, S2...) record_type = stripped_line[1] # 地址部分的长度,例如:S1记录有2字节地址 address_length = (ord(stripped_line[2]) - 51) * 2 # 数据部分的起始位置 data_start = 10 # 提取地址部分 address = int(stripped_line[2:2 + address_length], 16) # 提取数据部分 data = stripped_line[data_start:-2].replace(' ', '') # 将数据部分转换为字节数据 bytes_data = bytes.fromhex(data) # 写入地址和数据到Hex文件 hex_file.write(bytes([address >> 8, address & 0xFF])) hex_file.write(bytes_data) # 使用示例 s19_file_path = 'input.s19' # S19格式文件路径 hex_file_path = 'output.hex' # Hex格式文件路径 s19_to_hex(s19_file_path, hex_file_path) ``` 请确保将`input.s19`替换为你实际的S19文件路径,并将`output.hex`替换为你希望生成的Hex文件路径。 注意:上述脚本对于S19文件格式的解析是非常基础的,它假设输入的S19文件格式是正确的,并且没有进行错误处理或异常处理。在实际应用中,你可能需要增加更严格的格式校验和错误处理逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魏振东991

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值