node-zserial 1.0.21 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/zserial.js +54 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-zserial",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "description": "Node-RED nodes to talk to serial ports",
5
5
  "dependencies": {
6
6
  "serialport": "^12.0.0"
package/zserial.js CHANGED
@@ -24,7 +24,27 @@ module.exports = function (RED) {
24
24
  node.send(msg);
25
25
  done();
26
26
  }, node);
27
- } else {
27
+ }
28
+ else if (msg.serialConfigs) {
29
+ let allLen = msg.serialConfigs.length;
30
+ let total = msg.serialConfigs.length;
31
+ try {
32
+ for (var i = 0; i < allLen; i++) {
33
+ let serialConfig = msg.serialConfigs[i];
34
+ serialPool.close(serialConfig.serialport, function (err) {
35
+ total--;
36
+ if (total <= 0) {
37
+ node.send(msg);
38
+ done();
39
+ }
40
+ }, node);
41
+ }
42
+ } catch (error) {
43
+ node.error(error, msg);
44
+ done();
45
+ }
46
+ }
47
+ else {
28
48
  serialPool.closeAll(() => {
29
49
  node.send(msg);
30
50
  done();
@@ -915,42 +935,47 @@ module.exports = function (RED) {
915
935
  return { ok: false };
916
936
  }
917
937
 
918
- // 698(68-LEN 变体):68 LL LH C ... DATA ... CS 16 ;兼容 FE* 前导
938
+ // 698(68-LEN 变体):68 LL LH C ... DATA ... FCS(2) 16 ;兼容 FE* 前导
919
939
  function tryParse698Len(input) {
920
- // FE 前导
940
+ // 剥离前导 FE
921
941
  const b = stripFE(input);
922
942
  if (b.length < 6 || b[0] !== 0x68) return { ok: false };
923
- // 避免把 645 误判成 698-LEN
943
+ // 避免把 645 错误识别为 698-LEN(645 在 b[7] 处固定为 0x68)
924
944
  if (b.length >= 8 && b[7] === 0x68) return { ok: false };
925
945
 
926
- // 在缓冲里寻找候选的 0x16 作为帧尾(支持一包多帧/脏数据)
927
- let end = b.indexOf(0x16, 5);
928
- while (end !== -1) {
929
- // —— 先试 2 字节 CRC-16/X.25(低字节在前),计算区间:从 LL 开始到 CRC 前一字节 ——
930
- if (end >= 3) {
931
- const lo = b[end - 2], hi = b[end - 1];
932
- const fcs = (hi << 8) | lo;
933
- const calc = crc16x25(b, /*start=*/1, /*end=*/end - 2); // [LL..CRC-1]
934
- if (calc === fcs) {
935
- return { ok: true, used: end + 1, frame: b.slice(0, end + 1) };
936
- }
937
- }
938
-
939
- // —— 再试 1 字节和校验(sum8),计算区间:从 control 起到 CS 前 ——
940
- if (end >= 2) {
941
- const cs = b[end - 1];
942
- const sum = sum8(b, /*start=*/3, /*end=*/end - 1);
943
- if (cs === sum) {
944
- return { ok: true, used: end + 1, frame: b.slice(0, end + 1) };
945
- }
946
- }
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
+ }
947
964
 
948
- // 找下一个 0x16
949
- end = b.indexOf(0x16, end + 1);
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" };
950
972
  }
951
973
 
952
- // 还不能定论,继续累计字节
953
- return { ok: false };
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) };
954
979
  }
955
980
 
956
981
  // 698(HDLC):7E ... [FCS(lo,hi)] 7E,支持 0x7D 转义与 X.25 FCS