96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
import struct
|
|
import logging
|
|
|
|
|
|
class Command07:
|
|
def __init__(self):
|
|
self.command = 0x07 # 07H命令码
|
|
|
|
def parse_07h(self, data):
|
|
"""
|
|
解析07H回复对时命令
|
|
|
|
:param data: 完整的07H命令报文
|
|
:return: 解析后的字典或None
|
|
"""
|
|
try:
|
|
# 验证基本帧格式
|
|
if len(data) < 14 or data[0:2] != b'JX' or data[2] != 0x07:
|
|
logging.warning("07H命令帧格式不正确")
|
|
return None
|
|
|
|
# 提取桩号
|
|
pile_id_bytes = data[3:11]
|
|
|
|
# 提取时间标识
|
|
time_bytes = data[14:20]
|
|
year = time_bytes[0] + 2000
|
|
month, day, hour, minute, second = time_bytes[1:6]
|
|
timestamp = f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}"
|
|
|
|
# 解析对时结果
|
|
time_sync_result = data[20]
|
|
time_sync_result_text = "成功" if time_sync_result == 0x01 else "失败"
|
|
|
|
# 解析失败原因(如果有)
|
|
failure_reason = data[21] if len(data) > 21 else 0x00
|
|
failure_reason_text = {
|
|
0x00: "无",
|
|
0x01: "数据格式异常"
|
|
}.get(failure_reason, "未知原因")
|
|
|
|
# 打印解析结果
|
|
print("\n07H命令解析结果:")
|
|
print(f"桩号: {pile_id_bytes.hex()}")
|
|
print(f"时间标识: {timestamp}")
|
|
print(f"对时结果: {time_sync_result_text}")
|
|
print(f"失败原因: {failure_reason_text}")
|
|
|
|
return {
|
|
"pile_id": pile_id_bytes.hex(),
|
|
"timestamp": timestamp,
|
|
"time_sync_result": time_sync_result,
|
|
"time_sync_result_text": time_sync_result_text,
|
|
"failure_reason": failure_reason,
|
|
"failure_reason_text": failure_reason_text
|
|
}
|
|
|
|
except Exception as e:
|
|
logging.error(f"解析07H命令失败: {str(e)}")
|
|
return None
|
|
|
|
def process_07h(self, data):
|
|
"""
|
|
处理07H回复对时命令
|
|
|
|
:param data: 完整的07H命令报文
|
|
:return: 是否成功处理
|
|
"""
|
|
try:
|
|
parsed_data = self.parse_07h(data)
|
|
|
|
if parsed_data is None:
|
|
logging.warning("07H命令解析失败")
|
|
return False
|
|
|
|
# 记录对时结果日志
|
|
if parsed_data['time_sync_result'] == 0x01:
|
|
logging.info(f"成功处理07H对时命令: 桩号 {parsed_data['pile_id']} 对时成功")
|
|
else:
|
|
logging.warning(
|
|
f"处理07H对时命令: 桩号 {parsed_data['pile_id']} 对时失败, 原因: {parsed_data['failure_reason_text']}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
logging.error(f"处理07H命令出错: {str(e)}")
|
|
return False
|
|
|
|
|
|
# 测试用示例
|
|
if __name__ == "__main__":
|
|
# 示例报文
|
|
test_data = bytes.fromhex("4A 58 07 03 17 67 63 11 36 06 57 01 08 00 19 01 09 09 37 1F 01 00 59")
|
|
|
|
parser = Command07()
|
|
parser.process_07h(test_data) |