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:
parent
d2ce6af486
commit
67e16adfd0
@ -66,6 +66,7 @@
|
||||
<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>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">
|
||||
@ -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"> </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;
|
||||
|
@ -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,27 +45,28 @@ 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");
|
||||
}
|
||||
}
|
||||
RED.nodes.registerType("serial out",SerialOutNode);
|
||||
|
||||
|
||||
SerialOutNode.prototype.close = function() {
|
||||
this.on("close", function() {
|
||||
if (this.serialConfig) {
|
||||
serialPool.close(this.serialConfig.serialport);
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("serial out",SerialOutNode);
|
||||
|
||||
|
||||
function SerialInNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
@ -81,18 +83,13 @@ 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");
|
||||
}
|
||||
}
|
||||
RED.nodes.registerType("serial in",SerialInNode);
|
||||
|
||||
|
||||
SerialInNode.prototype.close = function() {
|
||||
this.on("close", function() {
|
||||
if (this.serialConfig) {
|
||||
try {
|
||||
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.");
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("serial in",SerialInNode);
|
||||
|
||||
|
||||
var serialPool = function() {
|
||||
var connections = {};
|
||||
|
Loading…
Reference in New Issue
Block a user