Android 串口数据分包处理

/*

class PacketHandler {

private static final byte PACKET_START = 0x79; // 假设包的开始标记为0x79

private static final byte PACKET_END = 0x0D; // 假设包的结束标记为0x0D

复制代码
private static ByteArrayOutputStream buffer = new ByteArrayOutputStream();

public static void onDataReceived(byte[] data) {
    buffer.write(data, 0, data.length);

    byte[] completePacket = null;
    while ((completePacket = findNextPacket(buffer.toByteArray())) != null) {
        // 处理完整的包
        processPacket(completePacket);
        // 从缓冲区中移除已处理的包
        removeProcessedPacket(buffer, completePacket);
    }
}

private static byte[] findNextPacket(byte[] data) {

    int startIndex = -1;
    for (int i = 0; i < data.length; i++) {
        if (data[i] == PACKET_START) {
            startIndex = i;
            break;
        }
    }
    if (startIndex >= 0) {
        int endIndex = -1;
        for (int i = startIndex; i < data.length; i++) {
            if (data[i] == PACKET_END) {
                endIndex = i;
                break;
            }
        }
        if (endIndex > startIndex) {
            return Arrays.copyOfRange(data, startIndex, endIndex + 1);
        }
    }
    return null;
}

static StringBuffer stringBuffer = new StringBuffer();
static boolean isComnplete;

private static void processPacket(byte[] packet) {
    // 处理包的逻辑
    String s = MyFunc.ByteArrToNoSpaceHex(packet);
    stringBuffer.append(s);
    try {

// Log.i("byteDataResult", stringBuffer.toString() + "=111=" + fromHexString(stringBuffer.toString()));

} catch (Exception e) {

e.printStackTrace();

}

复制代码
    if (stringBuffer != null && stringBuffer.toString().endsWith("23796F6E6B65720D")) {
        String dateTemp = stringBuffer.toString();
        int startPosition = dateTemp.lastIndexOf("796F6E6B657223");
        String date = dateTemp.substring(startPosition);

        stringBuffer = new StringBuffer();
        isComnplete = true;
        if (isComnplete) {
            isComnplete = false;
            Log.i("byteDataResult", "===" + date);
        }
    }
}

public static String fromHexString(String hexString) throws Exception {
    String result = "";
    hexString = hexString.toUpperCase();
    String hexDigital = "0123456789ABCDEF";
    char[] hexs = hexString.toCharArray();
    byte[] bytes = new byte[hexString.length() / 2];
    int n;
    for (int i = 0; i < bytes.length; i++) {
        n = hexDigital.indexOf(hexs[2 * i]) * 16 + hexDigital.indexOf(hexs[2 * i + 1]);
        bytes[i] = (byte) (n & 0xff);
    }
    result = new String(bytes, "UTF-8");
    return result;
}

private static void removeProcessedPacket(ByteArrayOutputStream buffer, byte[] packet) {
    byte[] newData = new byte[buffer.size() - packet.length];
    int packetStart = 0;
    for (int i = 0; i < buffer.size(); i++) {
        if (i < packet.length) {
            packetStart = i;
        } else {
            newData[i - packet.length] = buffer.toByteArray()[i];
        }
    }
    buffer.reset();
    buffer.write(newData, 0, newData.length);
}

}*/

相关推荐
2501_915918416 小时前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者86 小时前
iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
android·ios·小程序·https·uni-app·iphone·webview
雨白10 小时前
实现双向滑动的 ScalableImageView(上)
android
Y40900111 小时前
数据库基础知识——聚合函数、分组查询
android·数据库
没有了遇见16 小时前
Android 原生定位(替代高德 / 百度等三方定位)<终极版本>
android
2501_9160088917 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214317 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
CYRUS_STUDIO17 小时前
LLVM 全面解析:NDK 为什么离不开它?如何亲手编译调试 clang
android·编译器·llvm
CYRUS_STUDIO17 小时前
静态分析神器 + 动态调试利器:IDA Pro × Frida 混合调试实战
android·逆向
g_i_a_o_giao20 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析