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