import struct import logging from datetime import datetime class Command25: def __init__(self): self.command = 0x25 # 25H命令码 def parse_25h(self, data): """解析25H充电信息命令""" try: print("\n开始解析25H充电信息命令...") print(f"接收数据: {data.hex().upper()}") # 基础校验 if len(data) < 14 or data[0:2] != b'JX' or data[2] != self.command: logging.warning("25H命令帧格式不正确") return None # 基础信息解析 pile_id = data[3:11] # 桩号 encrypt_mode = data[11] # 加密方式 data_len = struct.unpack(" 2025-01-09 12:15:44 """ def bcd_to_int(byte): """BCD转换为整数""" return ((byte >> 4) * 10) + (byte & 0x0F) try: # 解析时间字段 year = bcd_to_int(time_bytes[0]) + 2000 # BCD年转换 (0x19 -> 25 -> 2025年) month = bcd_to_int(time_bytes[1]) # BCD月 (0x01 -> 1月) day = bcd_to_int(time_bytes[2]) # BCD日 (0x09 -> 9日) hour = bcd_to_int(time_bytes[3]) # BCD时 (0x0C -> 12时) minute = bcd_to_int(time_bytes[4]) # BCD分 (0x16 -> 22分) second = bcd_to_int(time_bytes[5]) # BCD秒 (0x12 -> 18秒) # 调试输出,查看BCD解码后的结果 print(f"Debug - Raw bytes: {[hex(b) for b in time_bytes]}") print(f"Debug - Decoded: {year}-{month}-{day} {hour}:{minute}:{second}") # 返回格式化时间字符串 return f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}" except Exception as e: print(f"时间解析错误: {e}") print(f"错误的时间字节: {[hex(b) for b in time_bytes]}") return "Invalid time" # 解析每个字段 parsed_data = { # 基础信息 "pile_id": pile_id.hex().upper(), "timestamp": parse_time(data_field[0:6]), "gun_no": data_field[6], # 电压电流 "voltage": struct.unpack("