Last active
May 1, 2025 02:30
-
-
Save Demon000/d946f7ef9a05aff59136eeaca443c7e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import io | |
import sys | |
txt_conf = sys.argv[1] | |
bin_conf = sys.argv[2] | |
with open(txt_conf, 'r', encoding='utf-8') as f: | |
data = f.read() | |
f.seek(0, io.SEEK_SET) | |
data_lines = f.readlines() | |
assert data[0] == 'R' | |
assert data[11] == 'C' | |
assert data[12] == 'R' | |
crc = data[18:22] | |
crc = int(crc, 16) | |
buf = bytearray() | |
buf.append(crc >> 8) | |
buf.append(crc & 0xff) | |
for line in data_lines[1:]: | |
line = line.strip() | |
if not line: | |
continue | |
skip = 0 | |
if line.startswith('NCI_SEND_PROP'): | |
skip = 20 | |
elif line.startswith('NCI_DIRECT_CTRL'): | |
skip = 22 | |
if skip: | |
# print(f'skip: {line[:skip]}') | |
pass | |
line = line[skip:] | |
n = '' | |
payload = bytearray() | |
invalid = False | |
for c in line: | |
c = c.strip() | |
if not c: | |
continue | |
try: | |
int(c, 16) | |
except ValueError: | |
print(f'invalid: {line}') | |
invalid = True | |
break | |
n += c | |
if len(n) == 2: | |
v = int(n, 16) | |
payload.append(v) | |
n = '' | |
if invalid: | |
continue | |
buf.append(0x2f) | |
buf.append(0x02) | |
buf.append(len(payload)) | |
buf.extend(payload) | |
with open(bin_conf, 'wb') as f: | |
f.write(buf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment