33 lines
1019 B
Python
33 lines
1019 B
Python
|
|
def calculate_frame_lengths(file_content):
|
||
|
|
# 将文件内容转换为十六进制字节列表
|
||
|
|
hex_data = file_content.split()
|
||
|
|
|
||
|
|
frame_lengths = []
|
||
|
|
frame_start = 0
|
||
|
|
|
||
|
|
# 遍历字节列表,查找帧头
|
||
|
|
for i in range(len(hex_data) - 1):
|
||
|
|
if hex_data[i] == '55' and hex_data[i + 1] == 'AA':
|
||
|
|
# 找到帧头,计算当前帧的长度
|
||
|
|
if frame_start != 0:
|
||
|
|
frame_length = i - frame_start
|
||
|
|
frame_lengths.append(frame_length)
|
||
|
|
frame_start = i
|
||
|
|
|
||
|
|
# 处理最后一帧
|
||
|
|
if frame_start != 0:
|
||
|
|
frame_length = len(hex_data) - frame_start
|
||
|
|
frame_lengths.append(frame_length)
|
||
|
|
|
||
|
|
return frame_lengths
|
||
|
|
|
||
|
|
# 读取文件内容
|
||
|
|
with open('serial_data.txt', 'r') as file:
|
||
|
|
file_content = file.read()
|
||
|
|
|
||
|
|
# 计算每一帧的长度
|
||
|
|
frame_lengths = calculate_frame_lengths(file_content)
|
||
|
|
|
||
|
|
# 输出每一帧的长度
|
||
|
|
for i, length in enumerate(frame_lengths):
|
||
|
|
print(f"Frame {i + 1}: Length = {length} bytes")
|