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

}*/

相关推荐
m0_548514773 小时前
2024.12.10——攻防世界Web_php_include
android·前端·php
凤邪摩羯3 小时前
Android-性能优化-03-启动优化-启动耗时
android
凤邪摩羯3 小时前
Android-性能优化-02-内存优化-LeakCanary原理解析
android
喀什酱豆腐4 小时前
Handle
android
m0_748232925 小时前
Android Https和WebView
android·网络协议·https
m0_748251725 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
m0_748254667 小时前
go官方日志库带色彩格式化
android·开发语言·golang
zhangphil7 小时前
Android使用PorterDuffXfermode模式PorterDuff.Mode.SRC_OUT橡皮擦实现“刮刮乐”效果,Kotlin(2)
android·kotlin
爱学测试的李木子8 小时前
从0到1搭建 Android 自动化 python+appium 环境
android·软件测试·python·测试工具·自动化
咸芝麻鱼8 小时前
Android Studio | 连接手机设备后,启动App时出现:Waiting For DebuggerApplication (App名)...
android·adb·智能手机·android studio