Skip to content

Instantly share code, notes, and snippets.

@mipsparc
Last active December 14, 2023 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mipsparc/cf3bfe05b219ad87864774d0b902dbd0 to your computer and use it in GitHub Desktop.
Save mipsparc/cf3bfe05b219ad87864774d0b902dbd0 to your computer and use it in GitHub Desktop.
ATS-Pを復調したバイナリファイルから、ビットアラインメントがずれていてもCRCの正しい電文を取り出す
import bitstring
import collections
from crccheck.crc import Crc16Genibus
import re
f = open("../result_r_y_yg_nobori2-3.bin", "rb").read()
b = bitstring.BitArray(bytes=f)
r = re.compile('0x(7e.+7e)$')
def check(b):
l = []
for i in b.findall("0x7E"):
try:
for x in b.cut(80, i, i+80):
result = r.match(str(x))
if result is not None:
result = result.group()[2:]
l.append(result)
except ValueError:
pass
return l
s = check(b)
ranking = collections.Counter(s).most_common()
print("CRC合格した場合は表示されます")
print()
for bits, cnt in ranking:
crc_expect = bits[-6:-2]
data = bytearray.fromhex(bits[2:-6])
h = Crc16Genibus().calc(data)
crc_actual = f'{h:x}'.zfill(4)
if crc_expect == crc_actual:
print("-----CRC合格-----")
print("出現回数: ", cnt)
print("内容: ", bits[2:-6])
print("CRC部分: ", bits[-6:-2])
print("----------")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment