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

@ -66,6 +66,7 @@
<script type="text/x-red" data-help-name="serial out"> <script type="text/x-red" data-help-name="serial out">
<p>Provides a connection to an outbound serial port.</p> <p>Provides a connection to an outbound serial port.</p>
<p>Only the <b>msg.payload</b> is sent.</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>
<script type="text/javascript"> <script type="text/javascript">
@ -88,7 +89,6 @@
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
} }
}); });
</script> </script>
@ -121,14 +121,16 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-newline"><i class="icon-text-width"></i> New line</label> <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>
<!--
<div class="form-row"> <div class="form-row">
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label> <label for="node-config-input-addchar">&nbsp;</label>
<input type="text" id="node-config-input-name" placeholder="Name"> <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>
--> <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>
<script type="text/javascript"> <script type="text/javascript">
@ -138,7 +140,8 @@
//name: {value:""}, //name: {value:""},
serialport: {value:"",required:true}, serialport: {value:"",required:true},
serialbaud: {value:57600,required:true}, serialbaud: {value:57600,required:true},
newline: {value:"\\n"} newline: {value:"\\n"},
addchar: {value:false}
}, },
label: function() { label: function() {
//return this.name||this.serialport; //return this.name||this.serialport;

View File

@ -27,6 +27,7 @@ function SerialPortNode(n) {
this.serialport = n.serialport; this.serialport = n.serialport;
this.serialbaud = n.serialbaud * 1; this.serialbaud = n.serialbaud * 1;
this.newline = n.newline; this.newline = n.newline;
this.addchar = n.addchar || "false";
} }
RED.nodes.registerType("serial-port",SerialPortNode); RED.nodes.registerType("serial-port",SerialPortNode);
@ -44,27 +45,28 @@ function SerialOutNode(n) {
this.error(err); this.error(err);
return; return;
} }
node.addCh = "";
if (node.serialConfig.addchar == "true") { node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r"); }
node.on("input",function(msg) { node.on("input",function(msg) {
//console.log("{",msg,"}"); var payload = msg.payload;
node.port.write(msg.payload,function(err,res) { if (typeof payload === "object") { payload = JSON.stringify(payload); }
if (err) { if (typeof payload !== "buffer") { payload = new String(payload) + node.addCh; }
node.error(err); node.port.write(payload,function(err,res) {
} if (err) { node.error(err); }
}); });
}); });
} else { } else {
this.error("missing serial config"); this.error("missing serial config");
} }
}
RED.nodes.registerType("serial out",SerialOutNode);
this.on("close", function() {
SerialOutNode.prototype.close = function() {
if (this.serialConfig) { if (this.serialConfig) {
serialPool.close(this.serialConfig.serialport); serialPool.close(this.serialConfig.serialport);
} }
});
} }
RED.nodes.registerType("serial out",SerialOutNode);
function SerialInNode(n) { function SerialInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -81,18 +83,13 @@ function SerialInNode(n) {
} }
this.port.on('data', function(msg) { this.port.on('data', function(msg) {
// console.log("{",msg,"}"); node.send({ "payload": msg });
var m = { "payload": msg };
node.send(m);
}); });
} else { } else {
this.error("missing serial config"); this.error("missing serial config");
} }
}
RED.nodes.registerType("serial in",SerialInNode);
this.on("close", function() {
SerialInNode.prototype.close = function() {
if (this.serialConfig) { if (this.serialConfig) {
try { try {
serialPool.close(this.serialConfig.serialport); serialPool.close(this.serialConfig.serialport);
@ -100,7 +97,10 @@ SerialInNode.prototype.close = function() {
} }
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."); 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);
var serialPool = function() { var serialPool = function() {
var connections = {}; var connections = {};