Skip to content

可变长度粘包处理

方法名

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)
  }
})

参数说明

参数名参数类型是否必填默认值参数描述
paramsObject接口请求参数
params.indexInteger1串口索引,多串口使用,可选值,1~5
params.byteOrderInteger1大端还是小端,1:大端,2:小端
params.lenSizeInteger2len字段的长度,一般是2或4
params.lenIndexInteger0长度字段的位置
params.offsetInteger0整体长度的偏移,比如有一个字段不算在len字段内
successFunction执行成功的函数
failFunction执行失败的函数
completeFunction执行完成的函数,不管成功还是失败都会执行

说明

可变长度的粘包处理,使用于协议中有长度字段
例:协议为: 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": {
      }
    }
  • 回调说明:
    参数名参数类型参数描述
    messageString消息提示
    dataObject数据对象
    codeInteger返回类型,0.成功,其他:失败