Comments (9)
最近经常有小伙伴遇到粘包问题,这里提供一个案例解决方案:
实现AbsStickPackageHelper方法,这里演示头部固定,长度固定的案例
`
public class HeadStickPackageHelper implements AbsStickPackageHelper {
private final byte[] head;
private final List bytes;
private final int headLen;
public HeadStickPackageHelper(byte[] head) {
this.head = head;
if (head == null) {
throw new IllegalStateException(" head or tail ==null");
}
if (head.length == 0) {
throw new IllegalStateException(" head and tail length==0");
}
headLen = head.length;
bytes = new ArrayList<>();
}
private boolean endWith(Byte[] src, byte[] target) {
if (src.length < target.length) {
return false;
}
for (int i = 0; i < target.length; i++) {
if (target[target.length - i - 1] != src[src.length - i - 1]) {
return false;
}
}
return true;
}
private byte[] getRangeBytes(List<Byte> list, int start, int end) {
Byte[] temps = Arrays.copyOfRange(list.toArray(new Byte[0]), start, end);
byte[] result = new byte[temps.length];
for (int i = 0; i < result.length; i++) {
result[i] = temps[i];
}
return result;
}
@Override
public byte[] execute(InputStream is) {
bytes.clear();
int len = -1;
byte temp;
int startIndex = -1;
byte[] result = null;
boolean isFindStart = false;
try {
while ((len = is.read()) != -1) {
temp = (byte) len;
bytes.add(temp);
Byte[] byteArray = bytes.toArray(new Byte[]{});
if (headLen == 0) {//Only head or tail markers
if (endWith(byteArray, head)) {
if (startIndex == -1) {
startIndex = bytes.size() - headLen;
} else {
result = getRangeBytes(bytes, startIndex, bytes.size());
break;
}
}
} else {
if (!isFindStart) {
if (endWith(byteArray, head)) {
startIndex = bytes.size() - headLen;
isFindStart = true;
}
} else {
//5代表你的数据长度,自己修改
if (startIndex + headLen <= bytes.size()-5) {
result = getRangeBytes(bytes, startIndex, bytes.size());
break;
}
}
}
}
if (len == -1) {
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return result;
}
}
`
from serialport.
应该不是波特率问题,波特率用的115200,之前自己写的用的是115200数据是能正常收发的
from serialport.
你这个是粘包了,有二种处理方案
1.使用V3.1.7版本看一下是否满足你的业务需求,协议封装自己处理
2.自定义粘包处理,实现AbsStickPackageHelper接口
from serialport.
你这个是粘包了,有二种处理方案 1.使用V3.1.7版本看一下是否满足你的业务需求,协议封装自己处理 2.自定义粘包处理,实现AbsStickPackageHelper接口
AbsStickPackageHelper这个接口回调的数据也才只有六个
from serialport.
我想拿到最原始的数据该怎么做啊
from serialport.
我搞错了,谢谢了,知道怎么搞了
from serialport.
你这个是粘包了,有二种处理方案 1.使用V3.1.7版本看一下是否满足你的业务需求,协议封装自己处理 2.自定义粘包处理,实现AbsStickPackageHelper接口
有个问题,如果数据过长了,AbsStickPackageHelper是一段一段的返回,这种是不是只能协议上做处理啊
from serialport.
数据很长的话 拼接一下数据就可以了 分段接收
from serialport.
数据很长的话 拼接一下数据就可以了 分段接收
好的谢谢
from serialport.
Related Issues (20)
- 您好,我有4路串口,如何做到开启多路串口. HOT 3
- 权限问题 HOT 1
- 博主你好,想问下这个Demo是否支持ADH815 这种柜机串口通讯 HOT 2
- 发送 和连接的回调都走了,但是设备没有在发送成功以后回调相关数据 HOT 2
- 请问收不到串口数据怎么处理?onDataReceived不调用 HOT 4
- 修改串口路径 HOT 1
- 如何实现多串口并发 HOT 1
- 是否可以设置数据位,检验位,停止位 HOT 1
- 有时候收不到数据 HOT 1
- 为啥扫描结果会重复2次 HOT 1
- ANR HOT 3
- v3.1.6 HOT 2
- 能不能更新下构建版本,最新的Android Studio Hedgehog 2023.1.1不能构建 HOT 2
- 粘包和丢包 HOT 3
- 有的设备获取不到 HOT 1
- 怎么设置停止位校验位? HOT 1
- 请严格按照 issue 模板来提问题
- 建议这个库支持TTL、RS232、RS485协议吗?TTL就是说接口时USB转串口,最后是直接插到平板的USB口上的? HOT 1
- 返回数据异常 HOT 1
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 serialport.