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
@ -26,15 +26,15 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="serial in">
|
<script type="text/x-red" data-help-name="serial in">
|
||||||
<p>Reads data from a local serial port.</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>
|
<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>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('serial in',{
|
RED.nodes.registerType('serial in',{
|
||||||
category: 'input',
|
category: 'input',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: {name:""},
|
name: {name:""},
|
||||||
serial: {type:"serial-port",required:true}
|
serial: {type:"serial-port",required:true}
|
||||||
},
|
},
|
||||||
color:"BurlyWood",
|
color:"BurlyWood",
|
||||||
@ -64,15 +64,16 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<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">
|
||||||
RED.nodes.registerType('serial out',{
|
RED.nodes.registerType('serial out',{
|
||||||
category: 'output',
|
category: 'output',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: {name:""},
|
name: {name:""},
|
||||||
serial: {type:"serial-port",required:true}
|
serial: {type:"serial-port",required:true}
|
||||||
},
|
},
|
||||||
color:"BurlyWood",
|
color:"BurlyWood",
|
||||||
@ -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"> </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;
|
||||||
|
@ -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,28 +45,29 @@ 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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.on("close", function() {
|
||||||
|
if (this.serialConfig) {
|
||||||
|
serialPool.close(this.serialConfig.serialport);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("serial out",SerialOutNode);
|
RED.nodes.registerType("serial out",SerialOutNode);
|
||||||
|
|
||||||
|
|
||||||
SerialOutNode.prototype.close = function() {
|
|
||||||
if (this.serialConfig) {
|
|
||||||
serialPool.close(this.serialConfig.serialport);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function SerialInNode(n) {
|
function SerialInNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.serial = n.serial;
|
this.serial = n.serial;
|
||||||
@ -81,27 +83,25 @@ 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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
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 serialPool = function() {
|
||||||
var connections = {};
|
var connections = {};
|
||||||
return {
|
return {
|
||||||
@ -123,29 +123,29 @@ var serialPool = function() {
|
|||||||
try {
|
try {
|
||||||
if (newline == "") {
|
if (newline == "") {
|
||||||
obj.serial = new serialp.SerialPort(port,{
|
obj.serial = new serialp.SerialPort(port,{
|
||||||
baudrate: baud,
|
baudrate: baud,
|
||||||
parser: serialp.parsers.raw
|
parser: serialp.parsers.raw
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
obj.serial = new serialp.SerialPort(port,{
|
obj.serial = new serialp.SerialPort(port,{
|
||||||
baudrate: baud,
|
baudrate: baud,
|
||||||
parser: serialp.parsers.readline(newline)
|
parser: serialp.parsers.readline(newline)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
obj.serial.on('error', function(err) {
|
obj.serial.on('error', function(err) {
|
||||||
util.log("[serial] serial port "+port+" error "+err);
|
util.log("[serial] serial port "+port+" error "+err);
|
||||||
obj.tout = setTimeout(function() {
|
obj.tout = setTimeout(function() {
|
||||||
setupSerial();
|
setupSerial();
|
||||||
},settings.serialReconnectTime);
|
},settings.serialReconnectTime);
|
||||||
});
|
});
|
||||||
obj.serial.on('close', function() {
|
obj.serial.on('close', function() {
|
||||||
if (!obj._closing) {
|
if (!obj._closing) {
|
||||||
util.log("[serial] serial port "+port+" closed unexpectedly");
|
util.log("[serial] serial port "+port+" closed unexpectedly");
|
||||||
obj.tout = setTimeout(function() {
|
obj.tout = setTimeout(function() {
|
||||||
setupSerial();
|
setupSerial();
|
||||||
},settings.serialReconnectTime);
|
},settings.serialReconnectTime);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
obj.serial.on('open',function() {
|
obj.serial.on('open',function() {
|
||||||
util.log("[serial] serial port "+port+" opened at "+baud+" baud");
|
util.log("[serial] serial port "+port+" opened at "+baud+" baud");
|
||||||
@ -177,7 +177,7 @@ var serialPool = function() {
|
|||||||
connections[port]._closing = true;
|
connections[port]._closing = true;
|
||||||
try {
|
try {
|
||||||
connections[port].close(function() {
|
connections[port].close(function() {
|
||||||
util.log("[serial] serial port closed");
|
util.log("[serial] serial port closed");
|
||||||
});
|
});
|
||||||
} catch(err) { };
|
} catch(err) { };
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user