AndroidSerialPort icon indicating copy to clipboard operation
AndroidSerialPort copied to clipboard

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

Open ParticLIU opened this issue 5 years ago • 2 comments

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

ParticLIU avatar Nov 13 '18 01:11 ParticLIU

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

cl-6666 avatar Jun 29 '20 02:06 cl-6666

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

ParticLIU avatar Jul 03 '20 09:07 ParticLIU