解决数据解析长度不对

This commit is contained in:
peng 2025-06-25 20:30:54 +08:00
parent 09ff19795a
commit 981a9b6b4d

View File

@ -276,19 +276,6 @@ public class ASR5515Protocol {
return null;
}
// 从起始符后开始查找帧结束符']'
int endIndex = -1;
for (int i = startIndex + 7; i < data.length; i++) {
if (data[i] == FRAME_END) {
endIndex = i;
break;
}
}
if (endIndex == -1) {
return null;
}
try {
// 提取type2字节
short type = (short)(((data[startIndex + 2] & 0xFF) << 8) | (data[startIndex + 1] & 0xFF));
@ -299,14 +286,25 @@ public class ASR5515Protocol {
// 提取len2字节
short len = (short)(((data[startIndex + 6] & 0xFF) << 8) | (data[startIndex + 5] & 0xFF));
// 提取数据部分如果有的话
int dataLength = endIndex - (startIndex + 7); // 7 = head(1) + type(2) + sn(2) + len(2)
LogManager.d(TAG, "len: "+ len + " dataLength: " + dataLength + " end data" + data[endIndex]);
// 计算帧的总长度head(1) + type(2) + sn(2) + len(2) + data(len) + end(1)
int totalFrameLength = 1 + 2 + 2 + 2 + len + 1;
// 检查是否有足够的数据
if (startIndex + totalFrameLength > data.length) {
return null;
}
// 检查帧结束符
if (data[startIndex + totalFrameLength - 1] != FRAME_END) {
return null;
}
// 提取数据部分
byte[] frameData;
if (dataLength > 0) {
frameData = new byte[dataLength];
System.arraycopy(data, startIndex + 7, frameData, 0, dataLength);
LogManager.d(TAG,"data :"+ bytesToHexString(frameData));
if (len > 0) {
frameData = new byte[len];
System.arraycopy(data, startIndex + 7, frameData, 0, len);
//LogManager.d(TAG,"data: "+ bytesToHexString(frameData));
} else {
frameData = new byte[0];
}
@ -315,11 +313,12 @@ public class ASR5515Protocol {
Frame frame = new Frame(type, frameData);
// 如果还有剩余数据保存到remainingData
if (endIndex + 1 < data.length) {
frame.remainingData = new byte[data.length - endIndex - 1];
System.arraycopy(data, endIndex + 1, frame.remainingData, 0, frame.remainingData.length);
if (startIndex + totalFrameLength < data.length) {
frame.remainingData = new byte[data.length - (startIndex + totalFrameLength)];
System.arraycopy(data, startIndex + totalFrameLength, frame.remainingData, 0, frame.remainingData.length);
}
LogManager.d(TAG, "Frame parsed - type: " + type + ", len: " + len + ", data length: " + frameData.length);
return frame;
} catch (Exception e) {
LogManager.e(TAG, "Parse frame error: " + e.getMessage());
@ -738,7 +737,7 @@ public class ASR5515Protocol {
if (frame == null || frame.command != Commands.CMD_ALGO_RESULT || frame.data.length < 4) {
return null;
}
LogManager.d(TAG," frame.data.length:"+ frame.data.length);
try {
// 解析心率值只取第一个字节
int heartRate = frame.data[4];