@ali-pay/node-red-contrib-s7 3.2.0 → 3.3.0

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/README.md CHANGED
@@ -3,6 +3,92 @@ A Node-RED node to interact with Siemens S7 PLCs.
3
3
 
4
4
  This node was created as part of the [ST-One](https://st-one.io) project.
5
5
 
6
+ ## 更新日志
7
+
8
+ ### v3.3.0
9
+
10
+ - `s7-endpoint` 节点增加 `失败重写` 的配置
11
+
12
+ ```bash
13
+ 注意!
14
+ 应与PLC开发人员约定:如果要重置数据,则数据必须至少保留3秒再重置,避免写入数据后立即重置而导致程序认为写入数据失败
15
+ ```
16
+
17
+ ```bash
18
+ 失败重写次数:该参数大于0时,当写入数据失败后,会自动重试写入数据,直至达到该次数限制
19
+
20
+ 失败重写间隔:重试写入数据的时间间隔
21
+ ```
22
+
23
+ - `s7-in` 节点重构 `msg` 的输出
24
+
25
+ ```json
26
+ {
27
+ // s7 节点参数
28
+ "_s7": {
29
+ "plc": "7#-8#", // plc 名称
30
+ "ip": "172.19.21.70", // plc ip
31
+ "status": "在线", // plc 状态 [在线/离线]
32
+ "time": "2024-07-26T02:15:38.128Z" // msg 消息时间
33
+ },
34
+
35
+ // msg 消息内容
36
+ "payload": { "a":1, "b":2 }
37
+ }
38
+ ```
39
+
40
+ - `s7-out` 节点重构 `msg` 的输出
41
+
42
+ ```json
43
+ {
44
+ // s7 节点参数
45
+ "_s7": {
46
+ "plc": "7#-8#", // plc 名称
47
+ "ip": "172.19.21.70", // plc ip
48
+ "status": "在线", // plc 状态 [在线/离线]
49
+ "time": "2024-07-26T02:15:38.128Z" // msg 消息时间
50
+ },
51
+
52
+ // msg 消息内容
53
+ "payload": {
54
+ "variable": ["a", "b"], // 写入的键 msg.variable
55
+ "payload": [1, 2], // 写入的值 msg.payload
56
+ "values": { "a":1, "b":2 }, // 写入的键值对
57
+ "newValues": {}, // plc的最新键值对
58
+ "wrongValues": {}, // 跟写入值不一致的键值对
59
+ "bingo": false, // 是否写入成功 [plc的最新值跟写入值是否一致]
60
+ "error": "Error: Not connected" // 错误
61
+ }
62
+ }
63
+ ```
64
+
65
+ - `s7` 所有节点增加 `简体中文` 的翻译
66
+
67
+ ### v3.2.0
68
+
69
+ - `s7-in` 节点增加 `设备状态` 的输出
70
+
71
+ ```json
72
+ {
73
+ "name": "7#-8#", // plc 名称
74
+ "ip": "172.19.21.70", // plc ip
75
+ "status": "online" // plc 状态 online / offline
76
+ }
77
+ ```
78
+
79
+ - `s7-out` 节点增加 `写入结果` 的输出
80
+
81
+ ```json
82
+ {
83
+ "error": "Error: Not connected", // 错误
84
+ "variable": ["a", "b"], // 写入的键 msg.variable
85
+ "payload": [1, 2], // 写入的值 msg.payload
86
+ "values": { "a":1, "b":2 }, // 写入的键值对
87
+ "newValues": {}, // plc的最新键值对
88
+ "bingo": false, // plc的最新值跟写入值是否一致
89
+ "wrongValues": {} // 跟写入值不一致的键值对
90
+ }
91
+ ```
6
92
 
7
93
  ## Install
8
94
 
package/package.json CHANGED
@@ -1,21 +1,23 @@
1
1
  {
2
2
  "name": "@ali-pay/node-red-contrib-s7",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "A Node-RED node to interact with Siemens S7 PLCs",
5
5
  "main": "index.js",
6
- "private": false,
7
6
  "dependencies": {
8
7
  "@st-one-io/nodes7": "1.1.0"
9
8
  },
10
9
  "repository": {
11
10
  "type": "git",
12
- "url": "https://github.com/st-one-io/node-red-contrib-s7.git"
11
+ "url": "https://github.com/ali-pay/node-red-contrib-s7.git"
13
12
  },
14
13
  "bugs": {
15
- "url": "https://github.com/st-one-io/node-red-contrib-s7/issues"
14
+ "url": "https://github.com/ali-pay/node-red-contrib-s7/issues"
16
15
  },
17
16
  "scripts": {
18
- "test": "echo \"Error: no test specified\" && exit 1"
17
+ "publish": "npm publish"
18
+ },
19
+ "publishConfig": {
20
+ "registry": "https://registry.npmjs.org"
19
21
  },
20
22
  "keywords": [
21
23
  "hardware",
@@ -48,6 +48,8 @@
48
48
  "tsap-remote": "Remote TSAP",
49
49
  "cycletime": "Zykluszeit",
50
50
  "timeout": "Timeout",
51
+ "rewritetimes": "Rewrite Times",
52
+ "rewriteinterval": "Rewrite Interval",
51
53
  "verbose": "Debug",
52
54
  "adapter": "Adapter",
53
55
  "adapterplaceholder": "Bitte 'node-red-contrib-mpi-s7' installieren",
@@ -48,6 +48,8 @@
48
48
  "tsap-remote": "Remote TSAP",
49
49
  "cycletime": "Cycle time",
50
50
  "timeout": "Timeout",
51
+ "rewritetimes": "Rewrite Times",
52
+ "rewriteinterval": "Rewrite Interval",
51
53
  "verbose": "Debug",
52
54
  "adapter": "Adapter",
53
55
  "adapterplaceholder": "Please install 'node-red-contrib-mpi-s7' for MPI/PPI/DP support",
@@ -0,0 +1,129 @@
1
+ {
2
+ "s7": {
3
+ "in": {
4
+ "label": {
5
+ "name": "s7 in",
6
+ "endpoint": "PLC",
7
+ "mode": "输出模式",
8
+ "variable": "变量",
9
+ "diff": "当数据有变化时才输出消息",
10
+ "variable-select": "请选择一个变量",
11
+ "variable-novar": "没有变量,请先定义变量"
12
+ },
13
+ "mode": {
14
+ "single": "单个变量",
15
+ "all-split": "所有变量,每个变量皆输出为一条消息",
16
+ "all": "所有变量"
17
+ }
18
+ },
19
+ "out": {
20
+ "label": {
21
+ "name": "s7 out",
22
+ "endpoint": "PLC",
23
+ "variable": "变量",
24
+ "variable-select": "请选择一个变量"
25
+ },
26
+ "disclaimer": "生产环境中,请谨慎使用!"
27
+ },
28
+ "control": {
29
+ "label": {
30
+ "name": "s7 control",
31
+ "function": "Function",
32
+ "endpoint": "PLC"
33
+ },
34
+ "function": {
35
+ "cycletime": "Cycle time",
36
+ "trigger": "Trigger read"
37
+ }
38
+ },
39
+ "endpoint": {
40
+ "label": {
41
+ "transport": "Transport",
42
+ "address": "Address",
43
+ "port": "Port",
44
+ "connmode": "Mode",
45
+ "rack": "Rack",
46
+ "slot": "Slot",
47
+ "tsap-local": "Local TSAP",
48
+ "tsap-remote": "Remote TSAP",
49
+ "cycletime": "循环读取周期",
50
+ "timeout": "连接超时时间",
51
+ "rewritetimes": "失败重写次数",
52
+ "rewriteinterval": "失败重写间隔",
53
+ "verbose": "Debug",
54
+ "adapter": "Adapter",
55
+ "adapterplaceholder": "Please install 'node-red-contrib-mpi-s7' for MPI/PPI/DP support",
56
+ "busaddr": "Address",
57
+ "search": "Search",
58
+ "flash-led": "Flash LED",
59
+ "tabs": {
60
+ "connection": "连接设置",
61
+ "variables": "变量设置"
62
+ },
63
+ "variables": {
64
+ "list": "变量列表",
65
+ "add": "添加",
66
+ "clean": "删除所有",
67
+ "del": "删除",
68
+ "name": "变量名",
69
+ "addr": "地址",
70
+ "export": "导出",
71
+ "import": "导入"
72
+ }
73
+ },
74
+ "placeholder": {
75
+ "address": "IP Address or FQDN (plc01.example.com)"
76
+ },
77
+ "transport": {
78
+ "iso-on-tcp": "Ethernet (ISO-on-TCP)",
79
+ "mpi-s7": "MPI/PPI/DP Adapter"
80
+ },
81
+ "connmode": {
82
+ "rack-slot": "Rack/Slot",
83
+ "tsap": "TSAP"
84
+ },
85
+ "status": {
86
+ "online": "在线",
87
+ "badvalues": "失败",
88
+ "offline": "离线",
89
+ "unknown": "未知",
90
+ "connecting": "连接中"
91
+ }
92
+ },
93
+ "label": {
94
+ "name": "名称",
95
+ "select": "Select a device"
96
+ },
97
+ "error": {
98
+ "onconnect": "Error connecting to PLC: ",
99
+ "missingconfig": "Missing configuration",
100
+ "varunknown": "Unknown variable '__var__'",
101
+ "valmismatch": "Amount of variables and data mismatch",
102
+ "noresponse": "No response from PLC, restarting communication",
103
+ "invalidconntype": "Invalid configuration type \"__connmode__\"",
104
+ "invalidtransport": "Invalid transport \"__transport__\"",
105
+ "invalidtsap": "Invalid TSAP configuration",
106
+ "invalidtimeinterval": "Invalid time interval for cycle time of \"__interval__\"",
107
+ "invalidcontrolfunction": "Invalid control function \"__function__\""
108
+ },
109
+ "info": {
110
+ "connect": "Initiating connection to PLC",
111
+ "disconnect": "Closing connection to PLC",
112
+ "novars": "No variables configured, skipping cyclic reading",
113
+ "cycletimetooshort": "Cycle time too short, enforcing minimum of __min__ ms"
114
+ },
115
+ "validation": {
116
+ "ERR_PARSE_EMPTY": "Address cannot be empty",
117
+ "ERR_PARSE_UNKNOWN_FORMAT": "Unknown address format, check node's help section",
118
+ "ERR_PARSE_ADDR_OFFSET": "Byte offset is invalid",
119
+ "ERR_PARSE_DB_NUMBER": "Number of DB is unknown or invalid",
120
+ "ERR_PARSE_DB_DATATYPE": "Data type of DB area is unknown or invalid",
121
+ "ERR_PARSE_AREA": "Area addressed is unknown or invalid",
122
+ "ERR_PARSE_DATATYPE": "Data type is unknown or invalid",
123
+ "ERR_PARSE_BIT_OFFSET": "Bit offset is missing or is invalid",
124
+ "ERR_PARSE_STRING_LEN": "String length is missing or is invalid",
125
+ "ERR_PARSE_INVALID_BIT_OFFSET": "Bit offset not supported for this type",
126
+ "ERR_PARSE_INVALID_ARR_LEN": "Array length specified is invalid"
127
+ }
128
+ }
129
+ }
package/red/s7.html CHANGED
@@ -66,6 +66,14 @@
66
66
  <label for="node-config-input-timeout"><i class="fa fa-clock-o"></i> <span data-i18n="s7.endpoint.label.timeout"></span></label>
67
67
  <input type="text" id="node-config-input-timeout" data-i18n="[placeholder]s7.endpoint.label.timeout" style="width: 60px;"> <span>ms</span>
68
68
  </div>
69
+ <div class="form-row">
70
+ <label for="node-config-input-rewritetimes"><span data-i18n="s7.endpoint.label.rewritetimes"></span></label>
71
+ <input type="text" id="node-config-input-rewritetimes" style="width: 60px;">
72
+ </div>
73
+ <div class="form-row">
74
+ <label for="node-config-input-rewriteinterval"><span data-i18n="s7.endpoint.label.rewriteinterval"></span></label>
75
+ <input type="text" id="node-config-input-rewriteinterval" style="width: 60px;"> <span>ms</span>
76
+ </div>
69
77
  <div class="form-row">
70
78
  <label for="node-config-input-name"><i class="fa fa-tag"></i> <span data-i18n="s7.label.name"></span></label>
71
79
  <input type="text" id="node-config-input-name" data-i18n="[placeholder]s7.label.name">
@@ -559,7 +567,7 @@
559
567
  validate: RED.validators.number()
560
568
  },
561
569
  slot: {
562
- value: "2",
570
+ value: "1",
563
571
  validate: RED.validators.number()
564
572
  },
565
573
  localtsaphi: {
@@ -598,6 +606,12 @@
598
606
  name: {
599
607
  value: ""
600
608
  },
609
+ rewritetimes: {
610
+ value: 0
611
+ },
612
+ rewriteinterval: {
613
+ value: 500
614
+ },
601
615
  vartable: {
602
616
  value: [{
603
617
  name: "",
@@ -780,6 +794,14 @@
780
794
  });
781
795
  });
782
796
 
797
+ $("#node-config-input-rewritetimes").spinner({
798
+ min: 0
799
+ });
800
+
801
+ $("#node-config-input-rewriteinterval").spinner({
802
+ min: 0
803
+ });
804
+
783
805
  $("#node-config-input-cycletime").spinner({
784
806
  min: 0
785
807
  });
@@ -877,8 +899,18 @@
877
899
  connTypeList.val(self.connmode);
878
900
  }
879
901
 
902
+ if (self.rewritetimes === undefined) {
903
+ self.rewritetimes = 0;
904
+ $("#node-config-input-rewritetimes").val(self.rewritetimes);
905
+ }
906
+
907
+ if (self.rewriteinterval === undefined) {
908
+ self.rewriteinterval = 500;
909
+ $("#node-config-input-rewriteinterval").val(self.rewriteinterval);
910
+ }
911
+
880
912
  if (self.timeout === undefined) {
881
- self.timeout = 1500;
913
+ self.timeout = 2000;
882
914
  $("#node-config-input-timeout").val(self.timeout);
883
915
  }
884
916
 
@@ -1127,7 +1159,7 @@
1127
1159
  },
1128
1160
  color: "#3FADB5",
1129
1161
  inputs: 0,
1130
- outputs: 2,
1162
+ outputs: 1,
1131
1163
  icon: "serial.png",
1132
1164
  label: function () {
1133
1165
  var self = this;
package/red/s7.js CHANGED
@@ -349,7 +349,6 @@ module.exports = function (RED) {
349
349
 
350
350
  itemGroup = new nodes7.S7ItemGroup(node.endpoint);
351
351
  itemGroup.setTranslationCB(k => node._vars[k]);
352
- node.itemGroup = itemGroup;
353
352
 
354
353
  let varKeys = Object.keys(node._vars)
355
354
  if (!varKeys || !varKeys.length) {
@@ -359,6 +358,10 @@ module.exports = function (RED) {
359
358
  itemGroup.addItems(varKeys);
360
359
  }
361
360
 
361
+ // 将字段加入s7 endpoint节点对象中
362
+ node.itemGroup = itemGroup;
363
+ node.rewritetimes = parseInt(config.rewritetimes);
364
+ node.rewriteinterval = parseInt(config.rewriteinterval);
362
365
  }
363
366
  RED.nodes.registerType("s7 endpoint", S7Endpoint);
364
367
 
@@ -377,20 +380,16 @@ module.exports = function (RED) {
377
380
  function sendMsg(data, key, status) {
378
381
  if (key === undefined) key = '';
379
382
  if (data instanceof Date) data = data.getTime();
380
- var msg = [
381
- {
382
- payload: data,
383
- topic: key
384
- },
385
- {
386
- payload: {
387
- name: node.endpoint.name,
388
- ip: node.endpoint.endpoint._connOptsTcp.host,
389
- status: node.endpoint.getStatus(),
390
- },
391
- topic: ''
392
- },
393
- ];
383
+ var msg = {
384
+ topic: key,
385
+ payload: data,
386
+ _s7: {
387
+ plc: node.endpoint.name,
388
+ ip: node.endpoint.endpoint._connOptsTcp.host,
389
+ status: node.endpoint.getStatus() === 'online' ? '在线' : '离线',
390
+ time: new Date(),
391
+ }
392
+ };
394
393
  statusVal = status !== undefined ? status : data;
395
394
  node.send(msg);
396
395
  node.status(generateStatus(node.endpoint.getStatus(), statusVal));
@@ -418,21 +417,17 @@ module.exports = function (RED) {
418
417
  node.status(generateStatus(s.status, statusVal));
419
418
 
420
419
  // 只触发 ['online', 'offline'] 的事件
421
- if (!['online', 'offline'].includes(node.endpoint.getStatus())) return;
422
- var msg = [
423
- {
424
- payload: {},
425
- topic: ''
426
- },
427
- {
428
- payload: {
429
- name: node.endpoint.name,
430
- ip: node.endpoint.endpoint._connOptsTcp.host,
431
- status: node.endpoint.getStatus(),
432
- },
433
- topic: ''
434
- },
435
- ];
420
+ // if (!['online', 'offline'].includes(node.endpoint.getStatus())) return;
421
+ var msg = {
422
+ topic: '',
423
+ payload: {},
424
+ _s7: {
425
+ plc: node.endpoint.name,
426
+ ip: node.endpoint.endpoint._connOptsTcp.host,
427
+ status: node.endpoint.getStatus() === 'online' ? '在线' : '离线',
428
+ time: new Date(),
429
+ }
430
+ };
436
431
  node.send(msg);
437
432
  }
438
433
 
@@ -500,63 +495,102 @@ module.exports = function (RED) {
500
495
  val: msg.payload,
501
496
  done: (error) => {
502
497
 
498
+ /**
499
+ * 第一次写入数据后,会进入本函数
500
+ */
501
+
502
+ // 写入的键
503
503
  const variable = config.variable || msg.variable
504
+ // 写入的值
504
505
  const payload = msg.payload
505
-
506
+ // 写入的键(数组)
506
507
  const variables = Array.isArray(variable) ? variable : [variable]
508
+ // 写入的值(数组)
507
509
  const payloads = Array.isArray(payload) ? payload : [payload]
508
510
 
511
+ // 写入的键值对
509
512
  const values = {}
510
513
  variables.forEach((key, index) => {
511
514
  values[key] = payloads[index]
512
515
  })
513
516
 
514
- // 调用 s7-out 后的返回消息
515
- const message = {
516
- payload: {
517
- error: error, // 错误
518
- variable: variable, // 写入的键
519
- payload: payload, // 写入的值
520
- values: values, // 写入的键值对
521
- newValues: {}, // plc的最新键值对
522
- bingo: false, // plc的最新值跟写入值是否一致
523
- wrongValues: {}, // 跟写入值不一致的键值对
524
- }
517
+ // 调用 s7-out 后的输出消息
518
+ msg._s7 = {
519
+ plc: node.endpoint.name,
520
+ ip: node.endpoint.endpoint._connOptsTcp.host,
521
+ status: node.endpoint.getStatus() === 'online' ? '在线' : '离线',
522
+ time: new Date(),
523
+ }
524
+ msg.payload = {
525
+ variable: variable, // 写入的键
526
+ payload: payload, // 写入的值
527
+ values: values, // 写入的键值对
528
+ newValues: {}, // plc的最新键值对
529
+ wrongValues: {}, // 跟写入值不一致的键值对
530
+ bingo: false, // plc的最新值跟写入值是否一致
531
+ error: error, // 错误
525
532
  }
526
533
 
527
- // 处理错误 done(r) 不生效;需要使用 node.error(e)
534
+ // 处理错误 done(e) 不生效;需要使用 node.error(e)
528
535
  // https://nodered.org/docs/creating-nodes/node-js#handling-errors
529
536
  if (error) {
530
537
  node.error(error)
531
- node.send(message)
538
+ node.send(msg)
532
539
  return
533
540
  }
534
541
 
535
- // 读取最新的值
536
- node.endpoint.itemGroup.readAllItems()
537
- .then(newValues => {
538
- // 过滤变量
539
- for (const key in newValues) {
540
- if (variables.includes(key)) {
541
- message.payload.newValues[key] = newValues[key]
542
-
543
- // 重要!需要跟PLC约定:读取完数据1秒后再清空,避免本程序做二次读取校验时读取不到当时写入的数据
544
- if (newValues[key] !== values[key]) message.payload.wrongValues[key] = newValues[key]
545
- }
546
- }
547
-
548
- // 数据是否完全写入成功
549
- const v1 = Object.keys(message.payload.values).length
550
- const v2 = Object.keys(message.payload.newValues).length
551
- const v3 = Object.keys(message.payload.wrongValues).length
552
- message.payload.bingo = v1 === v2 && v3 === 0
553
- })
554
- .catch(e => {
555
- node.error(e)
556
- })
557
- .finally(() => {
558
- node.send(message)
559
- })
542
+ // 已重写次数
543
+ let rewriteCount = 0;
544
+ // 判断是否需要重写
545
+ function rewrite() {
546
+ // 读取最新的值
547
+ node.endpoint.itemGroup.readAllItems()
548
+ .then(newValues => {
549
+
550
+ // plc的最新所有的键值对
551
+ for (const key in newValues) {
552
+
553
+ // 只匹配此次写入的变量
554
+ if (variables.includes(key)) {
555
+ // plc的最新键值对
556
+ msg.payload.newValues[key] = newValues[key]
557
+
558
+ // 跟写入值不一致的键值对
559
+ if (newValues[key] !== values[key]) msg.payload.wrongValues[key] = newValues[key]
560
+ }
561
+ }
562
+
563
+ // 判断数据是否完全写入成功
564
+ const v1 = Object.keys(msg.payload.values).length
565
+ const v2 = Object.keys(msg.payload.newValues).length
566
+ const v3 = Object.keys(msg.payload.wrongValues).length
567
+ msg.payload.bingo = v1 === v2 && v3 === 0
568
+ })
569
+ .catch(e => node.error(e))
570
+ .finally(() => {
571
+ // 判断是否需要重写数据
572
+ if (!msg.payload.bingo && node.endpoint.rewritetimes > rewriteCount) {
573
+
574
+ // 递增已重写次数
575
+ rewriteCount++
576
+
577
+ // 延时执行重写
578
+ new Promise(resolve => setTimeout(resolve, node.endpoint.rewriteinterval))
579
+ .then(() => {
580
+ node.endpoint.itemGroup.writeItems(writeObj.name, writeObj.val)
581
+ .catch(e => node.error(e))
582
+ .finally(() => rewrite()) // 判断是否需要重写
583
+ })
584
+ return
585
+ }
586
+
587
+ // 输出消息
588
+ node.send(msg)
589
+ })
590
+ }
591
+
592
+ // 判断是否需要重写
593
+ rewrite()
560
594
  }
561
595
  };
562
596