1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Added output "New Line" char to Serial

Addresses Issue #104
adds the option to re-use the character used the split input into lineson input as an append to every line sent out to the serial port.
This commit is contained in:
Dave C-J 2013-12-06 21:03:33 +00:00
parent d2ce6af486
commit 67e16adfd0
2 changed files with 57 additions and 54 deletions

View File

@ -26,15 +26,15 @@
</script>
<script type="text/x-red" data-help-name="serial in">
<p>Reads data from a local serial port.</p>
<p>Keeps reading from the serial port until it sees \n (default) or the character(s) requested. Only sets <b>msg.payload</b>.</p>
<p>Reads data from a local serial port.</p>
<p>Keeps reading from the serial port until it sees \n (default) or the character(s) requested. Only sets <b>msg.payload</b>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('serial in',{
category: 'input',
defaults: {
name: {name:""},
name: {name:""},
serial: {type:"serial-port",required:true}
},
color:"BurlyWood",
@ -64,15 +64,16 @@
</script>
<script type="text/x-red" data-help-name="serial out">
<p>Provides a connection to an outbound serial port.</p>
<p>Only the <b>msg.payload</b> is sent.</p>
<p>Provides a connection to an outbound serial port.</p>
<p>Only the <b>msg.payload</b> is sent.</p>
<p>Optionally the new line character used to split the input can be appended to every message sent out to the serial port.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('serial out',{
category: 'output',
defaults: {
name: {name:""},
name: {name:""},
serial: {type:"serial-port",required:true}
},
color:"BurlyWood",
@ -88,7 +89,6 @@
return this.name?"node_label_italic":"";
}
});
</script>
@ -121,14 +121,16 @@
</div>
<div class="form-row">
<label for="node-config-input-newline"><i class="icon-text-width"></i> New line</label>
<input type="text" id="node-config-input-newline">
<input type="text" id="node-config-input-newline" style="width: 50px;">
</div>
<!--
<div class="form-row">
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
<label for="node-config-input-addchar">&nbsp;</label>
<select type="text" id="node-config-input-addchar" style="width: 70%;">
<option value="false">Don't add 'New Line' to serial output</option>
<option value="true">Add 'New line' to serial output message</option>
</select>
</div>
-->
<div class="form-tips">Tip: the new line character is used to split the input into separate messages. It can also be added to every message sent out to the serial port.</div>
</script>
<script type="text/javascript">
@ -138,7 +140,8 @@
//name: {value:""},
serialport: {value:"",required:true},
serialbaud: {value:57600,required:true},
newline: {value:"\\n"}
newline: {value:"\\n"},
addchar: {value:false}
},
label: function() {
//return this.name||this.serialport;

View File

@ -27,6 +27,7 @@ function SerialPortNode(n) {
this.serialport = n.serialport;
this.serialbaud = n.serialbaud * 1;
this.newline = n.newline;
this.addchar = n.addchar || "false";
}
RED.nodes.registerType("serial-port",SerialPortNode);
@ -44,28 +45,29 @@ function SerialOutNode(n) {
this.error(err);
return;
}
node.addCh = "";
if (node.serialConfig.addchar == "true") { node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r"); }
node.on("input",function(msg) {
//console.log("{",msg,"}");
node.port.write(msg.payload,function(err,res) {
if (err) {
node.error(err);
}
});
var payload = msg.payload;
if (typeof payload === "object") { payload = JSON.stringify(payload); }
if (typeof payload !== "buffer") { payload = new String(payload) + node.addCh; }
node.port.write(payload,function(err,res) {
if (err) { node.error(err); }
});
});
} else {
this.error("missing serial config");
}
this.on("close", function() {
if (this.serialConfig) {
serialPool.close(this.serialConfig.serialport);
}
});
}
RED.nodes.registerType("serial out",SerialOutNode);
SerialOutNode.prototype.close = function() {
if (this.serialConfig) {
serialPool.close(this.serialConfig.serialport);
}
}
function SerialInNode(n) {
RED.nodes.createNode(this,n);
this.serial = n.serial;
@ -81,27 +83,25 @@ function SerialInNode(n) {
}
this.port.on('data', function(msg) {
// console.log("{",msg,"}");
var m = { "payload": msg };
node.send(m);
node.send({ "payload": msg });
});
} else {
this.error("missing serial config");
}
this.on("close", function() {
if (this.serialConfig) {
try {
serialPool.close(this.serialConfig.serialport);
} catch(err) {
}
this.warn("Deploying with serial-port nodes is known to occasionally cause Node-RED to hang. This is due to an open issue with the underlying module.");
}
});
}
RED.nodes.registerType("serial in",SerialInNode);
SerialInNode.prototype.close = function() {
if (this.serialConfig) {
try {
serialPool.close(this.serialConfig.serialport);
} catch(err) {
}
this.warn("Deploying with serial-port nodes is known to occasionally cause Node-RED to hang. This is due to an open issue with the underlying module.");
}
}
var serialPool = function() {
var connections = {};
return {
@ -123,29 +123,29 @@ var serialPool = function() {
try {
if (newline == "") {
obj.serial = new serialp.SerialPort(port,{
baudrate: baud,
parser: serialp.parsers.raw
baudrate: baud,
parser: serialp.parsers.raw
});
}
else {
obj.serial = new serialp.SerialPort(port,{
baudrate: baud,
parser: serialp.parsers.readline(newline)
baudrate: baud,
parser: serialp.parsers.readline(newline)
});
}
obj.serial.on('error', function(err) {
util.log("[serial] serial port "+port+" error "+err);
obj.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
util.log("[serial] serial port "+port+" error "+err);
obj.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
});
obj.serial.on('close', function() {
if (!obj._closing) {
util.log("[serial] serial port "+port+" closed unexpectedly");
obj.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
}
if (!obj._closing) {
util.log("[serial] serial port "+port+" closed unexpectedly");
obj.tout = setTimeout(function() {
setupSerial();
},settings.serialReconnectTime);
}
});
obj.serial.on('open',function() {
util.log("[serial] serial port "+port+" opened at "+baud+" baud");
@ -177,7 +177,7 @@ var serialPool = function() {
connections[port]._closing = true;
try {
connections[port].close(function() {
util.log("[serial] serial port closed");
util.log("[serial] serial port closed");
});
} catch(err) { };
}