feat: 添加小车控制指令解析和校验和计算工具
This commit is contained in:
BIN
物流小车/app.exe
Normal file
BIN
物流小车/app.exe
Normal file
Binary file not shown.
72
物流小车/checksum.py
Normal file
72
物流小车/checksum.py
Normal file
@@ -0,0 +1,72 @@
|
||||
"""
|
||||
物流小车控制指令 - 校验和算法工具(交互式)
|
||||
|
||||
用法:
|
||||
python checksum.py # 启动交互模式,手动输入指令计算校验和
|
||||
"""
|
||||
|
||||
import sys
|
||||
import io
|
||||
|
||||
if sys.platform == 'win32':
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
||||
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')
|
||||
|
||||
|
||||
def calc_checksum(data: str) -> str:
|
||||
"""计算校验和:ASCII码求和 % 256 → 2位十六进制"""
|
||||
total = sum(ord(c) for c in data)
|
||||
return format(total % 256, '02X')
|
||||
|
||||
|
||||
def print_ascii_detail(data: str):
|
||||
"""打印每个字符的 ASCII 码值"""
|
||||
print(f"\n 原始字符串: {data}")
|
||||
print(f" ASCII 码分解:")
|
||||
|
||||
total = 0
|
||||
parts = []
|
||||
for c in data:
|
||||
val = ord(c)
|
||||
total += val
|
||||
parts.append(f"'{c}'({val})")
|
||||
|
||||
print(" " + " + ".join(parts))
|
||||
print(f" 总和 = {total}")
|
||||
print(f" 取余 = {total} % 256 = {total % 256}")
|
||||
print(f" 十六进制 = {calc_checksum(data)}")
|
||||
|
||||
|
||||
def main():
|
||||
print("\n" + "=" * 55)
|
||||
print(" 物流小车指令校验和工具(交互式)")
|
||||
print(" 输入原始字符串,自动计算校验和并生成完整指令")
|
||||
print(" 输入 q 退出")
|
||||
print("=" * 55)
|
||||
|
||||
while True:
|
||||
print(f"\n{'─' * 55}")
|
||||
raw = input("\n 请输入指令内容(如 LOGI:GS:001): ").strip()
|
||||
|
||||
if raw.lower() in ('q', 'quit', 'exit'):
|
||||
print("\n 再见!\n")
|
||||
break
|
||||
|
||||
if not raw:
|
||||
continue
|
||||
|
||||
# 如果用户输入的不是以 LOGI: 开头,自动拼接
|
||||
if not raw.upper().startswith('LOGI:'):
|
||||
print(f"\n [提示] 检测到未包含帧头,已自动添加 LOGI:")
|
||||
raw = 'LOGI:' + raw
|
||||
|
||||
cs = calc_checksum(raw)
|
||||
full_cmd = f"{raw}:{cs}#"
|
||||
|
||||
# 显示计算过程
|
||||
print_ascii_detail(raw)
|
||||
print(f"\n >>> 完整指令: {full_cmd}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1 +0,0 @@
|
||||
0hhh
|
||||
Reference in New Issue
Block a user