2025-03-14 11:02:02 +08:00

38 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import struct
import logging
class Command01:
def __init__(self):
self.pile_id = b"\x00\x27\x02\x12\x34\x56\x12\x34" # 示例桩号
def build_01h_request(self):
"""构建01H请求连接帧"""
frame = bytearray([0x4A, 0x58]) # 帧头: "JX"
frame.append(0x01) # 命令码: 01H
frame.extend(self.pile_id) # 桩号 (8字节)
frame.append(0x01) # 数据加密方式: 0x01 (不加密)
frame.extend([0x00, 0x00]) # 数据域长度 (0)
checksum = 0
for b in frame[2:-1]:
checksum ^= b
frame.append(checksum) # 校验码
return frame
def process_01h_response(self, response, sock):
"""处理02H响应由平台调用"""
if response and len(response) >= 14 and response[2] == 0x02:
result = response[14] # 请求结果 (0x01允许, 0x00拒绝)
if result == 0x01:
logging.info("连接请求成功")
return True
else:
logging.error(f"连接被拒绝,原因: {response[15]}")
return False
logging.warning("无效的02H响应")
return False
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
cmd = Command01()
request = cmd.build_01h_request()
print(f"01H请求帧: {request.hex().upper()}")