S9合ってるか
make_srec_and_eep_random_data.py
#coding: utf-8
import random
import sys
def make_eep_data():
for i in range(256):
print format(i, "02X")
# make_eep_data()
# sys.exit()
#データの合計値からチェックサム値を計算する
def calc_hosu(data_sum):
tmp=int("0x"+format(data_sum,"02x"), 16)
ret = 0xFF-tmp
return hex(ret)[-2:]
#あるレコードタイプのsrecデータを適当に作る
def show_each_data_type(record_type):
if record_type == "0":
record_byte_len = 1
address_byte_len = 2
data_byte_len = 14
checksum_byte_len = 1
byte_len = address_byte_len + data_byte_len + checksum_byte_len
aline_record = format(byte_len, "02X")
aline_add = "0000"
aline_data = "746573745f646174612e73726563"
aline_checksum = byte_len + 0 + 0x74+0x65+0x73+0x74+0x5f+0x64+0x61+0x74+0x61+0x2e+0x73+0x72+0x65+0x63
str_aline_checksum = calc_hosu(aline_checksum)
print "S" + record_type + aline_record + aline_add + aline_data + str_aline_checksum
return
elif record_type == "1":
byte_len = 0x13
address_byte_len = 2
area_start_address = 0x0
area_end_address = 0x10000
elif record_type == "2":
byte_len = 0x14
record_type = "2"
address_byte_len = 3
area_start_address = 0x10000
area_end_address = 0x40000
elif record_type == "9":
byte_len = 0x1 #最後のチェックサム以外の2byte何か解らんからなくしてチェックサムの1byteだけにする
aline_add = ""
aline_data = ""
aline_checksum = byte_len
str_aline_checksum = calc_hosu(aline_checksum)
print "S" + record_type + format(byte_len, "02X") + aline_add + aline_data + str_aline_checksum
return
else:
sys.exit()
checksum_byte_len = 1
data_byte_len = byte_len - address_byte_len - checksum_byte_len
for i in range(area_start_address,area_end_address,0x10):
#byte数 = address(2)+data(16)+checksum(1) (byte_len値の1byteは含まない。checksumの計算には含む)
#タイプ byte数 address data checksum
#"S1"+ "13"+ "xxXX"+ "xxXXxxXXxxXXxxXXxxXXxxXXxxXXxxXX"+ "xx"
aline_checksum = byte_len
#address
aline_add = format(i, "0"+str(address_byte_len*2)+"X")
aline_checksum += i
#data
aline_data = ""
for j in range(data_byte_len):
adata = random.randint(0,0xF)
aline_data = aline_data + format(adata, "02X")
aline_checksum += adata
#checksum
str_aline_checksum = calc_hosu(aline_checksum)
print "S" + record_type + format(byte_len, "02X") + aline_add + aline_data + str_aline_checksum
record_type = "0"
show_each_data_type(record_type)
record_type = "1"
show_each_data_type(record_type)
record_type = "2"
show_each_data_type(record_type)
record_type = "9"
show_each_data_type(record_type)
# import binascii
# print binascii.hexlify("test_data.srec")