node-zserial 1.0.22 → 1.0.23
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 +33 -28
package/package.json
CHANGED
package/zserial.js
CHANGED
|
@@ -935,42 +935,47 @@ module.exports = function (RED) {
|
|
|
935
935
|
return { ok: false };
|
|
936
936
|
}
|
|
937
937
|
|
|
938
|
-
// 698(68-LEN 变体):68 LL LH C ... DATA ...
|
|
938
|
+
// 698(68-LEN 变体):68 LL LH C ... DATA ... FCS(2) 16 ;兼容 FE* 前导
|
|
939
939
|
function tryParse698Len(input) {
|
|
940
|
-
//
|
|
940
|
+
// 剥离前导 FE
|
|
941
941
|
const b = stripFE(input);
|
|
942
942
|
if (b.length < 6 || b[0] !== 0x68) return { ok: false };
|
|
943
|
-
// 避免把 645
|
|
943
|
+
// 避免把 645 错误识别为 698-LEN(645 在 b[7] 处固定为 0x68)
|
|
944
944
|
if (b.length >= 8 && b[7] === 0x68) return { ok: false };
|
|
945
945
|
|
|
946
|
-
//
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
return { ok: true, used: end + 1, frame: b.slice(0, end + 1) };
|
|
965
|
-
}
|
|
966
|
-
}
|
|
946
|
+
// ---- 严格按长度域 L 判帧 ----
|
|
947
|
+
const LL = b[1] >>> 0;
|
|
948
|
+
const LH = b[2] >>> 0;
|
|
949
|
+
let Lraw = (LH << 8) | LL; // 含单位位、保留位
|
|
950
|
+
const isKB = (Lraw & 0x4000) !== 0; // bit14:单位位(0=字节,1=KB)
|
|
951
|
+
Lraw &= 0x3FFF; // 清除单位位与保留位,仅保留长度值
|
|
952
|
+
|
|
953
|
+
// 仅支持常见“字节”为单位的场景;若为 KB 单位,展开至字节
|
|
954
|
+
let L = isKB ? (Lraw << 10) : Lraw; // KB -> *1024
|
|
955
|
+
|
|
956
|
+
// 期望 0x16 的位置(相对 b 的起点):1 + L
|
|
957
|
+
const expectedEnd = 1 + L;
|
|
958
|
+
// 缓冲区数据还不够,继续累计
|
|
959
|
+
if (b.length < expectedEnd + 1) return { ok: false };
|
|
960
|
+
// 不是以 0x16 结尾,说明前面有脏字节或起点错位,丢弃 1 字节继续
|
|
961
|
+
if (b[expectedEnd] !== 0x16) {
|
|
962
|
+
return { ok: false, used: 1, frame: b.slice(0, 1), err: "698_LEN_BAD_END" };
|
|
963
|
+
}
|
|
967
964
|
|
|
968
|
-
|
|
969
|
-
|
|
965
|
+
// FCS 校验(CRC‑16/X.25),计算区间:[LL..FCS 前一字节]
|
|
966
|
+
const fcsLo = b[expectedEnd - 2];
|
|
967
|
+
const fcsHi = b[expectedEnd - 1];
|
|
968
|
+
const fcs = (fcsHi << 8) | fcsLo;
|
|
969
|
+
const calc = crc16x25(b, 1, expectedEnd - 2);
|
|
970
|
+
if (calc !== fcs) {
|
|
971
|
+
return { ok: false, used: 1, frame: b.slice(0, 1), err: "698_LEN_FCS_FAIL" };
|
|
970
972
|
}
|
|
971
973
|
|
|
972
|
-
//
|
|
973
|
-
|
|
974
|
+
// (可选)HCS 校验:从 LL 开始至 HCS 前一字节
|
|
975
|
+
// 按 698.45,HCS 位于地址域之后;为避免复杂度,这里不强制校验 HCS。
|
|
976
|
+
// 仅在需要时可补:解析地址域长度后再计算 HCS。
|
|
977
|
+
|
|
978
|
+
return { ok: true, used: expectedEnd + 1, frame: b.slice(0, expectedEnd + 1) };
|
|
974
979
|
}
|
|
975
980
|
|
|
976
981
|
// 698(HDLC):7E ... [FCS(lo,hi)] 7E,支持 0x7D 转义与 X.25 FCS
|