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

呃呃呃,大神那个关于粘包那块我看了GitHub上另一个大神的项目,稍微修改了一下SerialPortReadThread的代码,好像没啥问题了,您有空的话可以看看,我也不太明白啥原理 #17

Open
ParticLIU opened this issue Nov 13, 2018 · 2 comments

Comments

@ParticLIU
Copy link

    while (!isInterrupted()) {
        try {
            if (null == mInputStream) {
                return;
            }

            int available = mInputStream.available();

            if(available > 0){
                Log.i(TAG, "run: ");
                int size = mInputStream.read(mReadBuffer);

                if (-1 == size || 0 >= size) {
                    return;
                }

                if(size > 0){
                    byte[] readBytes = new byte[size];
                    System.arraycopy(mReadBuffer, 0, readBytes, 0, size);
                    Log.i(TAG, "run: readBytes = " + new String(readBytes));
                    onDataReceived(readBytes);
                }
            }else {
                SystemClock.sleep(50);
            }
@cl-6666
Copy link

cl-6666 commented Jun 29, 2020

我遇到了只能接收32位 用了这个方法解决了

@ParticLIU
Copy link
Author

在我们后续的项目里,我们尝试了两种方案来解决串口数据分包粘包问题,仅供参考:1.私有协议,定义结束符,读取到结束符则认为一条指令结束;2.根据协议数据读取完毕所需的时间判断,当读取某条指令大于一定时间,则认为指令结束。有更好的思路欢迎交流!

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