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

目前读取回调onDataReceived中,不管发送的数据包多大,总是拿到63字节的缓冲区。无法确定收到数据的大小。 #12

Closed
wildfire810 opened this issue Apr 7, 2021 · 3 comments

Comments

@wildfire810
Copy link

如题
目前在SerialHelper的读取回调onDataReceived中,不管发送的数据包多大,总是拿到63字节的缓冲区。无法确定收到数据的大小。

@wildfire810
Copy link
Author

                byte[] buffer = getStickPackageHelper().execute(SerialHelper.this.mInputStream);
                if (buffer != null && buffer.length > 0) {
                    ComBean ComRecData = new ComBean(SerialHelper.this.sPort, buffer, buffer.length);
                    SerialHelper.this.onDataReceived(ComRecData);
                }

// int available = SerialHelper.this.mInputStream.available();
//
// if (available > 0) {
// byte[] buffer = new byte['?'];
// int size = SerialHelper.this.mInputStream.read(buffer);
// if (size > 0) {
// ComBean ComRecData = new ComBean(SerialHelper.this.sPort, buffer, size);
// SerialHelper.this.onDataReceived(ComRecData);
// }
// } else {
// SystemClock.sleep(50);
// }

这个地方,没有处理读取到的长度

@xmaihh
Copy link
Owner

xmaihh commented Apr 8, 2021

默认实现的是 BaseStickPackageHelper,实现如下:

 @Override
    public byte[] execute(InputStream is) {
        try {
            int available = is.available();
            if (available > 0) {
                byte[] buffer = new byte['?'];
                int size = is.read(buffer);
                if (size > 0) {
                    return buffer;
                }
            } else {
                SystemClock.sleep(50);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

可以看到 byte[] buffer = new byte['?'];,定义了一个长度为63的字节数组(?的ascii值为63),所以总是返回63字节的数据。
无法确定收到数据大小的原因见issue1
你需要通过实现AbsStickPackageHelper接口,按照你的协议(帧头+数据长度+数据+校验值)来取数据。

@wildfire810
Copy link
Author

谢谢。能看懂了。

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