如何操作SREC文件 [英] how to manipulate SREC file

查看:29
本文介绍了如何操作SREC文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的 S19 文件:

I have an S19 file looking something like below:

S0030000FC
S30D0003C0000F0000000000000020
S3FD00000000782EFF1FB58E00003D2B00003D2B00003D2B00003D2B00003D2B00003D
S3ED000000F83D2B00003D2B00003D2B00003D2B00003D2B00003D2B00003D2B00003D
S31500000400FFFFFFFFFFFFFFFFFFFFFFFF7EF9FFFF7D
S3FD0000041010B5DFF828000468012147F22C10C4F20300016047F22010C4F2030000
S70500008EB4B8

我想分开前两个字符和接下来的两个字符,依此类推...我希望它看起来像下面(每行的最后两个字符也要分开):

I want to separate the first two characters and also the next two characters, and so on... I want it to look like below (last two characters are also to be separated for each line):

S0, 03, 0000, FC
S3, 0D, 0003C000, 0F00000000000000, 20
S3, FD, 00000000, 782EFF1FB58E00003D2B00003D2B00003D2B00003D2B00003D2B0000, 3D
S3, ED, 000000F8, 3D2B00003D2B00003D2B00003D2B00003D2B00003D2B00003D2B0000, 3D
S3, 15, 00000400, FFFFFFFFFFFFFFFFFFFFFFFF7EF9FFFF, 7D
S3, FD, 00000410, 10B5DFF828000468012147F22C10C4F20300016047F22010C4F20300, 00
S7, 05, 00008EB4, B8

如何在 Python 中执行此操作?我有这样的事情:

How can I do this in Python? I have something like this:

 #!/usr/bin/python
 import string,os,sys,re,fileinput
 print "hi"
 inputfile = "k60.S19"
 outputfile = "k60_out.S19"

 # open the source file and read it
 fh = file(inputfile, 'r')
 subject = fh.read()
 fh.close()

 # create the pattern object. Note the "r". In case you're unfamiliar with Python
 # this is to set the string as raw so we don't have to escape our escape characters

 pattern2 = re.compile(r'S3')
 pattern3 = re.compile(r'S7')
 pattern1 = re.compile(r'S0')
 # do the replace
 result1 = pattern1.sub("S0, ", subject)
 result2 = pattern2.sub("S3, ", subject)
 result3 = pattern3.sub("S7, ", subject)

 # write the file
 f_out = file(outputfile, 'w')

 f_out.write(result1)
 f_out.write(result2)
 f_out.write(result3)
 f_out.close()

 #EoF

但它不像我喜欢的那样工作!!有人可以帮助我如何为此提出正确的正则表达式使用吗?

but it is not working as I like!! Can someone help me with how to come up with proper regular expression use for this?

推荐答案

试试包 bincopy,也许你需要它.

try package bincopy, maybe you need it.

bincopy - 将字符串解释为打包的二进制数据

bincopy - Interpret strings as packed binary data

处理传达二进制信息的各种文件格式(摩托罗拉 S-Record、英特尔 HEX 和二进制文件).

Mangling of various file formats that conveys binary information (Motorola S-Record, Intel HEX and binary files).

import bincopy
f = bincopy.BinFile()
f.add_srec_file("path/to/your/s19/flie.s19")
f.as_binary() # print s19 as binary

或者您可以轻松地对文件使用 open() :

or you can easily use open() for a file:

with open("path/to/your/s19/flie.s19") as s19:
    for line in s19:
        type = line[0:2]
        count = line[2:4]
        adress = line[4:12]
        data = line[12:-2]
        crc = line[-2:]
        print type + ", "+ count + ", " + adress + ", " + data + ", " + crc + "\n"

希望有帮助.摩托罗拉 S-record 文件格式

这篇关于如何操作SREC文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆