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

}*/

相关推荐
Mr Lee_3 分钟前
android 配置鼠标右键快捷对apk进行反编译
android
顾北川_野38 分钟前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
&岁月不待人&1 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
Winston Wood3 小时前
Android Parcelable和Serializable的区别与联系
android·序列化
清风徐来辽3 小时前
Android 项目模型配置管理
android
帅得不敢出门3 小时前
Gradle命令编译Android Studio工程项目并签名
android·ide·android studio·gradlew
problc4 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
帅得不敢出门14 小时前
安卓设备adb执行AT指令控制电话卡
android·adb·sim卡·at指令·电话卡
我又来搬代码了16 小时前
【Android】使用productFlavors构建多个变体
android
德育处主任17 小时前
Mac和安卓手机互传文件(ADB)
android·macos