node-zserial 1.0.6 → 1.0.7

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 +31 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-zserial",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Node-RED nodes to talk to serial ports",
5
5
  "dependencies": {
6
6
  "serialport": "^12.0.0"
package/zserial.js CHANGED
@@ -46,6 +46,7 @@ module.exports = function (RED) {
46
46
  node.error('msg.serialConfig 没有定义,已创建更新波特率使用 msg.baudrate');
47
47
  msg.status = "NO_SERIAL_CONFIG";
48
48
  node.send(msg);
49
+ done();
49
50
  return;
50
51
  }
51
52
  let curPort = serialPool.get(msg.serialConfig);
@@ -64,6 +65,7 @@ module.exports = function (RED) {
64
65
  }
65
66
  if (!msg.hasOwnProperty("payload")) {
66
67
  node.warn(RED._("serial.errors.nopayload"));
68
+ done();
67
69
  return;
68
70
  } // do nothing unless we have a payload
69
71
 
@@ -75,6 +77,7 @@ module.exports = function (RED) {
75
77
  if (err) {
76
78
  node.error(err);
77
79
  }
80
+ done();
78
81
  });
79
82
  } else {
80
83
  var payload = curPort.encodePayload(msg.payload);
@@ -85,6 +88,7 @@ module.exports = function (RED) {
85
88
  } else {
86
89
  msg.status = "OK";
87
90
  }
91
+ done();
88
92
  });
89
93
  }
90
94
  }
@@ -93,7 +97,6 @@ module.exports = function (RED) {
93
97
  let curPort = serialPool.get(msg.serialConfig);
94
98
  // 确保只绑定一次事件
95
99
  if (curPort._isBindOnOutEventInit) {
96
- // if (done) done();
97
100
  return;
98
101
  }
99
102
  curPort._isBindOnOutEventInit = true;
@@ -122,7 +125,8 @@ module.exports = function (RED) {
122
125
  RED.nodes.createNode(this, n);
123
126
  var node = this;
124
127
  function onMsg(msg, send, done) {
125
- setCallback();
128
+ setCallback(msg, send, done);
129
+ done();
126
130
  }
127
131
 
128
132
  this.on("input", function (msg, send, done) {
@@ -135,7 +139,7 @@ module.exports = function (RED) {
135
139
  return { conns, connKeys }
136
140
  }
137
141
 
138
- function setCallback() {
142
+ function setCallback(msg, send, done) {
139
143
  let { conns, connKeys } = getConnection();
140
144
  // node.warn(connKeys);
141
145
  node.status({ fill: "green", shape: "dot", text: "当前连接数:" + connKeys.length });
@@ -207,6 +211,7 @@ module.exports = function (RED) {
207
211
 
208
212
  if (!msg.serialConfig) {
209
213
  node.error('msg.serialConfig 没有定义,已创建更新波特率使用 msg.baudrate');
214
+ done();
210
215
  return;
211
216
  }
212
217
  let curPort = serialPool.get(msg.serialConfig);
@@ -224,7 +229,10 @@ module.exports = function (RED) {
224
229
  });
225
230
  }
226
231
  }
227
- if (!msg.hasOwnProperty("payload")) { return; } // do nothing unless we have a payload
232
+ if (!msg.hasOwnProperty("payload")) {
233
+ done();
234
+ return;
235
+ } // do nothing unless we have a payload
228
236
  if (msg.hasOwnProperty("count") && (typeof msg.count === "number") && (msg.serialConfig.out === "count")) {
229
237
  msg.serialConfig.newline = msg.count;
230
238
  }
@@ -244,7 +252,6 @@ module.exports = function (RED) {
244
252
  let curPort = serialPool.get(msg.serialConfig);
245
253
  // 确保只绑定一次事件
246
254
  if (curPort._isBindEventInit) {
247
- // if (done) done();
248
255
  return;
249
256
  }
250
257
  // node.warn("setCallback called for " + curPort.serial.path);
@@ -257,20 +264,21 @@ module.exports = function (RED) {
257
264
  node.status({ fill: "green", shape: "dot", text: "ok:::" + curPort.serial.path });
258
265
  msgout.status = "OK";
259
266
  node.send(msgout);
260
- // if (done) done();
267
+ if (done) done();
261
268
  });
262
269
  curPort.on('timeout', function (msgout, sender) {
263
270
  if (sender !== node) { return; }
264
271
  msgout.status = "ERR_TIMEOUT";
265
272
  node.status({ fill: "red", shape: "ring", text: "timeout:::" + curPort.serial.path });
266
273
  node.send(msgout);
267
- // if (done) done();
274
+ if (done) done();
268
275
  });
269
276
  curPort.on('ready', function () {
270
277
  node.status({ fill: "green", shape: "dot", text: "connected:::" + curPort.serial.path });
271
278
  });
272
279
  curPort.on('closed', function () {
273
280
  node.status({ fill: "red", shape: "ring", text: "not-connected:::" + curPort.serial.path });
281
+ if (done) done();
274
282
  });
275
283
  curPort.on('stopped', function () {
276
284
  node.status({ fill: "grey", shape: "ring", text: "stopped:::" + curPort.serial.path });
@@ -299,7 +307,7 @@ module.exports = function (RED) {
299
307
  node.totalMsg = {};
300
308
  node.successMsg = {};
301
309
  node.errorMsg = {};
302
- function zsend(msg, err, alldone, port) {
310
+ function zsend(msg, err, alldone, port,done) {
303
311
 
304
312
  if (msg) {
305
313
  node.successMsg[port] = {
@@ -322,7 +330,7 @@ module.exports = function (RED) {
322
330
  }
323
331
 
324
332
  node.send([msg, err, alldone]);
325
- sendAll();
333
+ sendAll(done);
326
334
 
327
335
  }
328
336
  function onMsg(msg, send, done) {
@@ -339,11 +347,11 @@ module.exports = function (RED) {
339
347
  // totalMsg[serialConfig.serialport] = {
340
348
  // serialConfig: serialConfig,
341
349
  // };
342
- getSerialServer(msg, serialConfig);
350
+ getSerialServer(msg, serialConfig,done);
343
351
  }
344
352
  }
345
353
 
346
- function sendAll() {
354
+ function sendAll(done) {
347
355
  try {
348
356
  let len = Object.keys(node.totalMsg).length;
349
357
  if (len == node.totallenth) {
@@ -352,13 +360,15 @@ module.exports = function (RED) {
352
360
  successMsg: node.successMsg,
353
361
  errorMsg: node.errorMsg,
354
362
  }]);
363
+ done()
355
364
  }
356
365
  } catch (error) {
357
366
  node.error(error);
367
+ done()
358
368
  }
359
369
 
360
370
  }
361
- function getSerialServer(msg, serialConfig) {
371
+ function getSerialServer(msg, serialConfig,done) {
362
372
  let curPort = serialPool.get(serialConfig);
363
373
 
364
374
  if (msg.hasOwnProperty("baudrate")) {
@@ -368,7 +378,7 @@ module.exports = function (RED) {
368
378
  zsend(null, {
369
379
  msg: msg,
370
380
  text: RED._("serial.errors.badbaudrate")
371
- }, null, curPort.serial.path)
381
+ }, null, curPort.serial.path,done)
372
382
  } else {
373
383
  curPort.update({ baudRate: baud }, function (err, res) {
374
384
  if (err) {
@@ -378,7 +388,7 @@ module.exports = function (RED) {
378
388
  status: "ERR_UPDATE",
379
389
  error: err,
380
390
  text: "更新波特率失败"
381
- }, null, curPort.serial.path)
391
+ }, null, curPort.serial.path,done)
382
392
  }
383
393
  });
384
394
  }
@@ -387,7 +397,7 @@ module.exports = function (RED) {
387
397
  zsend(null, {
388
398
  msg: msg,
389
399
  text: "No payload"
390
- }, null, curPort.serial.path)
400
+ }, null, curPort.serial.path,done)
391
401
  return;
392
402
  } // do nothing unless we have a payload
393
403
  if (msg.hasOwnProperty("count") && (typeof msg.count === "number") && (serialConfig.out === "count")) {
@@ -395,9 +405,7 @@ module.exports = function (RED) {
395
405
  }
396
406
  if (msg.hasOwnProperty("flush") && msg.flush === true) { curPort.serial.flush(); }
397
407
 
398
- setCallback(msg, serialConfig, function (err, res) {
399
-
400
- });
408
+ setCallback(msg, serialConfig, done);
401
409
  // msg.payload = serialConfig.payload;
402
410
  setTimeout(function () {
403
411
  try {
@@ -420,7 +428,7 @@ module.exports = function (RED) {
420
428
  status: "ERR_IN_QUEUE",
421
429
  msg: '串口未打开,加入消息队列失败',
422
430
  port: curPort.serial.path
423
- }, null, curPort.serial.path);
431
+ }, null, curPort.serial.path,done);
424
432
  }
425
433
  } catch (error) {
426
434
  node.error(error);
@@ -443,7 +451,7 @@ module.exports = function (RED) {
443
451
  curPort.on('data', function (msgout, sender) {
444
452
  if (sender !== node) { return; }
445
453
  msgout.status = "OK";
446
- zsend(msgout, null, null, curPort.serial.path);
454
+ zsend(msgout, null, null, curPort.serial.path,done);
447
455
  });
448
456
  curPort.on('timeout', function (msgout, sender) {
449
457
  if (sender !== node) { return; }
@@ -453,7 +461,7 @@ module.exports = function (RED) {
453
461
  status: "ERR_TIMEOUT",
454
462
  msg: msgout,
455
463
  port: curPort.serial.path
456
- }, null, curPort.serial.path);
464
+ }, null, curPort.serial.path,done);
457
465
  });
458
466
 
459
467
  curPort.on('initerror', function (port, retryNum, olderr) {
@@ -462,7 +470,7 @@ module.exports = function (RED) {
462
470
  text: `请检查端口是否打开,重试次数${retryNum}`,
463
471
  error: olderr,
464
472
  port: port
465
- }, null, curPort.serial.path);
473
+ }, null, curPort.serial.path,done);
466
474
  });
467
475
 
468
476
 
@@ -472,7 +480,7 @@ module.exports = function (RED) {
472
480
  status: "ERR_retry",
473
481
  text: `重试${retryNum}失败`,
474
482
  port: port
475
- }, null, curPort.serial.path);
483
+ }, null, curPort.serial.path,done);
476
484
  });
477
485
 
478
486
  curPort.on('closed', function (port) {