Comments (7)
实现AbsStickPackageHelper接口,按照你自己的协议处理数据即可。
from android-serialport.
大佬,问下,这样写是不是就拆分了“AA” 打头数据的粘包问题
from android-serialport.
按照你之前的描述:“目前2种类型的数据,都是AA开头,第2位是对应长度,最后一位是合校验(2个类型的数据长度不一致)”
大致实现如此:
byte[] execute(InputStream is) {
byte[] buffer = new byte[1024];
int length = 0;
boolean hasStartFlag = false; // 是否已经接收到协议头AA
int dataLength = 0; // 数据长度
int readCount; // 实际读取到的字节数
while ((readCount = is.read(buffer, length, buffer.length - length)) > 0) {
length += readCount;
if (!hasStartFlag) { // 先找到协议头AA
int startIndex = findStartFlag(buffer, length);
if (startIndex >= 0) { // 找到了
hasStartFlag = true;
length -= startIndex;
} else { // 没找到,丢弃全部数据
length = 0;
}
} else { // 已经有协议头AA了
if (length >= 2) { // 至少要有2个字节的长度信息
dataLength = buffer[1] & 0xFF;
if (length >= dataLength + 3) { // 数据区加上校验和共dataLength+2个字节,再加1个字节的校验和
if (checkSum(buffer, 2, dataLength + 2) == buffer[dataLength + 2]) { // 校验和正确
byte[] result = new byte[dataLength];
System.arraycopy(buffer, 2, result, 0, dataLength);
System.arraycopy(buffer, dataLength + 3, buffer, 0, length - (dataLength + 3));
length -= (dataLength + 3);
hasStartFlag = false;
return result; // 返回解析结果
} else { // 校验和错误,丢弃这个包
System.arraycopy(buffer, dataLength + 3, buffer, 0, length - (dataLength + 3));
length -= (dataLength + 3);
hasStartFlag = false;
}
} else { // 数据还不够,继续读取
continue;
}
} else { // 长度信息还没有读完,继续读取
continue;
}
}
}
return null; // 没有读到完整的数据包,返回null
}
int findStartFlag(byte[] buffer, int length) {
for (int i = 0; i < length - 1; i++) {
if (buffer[i] == (byte) 0xAA && buffer[i + 1] > 0) { // 协议头AA并且长度不为0
return i;
}
}
return -1;
}
int checkSum(byte[] buffer, int offset, int length) {
int sum = 0;
for (int i = offset; i < offset + length; i++) {
sum += buffer[i] & 0xFF;
}
return sum & 0xFF;
}
from android-serialport.
能否留个联系方式,有偿解决。这边回传的result 是完整的带头和尾的吧?
写这个接口里是对的吧?
代码复制进去之后,部分参数没使用到,是没问题的吧?晚上去板子上测试..
from android-serialport.
按照你之前的描述:“目前2种类型的数据,都是AA开头,第2位是对应长度,最后一位是合校验(2个类型的数据长度不一致)”
大致实现如此:
byte[] execute(InputStream is) { byte[] buffer = new byte[1024]; int length = 0; boolean hasStartFlag = false; // 是否已经接收到协议头AA int dataLength = 0; // 数据长度 int readCount; // 实际读取到的字节数 while ((readCount = is.read(buffer, length, buffer.length - length)) > 0) { length += readCount; if (!hasStartFlag) { // 先找到协议头AA int startIndex = findStartFlag(buffer, length); if (startIndex >= 0) { // 找到了 hasStartFlag = true; length -= startIndex; } else { // 没找到,丢弃全部数据 length = 0; } } else { // 已经有协议头AA了 if (length >= 2) { // 至少要有2个字节的长度信息 dataLength = buffer[1] & 0xFF; if (length >= dataLength + 3) { // 数据区加上校验和共dataLength+2个字节,再加1个字节的校验和 if (checkSum(buffer, 2, dataLength + 2) == buffer[dataLength + 2]) { // 校验和正确 byte[] result = new byte[dataLength]; System.arraycopy(buffer, 2, result, 0, dataLength); System.arraycopy(buffer, dataLength + 3, buffer, 0, length - (dataLength + 3)); length -= (dataLength + 3); hasStartFlag = false; return result; // 返回解析结果 } else { // 校验和错误,丢弃这个包 System.arraycopy(buffer, dataLength + 3, buffer, 0, length - (dataLength + 3)); length -= (dataLength + 3); hasStartFlag = false; } } else { // 数据还不够,继续读取 continue; } } else { // 长度信息还没有读完,继续读取 continue; } } } return null; // 没有读到完整的数据包,返回null } int findStartFlag(byte[] buffer, int length) { for (int i = 0; i < length - 1; i++) { if (buffer[i] == (byte) 0xAA && buffer[i + 1] > 0) { // 协议头AA并且长度不为0 return i; } } return -1; } int checkSum(byte[] buffer, int offset, int length) { int sum = 0; for (int i = offset; i < offset + length; i++) { sum += buffer[i] & 0xFF; } return sum & 0xFF; }
我把我原来在onDataReceived方法里面做处理的改为你提供的这种方式,但是命令处理的反应速度却变慢了。。比如我点击一个按钮,要等串口返回,原来在onDataReceived里处理,立马就能接收到,现在用这种方式处理,就变慢了,请问下有什么办法解决吗?
from android-serialport.
我提供给你的是代码是为了帮助理解,你需要根据自己的业务去实现。这段代码是在一个循环中不断地检查、拆解和校验数据包,这种方式可能会造成处理速度较慢,你可以考虑异步处理方式,将数据接收和处理过程分离,避免阻塞主线程。
from android-serialport.
我提供给你的是代码是为了帮助理解,你需要根据自己的业务去实现。这段代码是在一个循环中不断地检查、拆解和校验数据包,这种方式可能会造成处理速度较慢,你可以考虑异步处理方式,将数据接收和处理过程分离,避免阻塞主线程。
我想了下,这个粘包的,接收是在execute中接收,如果再把处理扔回到onDataReceived,那不是等于没做execute这个步骤吗?一时间想不到怎么把两个部分分开。或者说在execute和onDataReceived里,分别执行什么部分的业务好呢?这个优化有点没想明白。
from android-serialport.
Related Issues (20)
- Open串口成功,可以发送数据,但接受不到数据 HOT 1
- 你好我想问一下,咱们这个支持 ModBus通讯协议,RTU模式 吗? HOT 1
- 运行demo,从demo发送数据正常,但是接收不到从pc发送的数据 HOT 11
- 能打开串口,但是接收不到消息! HOT 4
- 安卓设备发送大数据问题求助 HOT 1
- 在连接状态,拔了usb 串口,日志一直警告的报错,能监听usb串口连接状态吗 HOT 1
- ANR in com.ex.serialport (com.ex.serialport/.MainActivity) 点击了两次开启报ANR异常 HOT 1
- 串口发送正常,但接收数据单次超过9个数字类型长度app就会卡死。 HOT 6
- 我遇到了丢数据的问题 HOT 1
- 在Android 10.0版本上报错:Cannot run program "/system/bin/su": error=2, No such file or directory HOT 3
- Class 'FileDescriptor' has no field named 'descriptor'. HOT 2
- file not found and Use of undeclared identifier 'ioctl' HOT 3
- 多串口同时接收和发送数据怎样做? HOT 2
- java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.e(Log.java:236) at tp.xmaihh.serialport.SerialHelper$ReadThread.run(SerialHelper.java:114) HOT 8
- 串口发送正常,但接收数据单次超过9个数字类型长度app就会卡死
- 串口发送成功了,硬件已响应,但是程序不会收到结果(如果用其他串口软件开启一下串口后,用此项目就能显示到接收的结果) HOT 2
- serialHelper.close() HOT 1
- 如果我要读取卡片ID应该怎么做。 HOT 2
- 数据读取不全应该怎么解决 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-serialport.