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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/zserial.js +34 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-zserial",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "Node-RED nodes to talk to serial ports",
5
5
  "dependencies": {
6
6
  "serialport": "^12.0.0"
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
- const b = stripFE(input); // 关键:剥 FE 前导
835
- if (b.length < 5) return { ok: false };
836
- if (b[0] !== 0x68) return { ok: false };
837
- if (b.length >= 8 && b[7] === 0x68) return { ok: false }; // 645 645 解析
838
- const len = (b[1] | (b[2] << 8)) >>> 0;
839
- if (len <= 0 || len > 4096) return { ok: false, used: 1, frame: b.slice(0, 1), err: "698_BAD_LENGTH" };
840
- const total = 1 + 2 + len + 2; // 68 + len2 + payload(len) + CS,16
841
- if (b.length < total) return { ok: false }; // 半包
842
- const csIdx = total - 2, endIdx = total - 1;
843
- const endFlag = b[endIdx];
844
- const csExpect = sum8(b, 3, csIdx); // 从 control 起到 CS 前
845
- const csGot = b[csIdx];
846
- const frame = b.slice(0, total);
847
- if (endFlag !== 0x16) return { ok: false, used: total, frame, err: "698_END_FLAG_16_MISSING" };
848
- if (csGot !== csExpect) return { ok: false, used: total, frame, err: "698_CHECKSUM_MISMATCH" };
849
- return { ok: true, used: total, frame };
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