2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2014-06-08 15:57:57 +02:00
|
|
|
* Copyright 2013,2014 IBM Corp.
|
2014-04-30 15:09:08 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-06-08 15:57:57 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var settings = RED.settings;
|
|
|
|
var events = require("events");
|
|
|
|
var util = require("util");
|
|
|
|
var serialp = require("serialport");
|
2014-06-08 15:57:57 +02:00
|
|
|
var bufMaxSize = 32768; // Max serial buffer size, for inputs...
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// TODO: 'serialPool' should be encapsulated in SerialPortNode
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function SerialPortNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.serialport = n.serialport;
|
|
|
|
this.newline = n.newline;
|
|
|
|
this.addchar = n.addchar || "false";
|
|
|
|
this.serialbaud = parseInt(n.serialbaud) || 57600;
|
|
|
|
this.databits = parseInt(n.databits) || 8;
|
|
|
|
this.parity = n.parity || "none";
|
|
|
|
this.stopbits = parseInt(n.stopbits) || 1;
|
2014-06-08 15:57:57 +02:00
|
|
|
this.bin = n.bin || "false";
|
|
|
|
this.out = n.out || "char";
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("serial-port",SerialPortNode);
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function SerialOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.serial = n.serial;
|
|
|
|
this.serialConfig = RED.nodes.getNode(this.serial);
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (this.serialConfig) {
|
|
|
|
var node = this;
|
|
|
|
node.port = serialPool.get(this.serialConfig.serialport,
|
|
|
|
this.serialConfig.serialbaud,
|
|
|
|
this.serialConfig.databits,
|
|
|
|
this.serialConfig.parity,
|
|
|
|
this.serialConfig.stopbits,
|
|
|
|
this.serialConfig.newline);
|
|
|
|
node.addCh = "";
|
|
|
|
if (node.serialConfig.addchar == "true") {
|
2014-06-08 15:57:57 +02:00
|
|
|
node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0");
|
2013-12-13 11:27:52 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on("input",function(msg) {
|
|
|
|
var payload = msg.payload;
|
|
|
|
if (!Buffer.isBuffer(payload)) {
|
|
|
|
if (typeof payload === "object") {
|
|
|
|
payload = JSON.stringify(payload);
|
|
|
|
} else {
|
2014-07-18 22:49:48 +02:00
|
|
|
payload = payload.toString();
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
payload += node.addCh;
|
|
|
|
} else if (node.addCh !== "") {
|
|
|
|
payload = Buffer.concat([payload,new Buffer(node.addCh)]);
|
2013-12-08 17:59:36 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
node.port.write(payload,function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
});
|
2013-12-06 22:03:33 +01:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
node.port.on('ready', function() {
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2014-05-12 17:32:19 +02:00
|
|
|
});
|
|
|
|
node.port.on('closed', function() {
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"not connected"});
|
2014-05-12 17:32:19 +02:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
this.error("missing serial config");
|
|
|
|
}
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-14 22:34:17 +02:00
|
|
|
this.on("close", function(done) {
|
2014-05-04 00:32:04 +02:00
|
|
|
if (this.serialConfig) {
|
2014-05-14 22:34:17 +02:00
|
|
|
serialPool.close(this.serialConfig.serialport,done);
|
|
|
|
} else {
|
|
|
|
done();
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("serial out",SerialOutNode);
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-06-08 15:57:57 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function SerialInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.serial = n.serial;
|
|
|
|
this.serialConfig = RED.nodes.getNode(this.serial);
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2013-12-06 22:03:33 +01:00
|
|
|
if (this.serialConfig) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-06-08 15:57:57 +02:00
|
|
|
node.tout = null;
|
|
|
|
var buf;
|
|
|
|
if (node.serialConfig.out != "count") { buf = new Buffer(bufMaxSize); }
|
|
|
|
else { buf = new Buffer(Number(node.serialConfig.newline)); }
|
|
|
|
var i = 0;
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"unknown"});
|
2014-05-04 00:32:04 +02:00
|
|
|
node.port = serialPool.get(this.serialConfig.serialport,
|
|
|
|
this.serialConfig.serialbaud,
|
|
|
|
this.serialConfig.databits,
|
|
|
|
this.serialConfig.parity,
|
|
|
|
this.serialConfig.stopbits,
|
2014-06-08 15:57:57 +02:00
|
|
|
this.serialConfig.newline
|
|
|
|
);
|
2014-06-24 23:27:02 +02:00
|
|
|
|
2014-07-18 22:49:48 +02:00
|
|
|
var splitc;
|
2014-06-24 23:27:02 +02:00
|
|
|
if (node.serialConfig.newline.substr(0,2) == "0x") {
|
2014-07-18 22:49:48 +02:00
|
|
|
splitc = new Buffer([parseInt(node.serialConfig.newline)]);
|
2014-06-24 23:27:02 +02:00
|
|
|
} else {
|
2014-07-18 22:49:48 +02:00
|
|
|
splitc = new Buffer(node.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0"));
|
2014-06-24 23:27:02 +02:00
|
|
|
}
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
this.port.on('data', function(msg) {
|
2014-06-08 15:57:57 +02:00
|
|
|
// single char buffer
|
2014-07-18 22:49:48 +02:00
|
|
|
if ((node.serialConfig.newline === 0)||(node.serialConfig.newline === "")) {
|
2014-06-08 16:27:15 +02:00
|
|
|
if (node.serialConfig.bin !== "bin") { node.send({"payload": String.fromCharCode(msg)}); }
|
|
|
|
else { node.send({"payload": new Buffer([msg])}); }
|
2014-06-08 15:57:57 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// do the timer thing
|
|
|
|
if (node.serialConfig.out === "time") {
|
|
|
|
if (node.tout) {
|
|
|
|
i += 1;
|
|
|
|
buf[i] = msg;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.tout = setTimeout(function () {
|
|
|
|
node.tout = null;
|
|
|
|
var m = new Buffer(i+1);
|
|
|
|
buf.copy(m,0,0,i+1);
|
|
|
|
if (node.serialConfig.bin !== "bin") { m = m.toString(); }
|
|
|
|
node.send({"payload": m});
|
2014-06-17 21:06:54 +02:00
|
|
|
m = null;
|
2014-06-08 15:57:57 +02:00
|
|
|
}, node.serialConfig.newline);
|
|
|
|
i = 0;
|
|
|
|
buf[0] = msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// count bytes into a buffer...
|
|
|
|
else if (node.serialConfig.out === "count") {
|
|
|
|
buf[i] = msg;
|
|
|
|
i += 1;
|
2014-06-24 23:27:02 +02:00
|
|
|
if ( i >= parseInt(node.serialConfig.newline)) {
|
2014-06-08 15:57:57 +02:00
|
|
|
var m = new Buffer(i);
|
2014-06-24 23:27:02 +02:00
|
|
|
buf.copy(m,0,0,i);
|
2014-06-08 15:57:57 +02:00
|
|
|
if (node.serialConfig.bin !== "bin") { m = m.toString(); }
|
|
|
|
node.send({"payload":m});
|
2014-06-17 21:06:54 +02:00
|
|
|
m = null;
|
2014-06-08 15:57:57 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// look to match char...
|
|
|
|
else if (node.serialConfig.out === "char") {
|
|
|
|
buf[i] = msg;
|
|
|
|
i += 1;
|
|
|
|
if ((msg === splitc[0]) || (i === bufMaxSize)) {
|
|
|
|
var m = new Buffer(i);
|
|
|
|
buf.copy(m,0,0,i);
|
|
|
|
if (node.serialConfig.bin !== "bin") { m = m.toString(); }
|
|
|
|
node.send({"payload":m});
|
2014-06-17 21:06:54 +02:00
|
|
|
m = null;
|
2014-06-08 15:57:57 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { console.log("Should never get here"); }
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-05-12 17:32:19 +02:00
|
|
|
this.port.on('ready', function() {
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2014-05-12 17:32:19 +02:00
|
|
|
});
|
|
|
|
this.port.on('closed', function() {
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"not connected"});
|
2014-05-12 17:32:19 +02:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
this.error("missing serial config");
|
2013-12-06 22:03:33 +01:00
|
|
|
}
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-14 22:34:17 +02:00
|
|
|
this.on("close", function(done) {
|
2014-05-04 00:32:04 +02:00
|
|
|
if (this.serialConfig) {
|
2014-05-14 22:34:17 +02:00
|
|
|
serialPool.close(this.serialConfig.serialport,done);
|
|
|
|
} else {
|
|
|
|
done();
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("serial in",SerialInNode);
|
2014-05-12 17:32:19 +02:00
|
|
|
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
var serialPool = function() {
|
|
|
|
var connections = {};
|
|
|
|
return {
|
|
|
|
get:function(port,baud,databits,parity,stopbits,newline,callback) {
|
|
|
|
var id = port;
|
|
|
|
if (!connections[id]) {
|
|
|
|
connections[id] = function() {
|
|
|
|
var obj = {
|
|
|
|
_emitter: new events.EventEmitter(),
|
|
|
|
serial: null,
|
|
|
|
_closing: false,
|
|
|
|
tout: null,
|
|
|
|
on: function(a,b) { this._emitter.on(a,b); },
|
|
|
|
close: function(cb) { this.serial.close(cb); },
|
|
|
|
write: function(m,cb) { this.serial.write(m,cb); },
|
2013-12-08 17:59:36 +01:00
|
|
|
}
|
2014-06-08 15:57:57 +02:00
|
|
|
//newline = newline.replace("\\n","\n").replace("\\r","\r");
|
2014-05-04 00:32:04 +02:00
|
|
|
var setupSerial = function() {
|
2014-06-08 15:57:57 +02:00
|
|
|
//if (newline == "") {
|
2014-05-04 00:32:04 +02:00
|
|
|
obj.serial = new serialp.SerialPort(port,{
|
|
|
|
baudrate: baud,
|
|
|
|
databits: databits,
|
|
|
|
parity: parity,
|
|
|
|
stopbits: stopbits,
|
|
|
|
parser: serialp.parsers.raw
|
2014-07-18 22:49:48 +02:00
|
|
|
},true, function(err, results) { if (err) { obj.serial.emit('error',err); } });
|
2014-06-08 15:57:57 +02:00
|
|
|
//}
|
|
|
|
//else {
|
|
|
|
// obj.serial = new serialp.SerialPort(port,{
|
|
|
|
// baudrate: baud,
|
|
|
|
// databits: databits,
|
|
|
|
// parity: parity,
|
|
|
|
// stopbits: stopbits,
|
|
|
|
// parser: serialp.parsers.readline(newline)
|
|
|
|
// },true, function(err, results) { if (err) obj.serial.emit('error',err); });
|
|
|
|
//}
|
2014-05-04 00:32:04 +02:00
|
|
|
obj.serial.on('error', function(err) {
|
|
|
|
util.log("[serial] serial port "+port+" error "+err);
|
2014-05-12 17:32:19 +02:00
|
|
|
obj._emitter.emit('closed');
|
2013-12-06 22:03:33 +01:00
|
|
|
obj.tout = setTimeout(function() {
|
|
|
|
setupSerial();
|
2013-12-08 17:59:36 +01:00
|
|
|
}, settings.serialReconnectTime);
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
obj.serial.on('close', function() {
|
|
|
|
if (!obj._closing) {
|
|
|
|
util.log("[serial] serial port "+port+" closed unexpectedly");
|
2014-05-12 17:32:19 +02:00
|
|
|
obj._emitter.emit('closed');
|
2014-05-04 00:32:04 +02:00
|
|
|
obj.tout = setTimeout(function() {
|
|
|
|
setupSerial();
|
|
|
|
}, settings.serialReconnectTime);
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
obj.serial.on('open',function() {
|
|
|
|
util.log("[serial] serial port "+port+" opened at "+baud+" baud "+databits+""+parity.charAt(0).toUpperCase()+stopbits);
|
|
|
|
if (obj.tout) { clearTimeout(obj.tout); }
|
|
|
|
//obj.serial.flush();
|
|
|
|
obj._emitter.emit('ready');
|
|
|
|
});
|
|
|
|
obj.serial.on('data',function(d) {
|
2014-06-08 15:57:57 +02:00
|
|
|
//console.log(Buffer.isBuffer(d),d.length,d);
|
|
|
|
//if (typeof d !== "string") {
|
|
|
|
// //d = d.toString();
|
|
|
|
for (var z=0; z<d.length; z++) {
|
|
|
|
obj._emitter.emit('data',d[z]);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-06-08 15:57:57 +02:00
|
|
|
//}
|
|
|
|
//else {
|
|
|
|
// obj._emitter.emit('data',d);
|
|
|
|
//}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-07-18 22:49:48 +02:00
|
|
|
obj.serial.on("disconnect",function() {
|
2014-09-08 21:18:12 +02:00
|
|
|
util.log("[serial] serial port "+port+" gone away");
|
2014-07-18 22:49:48 +02:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
setupSerial();
|
|
|
|
return obj;
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
return connections[id];
|
|
|
|
},
|
2014-05-14 22:34:17 +02:00
|
|
|
close: function(port,done) {
|
2014-05-04 00:32:04 +02:00
|
|
|
if (connections[port]) {
|
2014-05-14 22:34:17 +02:00
|
|
|
if (connections[port].tout != null) {
|
|
|
|
clearTimeout(connections[port].tout);
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
connections[port]._closing = true;
|
|
|
|
try {
|
|
|
|
connections[port].close(function() {
|
|
|
|
util.log("[serial] serial port closed");
|
2014-05-14 22:34:17 +02:00
|
|
|
done();
|
2013-12-08 17:59:36 +01:00
|
|
|
});
|
2014-07-18 22:49:48 +02:00
|
|
|
}
|
|
|
|
catch(err) { }
|
2014-05-14 22:34:17 +02:00
|
|
|
delete connections[port];
|
|
|
|
} else {
|
|
|
|
done();
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
}();
|
2014-05-12 17:32:19 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.httpAdmin.get("/serialports",function(req,res) {
|
|
|
|
serialp.list(function (err, ports) {
|
2014-06-08 15:57:57 +02:00
|
|
|
//console.log(JSON.stringify(ports));
|
2014-05-04 00:32:04 +02:00
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.write(JSON.stringify(ports));
|
|
|
|
res.end();
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|