Code Monkey home page Code Monkey logo

Comments (9)

cl-6666 avatar cl-6666 commented on June 12, 2024 1

最近经常有小伙伴遇到粘包问题,这里提供一个案例解决方案:
实现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;
}

}

`

from serialport.

f262866551 avatar f262866551 commented on June 12, 2024

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

from serialport.

cl-6666 avatar cl-6666 commented on June 12, 2024

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

from serialport.

f262866551 avatar f262866551 commented on June 12, 2024

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

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

from serialport.

f262866551 avatar f262866551 commented on June 12, 2024

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

from serialport.

f262866551 avatar f262866551 commented on June 12, 2024

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

from serialport.

f262866551 avatar f262866551 commented on June 12, 2024

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

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

from serialport.

cl-6666 avatar cl-6666 commented on June 12, 2024

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

from serialport.

f262866551 avatar f262866551 commented on June 12, 2024

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

好的谢谢

from serialport.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.