安卓串口中InputStream数据接收不完整

 

串口读数组本身就很可能需要读几次才能读完,建议是写一个拼接数据的方法,每次记录读取的数据和数据的长度

 

一开始的写法如下 会出现串口读取断开情况

protected class LReadThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (!interrupted()) {
                int size;
                try {
                    //处理读取
                    byte[] buffer = new byte[64];
                    if (lInputStream == null)
                        return;
                    size = lInputStream.read(buffer);
                    if (size > 0) {
                        onDataReceived(buffer, size, 0);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

然后百度之后写法如下 不过这个写法要求你的卡号是\n结尾的

  protected class LReadThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (!interrupted()) {
                int bytes;
                int ch;//读取字符串变量
                try {
                    if (lInputStream == null)
                        return;
                    //处理读取
                    byte[] buffer = new byte[64];
                    bytes=0;
                    while ((ch = lInputStream.read()) != '\n'){
                        if (ch != -1) {
                            buffer[bytes] = (byte) ch;
                            bytes++;
                        }
                    }
                    buffer[bytes] = '\n';
                    bytes++;
                    if (bytes > 0) {
                        onDataReceived(buffer, bytes, 0);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 上面的有限制,所以呢大部分的可以是使用下面的代码处理 ,flag 是自己添加的区分那一个线程,

还有就是byte 大于8的问题,这个看自己要读取卡号的大小自己添加判断就行

private class MReadThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (!isInterrupted()) {
                try {
                    if (mInputStream == null)
                        return;
                    byte[] buffer = new byte[64];
                    int mcount;
                    if (mInputStream.available() <= 0) {
                        continue;
                    } else {
                        Thread.sleep(300);
                    }
                    mcount = mInputStream.read(buffer);
                    if (mcount > 8) {
                        onDataReceived(buffer, mcount, 1);
                    }

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

参考链接 非常感谢

https://blog.csdn.net/clliu_hust/article/details/80874272

https://blog.csdn.net/lilidejing/article/details/37913627

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值