node-zserial 1.0.7 → 1.0.9
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 +28 -39
package/package.json
CHANGED
package/zserial.js
CHANGED
|
@@ -229,9 +229,9 @@ module.exports = function (RED) {
|
|
|
229
229
|
});
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
|
-
if (!msg.hasOwnProperty("payload")) {
|
|
232
|
+
if (!msg.hasOwnProperty("payload")) {
|
|
233
233
|
done();
|
|
234
|
-
return;
|
|
234
|
+
return;
|
|
235
235
|
} // do nothing unless we have a payload
|
|
236
236
|
if (msg.hasOwnProperty("count") && (typeof msg.count === "number") && (msg.serialConfig.out === "count")) {
|
|
237
237
|
msg.serialConfig.newline = msg.count;
|
|
@@ -307,33 +307,25 @@ module.exports = function (RED) {
|
|
|
307
307
|
node.totalMsg = {};
|
|
308
308
|
node.successMsg = {};
|
|
309
309
|
node.errorMsg = {};
|
|
310
|
-
|
|
310
|
+
node._msg = null
|
|
311
|
+
function zsend(msg, err, alldone, port, done) {
|
|
311
312
|
|
|
313
|
+
let payload = msg || err;
|
|
314
|
+
node._msg.payload = payload;
|
|
315
|
+
node.totalMsg[port] = payload
|
|
312
316
|
if (msg) {
|
|
313
|
-
node.successMsg[port] =
|
|
314
|
-
|
|
315
|
-
}
|
|
317
|
+
node.successMsg[port] = msg
|
|
318
|
+
node.send([node._msg, null, null]);
|
|
316
319
|
}
|
|
317
320
|
if (err) {
|
|
318
|
-
node.errorMsg[port] =
|
|
319
|
-
|
|
320
|
-
}
|
|
321
|
+
node.errorMsg[port] = err
|
|
322
|
+
node.send([null, node._msg, null]);
|
|
321
323
|
}
|
|
322
|
-
if (node.totalMsg[port]) {
|
|
323
|
-
node.totalMsg[port] = Object.assign(node.totalMsg[port], {
|
|
324
|
-
msg: msg || err,
|
|
325
|
-
})
|
|
326
|
-
} else {
|
|
327
|
-
node.totalMsg[port] = {
|
|
328
|
-
msg: msg || err,
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
node.send([msg, err, alldone]);
|
|
333
324
|
sendAll(done);
|
|
334
325
|
|
|
335
326
|
}
|
|
336
327
|
function onMsg(msg, send, done) {
|
|
328
|
+
node._msg = msg;
|
|
337
329
|
if (!msg.serialConfigs) {
|
|
338
330
|
node.error("需要配置批量配置:msg.serialConfigs");
|
|
339
331
|
// zsend(msg, null, null)
|
|
@@ -344,10 +336,7 @@ module.exports = function (RED) {
|
|
|
344
336
|
for (var i = 0; i < msg.serialConfigs.length; i++) {
|
|
345
337
|
var serialConfig = msg.serialConfigs[i];
|
|
346
338
|
serialConfig._msgid = msg._msgid + "_" + i;
|
|
347
|
-
|
|
348
|
-
// serialConfig: serialConfig,
|
|
349
|
-
// };
|
|
350
|
-
getSerialServer(msg, serialConfig,done);
|
|
339
|
+
getSerialServer(msg, serialConfig, done);
|
|
351
340
|
}
|
|
352
341
|
}
|
|
353
342
|
|
|
@@ -355,11 +344,14 @@ module.exports = function (RED) {
|
|
|
355
344
|
try {
|
|
356
345
|
let len = Object.keys(node.totalMsg).length;
|
|
357
346
|
if (len == node.totallenth) {
|
|
358
|
-
|
|
347
|
+
let payload = {
|
|
359
348
|
totalMsg: node.totalMsg,
|
|
360
349
|
successMsg: node.successMsg,
|
|
361
350
|
errorMsg: node.errorMsg,
|
|
362
|
-
}
|
|
351
|
+
}
|
|
352
|
+
node.send([null, null,Object.assign({}, node._msg,{
|
|
353
|
+
payload: payload
|
|
354
|
+
})]);
|
|
363
355
|
done()
|
|
364
356
|
}
|
|
365
357
|
} catch (error) {
|
|
@@ -368,7 +360,7 @@ module.exports = function (RED) {
|
|
|
368
360
|
}
|
|
369
361
|
|
|
370
362
|
}
|
|
371
|
-
function getSerialServer(msg, serialConfig,done) {
|
|
363
|
+
function getSerialServer(msg, serialConfig, done) {
|
|
372
364
|
let curPort = serialPool.get(serialConfig);
|
|
373
365
|
|
|
374
366
|
if (msg.hasOwnProperty("baudrate")) {
|
|
@@ -376,9 +368,8 @@ module.exports = function (RED) {
|
|
|
376
368
|
if (isNaN(baud)) {
|
|
377
369
|
// node.error(RED._("serial.errors.badbaudrate"), msg);
|
|
378
370
|
zsend(null, {
|
|
379
|
-
msg: msg,
|
|
380
371
|
text: RED._("serial.errors.badbaudrate")
|
|
381
|
-
}, null, curPort.serial.path,done)
|
|
372
|
+
}, null, curPort.serial.path, done)
|
|
382
373
|
} else {
|
|
383
374
|
curPort.update({ baudRate: baud }, function (err, res) {
|
|
384
375
|
if (err) {
|
|
@@ -388,16 +379,15 @@ module.exports = function (RED) {
|
|
|
388
379
|
status: "ERR_UPDATE",
|
|
389
380
|
error: err,
|
|
390
381
|
text: "更新波特率失败"
|
|
391
|
-
}, null, curPort.serial.path,done)
|
|
382
|
+
}, null, curPort.serial.path, done)
|
|
392
383
|
}
|
|
393
384
|
});
|
|
394
385
|
}
|
|
395
386
|
}
|
|
396
387
|
if (!serialConfig.hasOwnProperty("payload")) {
|
|
397
388
|
zsend(null, {
|
|
398
|
-
msg: msg,
|
|
399
389
|
text: "No payload"
|
|
400
|
-
}, null, curPort.serial.path,done)
|
|
390
|
+
}, null, curPort.serial.path, done)
|
|
401
391
|
return;
|
|
402
392
|
} // do nothing unless we have a payload
|
|
403
393
|
if (msg.hasOwnProperty("count") && (typeof msg.count === "number") && (serialConfig.out === "count")) {
|
|
@@ -426,9 +416,9 @@ module.exports = function (RED) {
|
|
|
426
416
|
curPort._retryNum = 0
|
|
427
417
|
zsend(null, {
|
|
428
418
|
status: "ERR_IN_QUEUE",
|
|
429
|
-
|
|
419
|
+
text: '串口未打开,加入消息队列失败',
|
|
430
420
|
port: curPort.serial.path
|
|
431
|
-
}, null, curPort.serial.path,done);
|
|
421
|
+
}, null, curPort.serial.path, done);
|
|
432
422
|
}
|
|
433
423
|
} catch (error) {
|
|
434
424
|
node.error(error);
|
|
@@ -451,7 +441,7 @@ module.exports = function (RED) {
|
|
|
451
441
|
curPort.on('data', function (msgout, sender) {
|
|
452
442
|
if (sender !== node) { return; }
|
|
453
443
|
msgout.status = "OK";
|
|
454
|
-
zsend(msgout, null, null, curPort.serial.path,done);
|
|
444
|
+
zsend(msgout, null, null, curPort.serial.path, done);
|
|
455
445
|
});
|
|
456
446
|
curPort.on('timeout', function (msgout, sender) {
|
|
457
447
|
if (sender !== node) { return; }
|
|
@@ -459,9 +449,8 @@ module.exports = function (RED) {
|
|
|
459
449
|
node.status({ fill: "red", shape: "ring", text: "timeout:::" + curPort.serial.path });
|
|
460
450
|
zsend(null, {
|
|
461
451
|
status: "ERR_TIMEOUT",
|
|
462
|
-
msg: msgout,
|
|
463
452
|
port: curPort.serial.path
|
|
464
|
-
}, null, curPort.serial.path,done);
|
|
453
|
+
}, null, curPort.serial.path, done);
|
|
465
454
|
});
|
|
466
455
|
|
|
467
456
|
curPort.on('initerror', function (port, retryNum, olderr) {
|
|
@@ -470,7 +459,7 @@ module.exports = function (RED) {
|
|
|
470
459
|
text: `请检查端口是否打开,重试次数${retryNum}`,
|
|
471
460
|
error: olderr,
|
|
472
461
|
port: port
|
|
473
|
-
}, null, curPort.serial.path,done);
|
|
462
|
+
}, null, curPort.serial.path, done);
|
|
474
463
|
});
|
|
475
464
|
|
|
476
465
|
|
|
@@ -480,7 +469,7 @@ module.exports = function (RED) {
|
|
|
480
469
|
status: "ERR_retry",
|
|
481
470
|
text: `重试${retryNum}失败`,
|
|
482
471
|
port: port
|
|
483
|
-
}, null, curPort.serial.path,done);
|
|
472
|
+
}, null, curPort.serial.path, done);
|
|
484
473
|
});
|
|
485
474
|
|
|
486
475
|
curPort.on('closed', function (port) {
|