0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

適当なsrec形式のデータを作る

Last updated at Posted at 2019-06-16

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")


0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?