Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接收数据不完整丢失了 #33

Closed
f262866551 opened this issue Apr 24, 2024 · 9 comments
Closed

接收数据不完整丢失了 #33

f262866551 opened this issue Apr 24, 2024 · 9 comments

Comments

@f262866551
Copy link

【警告:请务必按照 issue 模板填写】

问题描述

  • 框架版本【必填】:com.github.cl-6666:serialPort:v4.0.1

  • 问题描述【必填】:接收数据只有前面的协议头高低位,后面的数据都没有

  • 复现步骤【必填】:接收数据回调

  • 是否必现【必填】:是

  • 出现问题机型信息【必填】:RK3399

  • 出现问题的安卓版本【必填】:Android 8.0

其他

  • 提供报错堆栈(如果有报错的话必填,注意不要拿被混淆过的代码堆栈上来)

  • 提供截图或视频(根据需要提供,此项不强制)

  • 提供解决方案(如果已经解决了的话,此项不强制)
    SerialUtils.getInstance().init(this,true,"TAG",500); 初始化用的这个 但是onDataReceived的回调只有前面的协议头跟高低位

@f262866551
Copy link
Author

应该不是波特率问题,波特率用的115200,之前自己写的用的是115200数据是能正常收发的

@cl-6666
Copy link
Owner

cl-6666 commented Apr 24, 2024

你这个是粘包了,有二种处理方案
1.使用V3.1.7版本看一下是否满足你的业务需求,协议封装自己处理
2.自定义粘包处理,实现AbsStickPackageHelper接口

@f262866551
Copy link
Author

你这个是粘包了,有二种处理方案 1.使用V3.1.7版本看一下是否满足你的业务需求,协议封装自己处理 2.自定义粘包处理,实现AbsStickPackageHelper接口

AbsStickPackageHelper这个接口回调的数据也才只有六个

@f262866551
Copy link
Author

我想拿到最原始的数据该怎么做啊

@f262866551
Copy link
Author

我搞错了,谢谢了,知道怎么搞了

@f262866551
Copy link
Author

你这个是粘包了,有二种处理方案 1.使用V3.1.7版本看一下是否满足你的业务需求,协议封装自己处理 2.自定义粘包处理,实现AbsStickPackageHelper接口

有个问题,如果数据过长了,AbsStickPackageHelper是一段一段的返回,这种是不是只能协议上做处理啊

@cl-6666
Copy link
Owner

cl-6666 commented Apr 24, 2024

数据很长的话 拼接一下数据就可以了 分段接收

@f262866551
Copy link
Author

数据很长的话 拼接一下数据就可以了 分段接收

好的谢谢

@cl-6666 cl-6666 pinned this issue Apr 25, 2024
@cl-6666
Copy link
Owner

cl-6666 commented May 24, 2024

最近经常有小伙伴遇到粘包问题,这里提供一个案例解决方案:
实现AbsStickPackageHelper方法,这里演示头部固定,长度固定的案例
`
public class HeadStickPackageHelper implements AbsStickPackageHelper {
private final byte[] head;
private final List bytes;
private final int headLen;

public HeadStickPackageHelper(byte[] head) {
    this.head = head;
    if (head == null) {
        throw new IllegalStateException(" head or tail ==null");
    }
    if (head.length == 0) {
        throw new IllegalStateException(" head and tail length==0");
    }
    headLen = head.length;
    bytes = new ArrayList<>();
}

private boolean endWith(Byte[] src, byte[] target) {
    if (src.length < target.length) {
        return false;
    }
    for (int i = 0; i < target.length; i++) {
        if (target[target.length - i - 1] != src[src.length - i - 1]) {
            return false;
        }
    }
    return true;
}

private byte[] getRangeBytes(List<Byte> list, int start, int end) {
    Byte[] temps = Arrays.copyOfRange(list.toArray(new Byte[0]), start, end);
    byte[] result = new byte[temps.length];
    for (int i = 0; i < result.length; i++) {
        result[i] = temps[i];
    }
    return result;
}

@Override
public byte[] execute(InputStream is) {
    bytes.clear();
    int len = -1;
    byte temp;
    int startIndex = -1;
    byte[] result = null;
    boolean isFindStart = false;
    try {
        while ((len = is.read()) != -1) {
            temp = (byte) len;
            bytes.add(temp);
            Byte[] byteArray = bytes.toArray(new Byte[]{});
            if (headLen == 0) {//Only head or tail markers
                if (endWith(byteArray, head)) {
                    if (startIndex == -1) {
                        startIndex = bytes.size() - headLen;
                    } else {
                        result = getRangeBytes(bytes, startIndex, bytes.size());
                        break;
                    }
                }
            } else {
                if (!isFindStart) {
                    if (endWith(byteArray, head)) {
                        startIndex = bytes.size() - headLen;
                        isFindStart = true;
                    }
                } else {
                    //5代表你的数据长度,自己修改
                    if (startIndex + headLen <= bytes.size()-5) {
                        result = getRangeBytes(bytes, startIndex, bytes.size());
                        break;
                    }
                }
            }
        }
        if (len == -1) {
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants