Appearance
可变长度粘包处理
方法名
setVariableLenStickPackage
用法
用法示例
js
module.setVariableLenStickPackage({
params:{
byteOrder: 1,
lenSize: 2,
lenIndex: 0,
offset: 0
},
success: (res) => {
console.log(res)
},
fail: () => {
console.log(res)
},
complete: (res) => {
console.log(res)
}
})js
module.setVariableLenStickPackage({
params:{
byteOrder: 1,
lenSize: 2,
lenIndex: 0,
offset: 0
},
success: (res) => {
console.log(res)
},
fail: () => {
console.log(res)
},
complete: (res) => {
console.log(res)
}
})参数说明
| 参数名 | 参数类型 | 是否必填 | 默认值 | 参数描述 |
|---|---|---|---|---|
| params | Object | 是 | 无 | 接口请求参数 |
| params.index | Integer | 否 | 1 | 串口索引,多串口使用,可选值,1~5 |
| params.byteOrder | Integer | 否 | 1 | 大端还是小端,1:大端,2:小端 |
| params.lenSize | Integer | 否 | 2 | len字段的长度,一般是2或4 |
| params.lenIndex | Integer | 否 | 0 | 长度字段的位置 |
| params.offset | Integer | 否 | 0 | 整体长度的偏移,比如有一个字段不算在len字段内 |
| success | Function | 否 | 无 | 执行成功的函数 |
| fail | Function | 否 | 无 | 执行失败的函数 |
| complete | Function | 否 | 无 | 执行完成的函数,不管成功还是失败都会执行 |
说明
可变长度的粘包处理,使用于协议中有长度字段
例:协议为: type+dataLen+data+md5
type:命名类型,两个字节
dataLen字段的长度,两个字节
data:数据字段,长度不定,长度为dataLen
md5字段,8个字节
使用时注意:
byteOrder:首先确定大小端
lenSize字段的长度,这个例子为2
lenIndex:len字段的位置,这个例子为2,因为len字段前面为type,它长度为2
offset:整个包的长度-len,这个例子是,type+dataLen+md5 三个字段的长度,也就是2+2+8=12
回调
- 示例json
{ "message": "", "code": 0, "data": { } } - 回调说明:
参数名 参数类型 参数描述 message String 消息提示 data Object 数据对象 code Integer 返回类型,0.成功,其他:失败
