licheedev / Android-SerialPort-API

Fork自Google开源的Android串口通信Demo,修改成Android Studio项目
https://code.google.com/archive/p/android-serialport-api/
Apache License 2.0
1.3k stars 374 forks source link

一次性传输数据限制 #48

Open yedashen opened 1 year ago

yedashen commented 1 year ago

为啥我从serialPort.getInputStream() 拿到的数据最多只能拿到32个字节 while (!isInterrupted()) { try { if (mInputStream == null) { return; } int available = mInputStream.available(); byte[] buffer = new byte[available]; int size = mInputStream.read(buffer); if (size > 0) { if (null != callback) { callback.callback(buffer); } } } catch (IOException e) { e.printStackTrace(); return; } }

我这里如果改成比32大的byte[] ,32位之后的数据全是0

licheedev commented 1 year ago

不要期待一次性就能把串口传输的数据读全的,我试过对接一个波特率只有4800的项目,每次read只能读到1个有效字节,解决方法是把读到的数据全缓存到一个大数组里面,然后按照具体协议进行分包。