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);
}

}*/

相关推荐
ha_lydms1 小时前
AnalyticDB分区、分布键性能优化
android·大数据·分布式·性能优化·分布式计算·分区·analyticdb
星辰1 小时前
Ijkplayer重新编译支持h264裸流
android
测试开发-学习笔记2 小时前
Android studio安装
android·ide·android studio
宋拾壹2 小时前
同时添加多个类目
android·开发语言·javascript
●VON3 小时前
AtomGit Flutter鸿蒙客户端:数据模型
android·服务器·安全·flutter·harmonyos·鸿蒙
火柴就是我3 小时前
记录一个文本随手指缩放的功能
android
Zender Han4 小时前
Android APK 签名 v1、v2、v3、v4 有什么区别?
android
神仙别闹4 小时前
基于 PHP + MySQL学生信息管理系统
android·mysql·php
墨狂之逸才5 小时前
Android 保活机制详解 —— 从概念到实践
android
故渊at5 小时前
第二板块:Android 四大组件标准化学理 | 第十二篇:四大组件全景总结与系统服务(System Server)架构
android·架构·wpf·四大组件·system service