# -*- coding: utf-8 -*- import io, re, sys TARGET = r"C:/Users/77338/Desktop/Git/控制终端/水产投料机与四路继电器二合一/V1.4-Chinese/STM32F103_App1/HardWare/CAR/air780e.c" BASE = r"C:/Users/77338/Desktop/Git/控制终端/水产投料机与四路继电器二合一/_air780e_baseline.c" PAIRS = [ ("> CAT1: tx truncated! need %d bytes", "> CAT1: 发送截断!需 %d 字节"), ("> 4G: first publish ok", "> 4G: 首次发布成功"), ("> 4G: Reset...", "> 4G: 复位..."), ("> 4G: Reset done", "> 4G: 复位完成"), ("> AT t/o [r%u]: %s", "> AT 超时 [r%u]: %s"), ("> 4G: CGATT attached", "> 4G: CGATT 已附着"), ("> 4G: PDN activated", "> 4G: PDN 已激活"), ("> 4G: CGATT not attached, retry %d/%d", "> 4G: CGATT 未附着,重试 %d/%d"), ("> 4G: CGATT attach timeout", "> 4G: CGATT 附着超时"), ("> 4G: Net...", "> 4G: 网络..."), ("> 4G: baud -> 921600", "> 4G: 波特率 -> 921600"), ("> 4G: no SIM card detected", "> 4G: 未检测到 SIM 卡"), ("> 4G: SIM not ready", "> 4G: SIM 未就绪"), ("> 4G: signal %d%% (rssi=%d)", "> 4G: 信号 %d%% (rssi=%d)"), ("> 4G: no signal (%d%%)", "> 4G: 无信号 (%d%%)"), ("> 4G: signal weak (%d%%)", "> 4G: 信号弱 (%d%%)"), ("> 4G: CSQ query failed, continue anyway", "> 4G: CSQ 查询失败,继续执行"), ("> 4G: GNSS power on failed (module without GNSS?)", "> 4G: GNSS 上电失败(模块无 GNSS?)"), ("> 4G: GNSS aiding failed", "> 4G: GNSS 辅助失败"), ("> 4G: GNSS on", "> 4G: GNSS 已开启"), ("> 4G: Net OK", "> 4G: 网络 OK"), ("> 4G: TCP connect t/o, last[%d]: %.80s", "> 4G: TCP 连接超时,last[%d]: %.80s"), ("> 4G: MQTT connect t/o, last[%d]: %.80s", "> 4G: MQTT 连接超时,last[%d]: %.80s"), ("> 4G: MSUB accepted (OK)", "> 4G: MSUB 已接受(OK)"), ("> 4G: MSUB fail, buff[%d]: %.80s", "> 4G: MSUB 失败,buff[%d]: %.80s"), ("> OTA ver saved & verified: %u", "> OTA 版本已保存并校验: %u"), ("> OTA ver verify mismatch: wrote=%u, read=%u", "> OTA 版本校验不匹配: 写入=%u, 读取=%u"), ("> OTA ver save FAILED: %u", "> OTA 版本保存失败: %u"), ("> OTA result report FAILED (no OK after 3 retries)", "> OTA 结果上报失败(重试 3 次无 OK)"), ("> OTA result reported: success=%d", "> OTA 结果已上报: success=%d"), ("> 4G exception: NO Service, reset", "> 4G 异常: 无服务,复位"), ("> 4G exception: MQTT/TCP CLOSED, reset", "> 4G 异常: MQTT/TCP 已断开,复位"), ("> 4G exception: module reboot (boot.rom), reset", "> 4G 异常: 模块重启(boot.rom),复位"), ("> OTA: in", "> OTA: 进入"), ("> OTA: aborted, resume normal operation", "> OTA: 已中止,恢复正常运行"), ("> OTA: Net...", "> OTA: 网络..."), ("> OTA: 2nd AT test", "> OTA: 第二次 AT 测试"), ("> 2nd AT fail", "> 第二次 AT 失败"), ("> OTA: 2nd AT ok", "> OTA: 第二次 AT 成功"), ("> OTA: MCONFIG fail", "> OTA: MCONFIG 失败"), ("> OTA: MIPSTART fail", "> OTA: MIPSTART 失败"), ("> OTA: MCONNECT fail", "> OTA: MCONNECT 失败"), ("> OTA: MQTT ready", "> OTA: MQTT 就绪"), ("> Size=%dB", "> 大小=%dB"), ("* OTA Update Start", "* OTA 升级开始"), ("* Size: %d bytes", "* 大小: %d 字节"), ("> Erasing W25Q64 new slot...", "> 正在擦除 W25Q64 新槽位..."), ("> W25Q64 new slot erased", "> W25Q64 新槽位擦除完成"), ("[Download] Start >>>", "[下载] 开始 >>>"), ("> DL offset=%d", "> DL 偏移=%d"), ("> BRK fail", "> BRK 失败"), ("> BRKEND fail", "> BRKEND 失败"), ("> No data @%d", "> 无数据 @%d"), ("> DL chunk @%d incomplete: received %d/%d bytes, retry...", "> DL 数据块 @%d 不完整: 已收 %d/%d 字节,重试..."), ("> W25Q64 verify fail at offset=%d, abort OTA", "> W25Q64 校验失败 offset=%d,中止 OTA"), ("> Nothing", "> 无数据"), ("> W25Q64 CRC16=0x%04X (server=0x%04X)", "> W25Q64 CRC16=0x%04X (服务器=0x%04X)"), ("> W25Q64 CRC16 mismatch! abort OTA", "> W25Q64 CRC16 不匹配!中止 OTA"), (">>> Verify %dB...", ">>> 校验 %dB..."), ("[FAIL]", "[失败]"), ("[ OK ] Firmware OK!", "[ OK ] 固件校验通过!"), ("* OTA Download Complete!", "* OTA 下载完成!"), ("* Rebooting to BootLoader...", "* 正在重启进入 BootLoader..."), ("> OTA confirm flag written", "> OTA 确认标志已写入"), ("> OTA confirm write FAILED after 3 tries", "> OTA 确认写入失败(已重试 3 次)"), ("> 4G: Init...", "> 4G: 初始化..."), ("> 4G: no SIM, abort 4G init", "> 4G: 无 SIM,中止 4G 初始化"), ("> 4G: no signal, abort 4G init", "> 4G: 无信号,中止 4G 初始化"), ("> 4G: network failed, retry %d", "> 4G: 网络失败,重试 %d"), ("> 4G: MQTT failed, retry %d", "> 4G: MQTT 失败,重试 %d"), ("> 4G: connect failed, no SIM=%d, no signal=%d", "> 4G: 连接失败,无 SIM=%d, 无信号=%d"), ("> GPS: CGNSINF timeout", "> GPS: CGNSINF 超时"), ("> GPS: no fix (run=%d fix=%d), skip", "> GPS: 未定位(run=%d fix=%d),跳过"), ] def placeholder_sig(s): return re.findall(r'%(?:\d+\$)?[-+ #0]*(?:\*|\d+)?(?:\.(?:\*|\d+))?[hlLzjt]*[%diuoxXfFeEgGcsSpn]', s) with io.open(BASE, 'r', encoding='utf-8', newline='') as f: base = f.read() text = base n = 0 for old, new in PAIRS: lo, ln = old, new cnt = text.count(lo) if cnt != 1: print("ERROR: literal occurs %d times (expected 1): %r" % (cnt, old)) sys.exit(1) if placeholder_sig(old) != placeholder_sig(new): print("ERROR: placeholder mismatch: %r -> %r" % (old, new)) sys.exit(1) text = text.replace(lo, ln) n += 1 with io.open(TARGET, 'w', encoding='utf-8', newline='') as f: f.write(text) print("translated: %d literals" % n) # ---- verification: strip all string-literal contents, byte-compare ---- STRIP_RE = re.compile(r'"(?:\\.|[^"\\])*"') def strip_strings(s): return STRIP_RE.sub('""', s) with io.open(TARGET, 'r', encoding='utf-8', newline='') as f: new_text = f.read() a = strip_strings(base).encode('utf-8') b = strip_strings(new_text).encode('utf-8') if a == b: print("VERIFY OK: code outside string literals is byte-identical") else: print("VERIFY FAILED") la, lb = strip_strings(base).splitlines(), strip_strings(new_text).splitlines() for i, (x, y) in enumerate(zip(la, lb), 1): if x != y: print("line %d:\n base: %s\n new : %s" % (i, x, y)) if len(la) != len(lb): print("line count differs: %d vs %d" % (len(la), len(lb))) sys.exit(2) # check no BOM raw = open(TARGET, 'rb').read() print("BOM present:", raw[:3] == b'\xef\xbb\xbf')