@
Marlon 协议是私有的,这个不好放出来. 大体结构就是如下:
接收的是 UintArrayBuffer
```javascript
function decode(buffer) {
var result = {
"success": true,
"message": "成功",
}
if (buffer.length < 15) {
result['success'] = false;
result['message'] = "数据长度不符合最低要求."
return result;
}
// 切除无效数据, 直到获取到指定字符.
while ((buffer[0] !== 0x00 || buffer[8] !== 0x00)) {
buffer = buffer.slice(0, 1);
}
if (buffer.length < 15) {
result['success'] = false;
result['message'] = "无有效数据."
return result;
}
var offset = 0;
// 长度
var dataLength = ((buffer[offset] << 8) | buffer[offset + 1]) & 0xFFFF;
offset += 2;
// CRC
var data = buffer.slice(offset, offset + dataLength);
offset += dataLength;
// 校验 CRC
var crc = (((buffer[offset] << 8) & 0xFF00) | (buffer[offset + 1] & 0xFF)) & 0xFFFF;
var calcCrc = _calculateCRC16(buffer.slice(0, offset));
//
if (crc !== calcCrc) {
result['success'] = false;
result['message'] = 'crc failed. receive: ' + crc + ", calculate: " + calcCrc
return result;
}
return {
"succes": true,
"message": "成功",
"hex": byte2Hex(buffer),
"decode": {
}
};
}
```