node-zserial 1.0.17 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/zserial.js +34 -16
package/package.json
CHANGED
package/zserial.js
CHANGED
|
@@ -831,22 +831,40 @@ module.exports = function (RED) {
|
|
|
831
831
|
|
|
832
832
|
// 698(68-LEN 变体):68 LL LH C ... DATA ... CS 16 ;兼容 FE* 前导
|
|
833
833
|
function tryParse698Len(input) {
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
if (b[0] !== 0x68) return { ok: false };
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
834
|
+
// 剥 FE 前导
|
|
835
|
+
const b = stripFE(input);
|
|
836
|
+
if (b.length < 6 || b[0] !== 0x68) return { ok: false };
|
|
837
|
+
// 避免把 645 误判成 698-LEN
|
|
838
|
+
if (b.length >= 8 && b[7] === 0x68) return { ok: false };
|
|
839
|
+
|
|
840
|
+
// 在缓冲里寻找候选的 0x16 作为帧尾(支持一包多帧/脏数据)
|
|
841
|
+
let end = b.indexOf(0x16, 5);
|
|
842
|
+
while (end !== -1) {
|
|
843
|
+
// —— ① 先试 2 字节 CRC-16/X.25(低字节在前),计算区间:从 LL 开始到 CRC 前一字节 ——
|
|
844
|
+
if (end >= 3) {
|
|
845
|
+
const lo = b[end - 2], hi = b[end - 1];
|
|
846
|
+
const fcs = (hi << 8) | lo;
|
|
847
|
+
const calc = crc16x25(b, /*start=*/1, /*end=*/end - 2); // [LL..CRC-1]
|
|
848
|
+
if (calc === fcs) {
|
|
849
|
+
return { ok: true, used: end + 1, frame: b.slice(0, end + 1) };
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// —— ② 再试 1 字节和校验(sum8),计算区间:从 control 起到 CS 前 ——
|
|
854
|
+
if (end >= 2) {
|
|
855
|
+
const cs = b[end - 1];
|
|
856
|
+
const sum = sum8(b, /*start=*/3, /*end=*/end - 1);
|
|
857
|
+
if (cs === sum) {
|
|
858
|
+
return { ok: true, used: end + 1, frame: b.slice(0, end + 1) };
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// 找下一个 0x16
|
|
863
|
+
end = b.indexOf(0x16, end + 1);
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// 还不能定论,继续累计字节
|
|
867
|
+
return { ok: false };
|
|
850
868
|
}
|
|
851
869
|
|
|
852
870
|
// 698(HDLC):7E ... [FCS(lo,hi)] 7E,支持 0x7D 转义与 X.25 FCS
|