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

Merge pull request #207 from fvdpol/master

Add configuration of serial protocol settings (25-serial node)
This commit is contained in:
Dave Conway-Jones 2014-04-30 13:53:36 +01:00
commit f60430305e
2 changed files with 96 additions and 9 deletions

View File

@ -95,7 +95,8 @@
<script type="text/x-red" data-template-name="serial-port">
<div class="form-row">
<label for="node-config-input-serialport"><i class="icon-bullhorn"></i> Serial Port</label>
<input type="text" id="node-config-input-serialport" placeholder="/dev/ttyUSB0" style="width:50%;">
<select type="text" id="node-config-input-serialport" style="width:75%;">
</select>
</div>
<div class="form-row">
<label for="node-config-input-serialbaud"><i class="icon-wrench"></i> Baud Rate</label>
@ -119,6 +120,32 @@
<option value="50">50</option>
</select>
</div>
<div class="form-row">
<label for="node-config-input-databits"><i class="icon-wrench"></i> Data Bits</label>
<select type="text" id="node-config-input-databits" style="width: 100px;">
<option value="8">8</option>
<option value="7">7</option>
<option value="6">6</option>
<option value="5">5</option>
</select>
</div>
<div class="form-row">
<label for="node-config-input-parity"><i class="icon-wrench"></i> Parity</label>
<select type="text" id="node-config-input-parity" style="width: 100px;">
<option value="none">None</option>
<option value="even">Even</option>
<option value="mark">Mark</option>
<option value="odd">Odd</option>
<option value="space">Space</option>
</select>
</div>
<div class="form-row">
<label for="node-config-input-stopbits"><i class="icon-wrench"></i> Stop Bits</label>
<select type="text" id="node-config-input-stopbits" style="width: 100px;">
<option value="2">2</option>
<option value="1">1</option>
</select>
</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" style="width: 50px;">
@ -140,12 +167,36 @@
//name: {value:""},
serialport: {value:"",required:true},
serialbaud: {value:57600,required:true},
databits: {value:8,required:true},
parity: {value:"none",required:true},
stopbits: {value:1,required:true},
newline: {value:"\\n"},
addchar: {value:false}
},
label: function() {
//return this.name||this.serialport;
return this.serialport+":"+this.serialbaud;
if (typeof this.serialbaud === 'undefined') {
this.serialbaud = 57600;
}
if (typeof this.databits === 'undefined') {
this.databits = 8;
}
if (typeof this.parity === 'undefined') {
this.parity = 'none';
}
if (typeof this.stopbits == 'undefined') {
this.stopbits = 1;
}
return this.serialport+":"+this.serialbaud+"-"+this.databits+this.parity.charAt(0).toUpperCase()+this.stopbits;
},
oneditprepare: function() {
var p = this.serialport;
var s = $("#node-config-input-serialport");
$.getJSON('serialports',function(data) {
$.each(data, function(i, port){
s.append($('<option>').text(port.comName).attr('value', port.comName));
});
s.val(p);
});
}
});
</script>

View File

@ -25,9 +25,29 @@ var serialp = require("serialport");
function SerialPortNode(n) {
RED.nodes.createNode(this,n);
this.serialport = n.serialport;
this.serialbaud = n.serialbaud * 1;
this.newline = n.newline;
this.addchar = n.addchar || "false";
if (typeof n.serialbaud === 'undefined') {
this.serialbaud = 57600;
} else {
this.serialbaud = parseInt(n.serialbaud);
}
if (typeof n.databits === 'undefined') {
this.databits = 8;
} else {
this.databits = parseInt(n.databits);
}
if (typeof n.parity === 'undefined') {
this.parity = 'none';
} else {
this.parity = n.parity;
}
if (typeof n.stopbits == 'undefined') {
this.stopbits = 1;
} else {
this.stopbits = parseInt(n.stopbits);
}
}
RED.nodes.registerType("serial-port",SerialPortNode);
@ -39,7 +59,12 @@ function SerialOutNode(n) {
if (this.serialConfig) {
var node = this;
node.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
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") {
node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r");
@ -82,7 +107,12 @@ function SerialInNode(n) {
if (this.serialConfig) {
var node = this;
this.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
node.port = serialPool.get(this.serialConfig.serialport,
this.serialConfig.serialbaud,
this.serialConfig.databits,
this.serialConfig.parity,
this.serialConfig.stopbits,
this.serialConfig.newline);
this.port.on('data', function(msg) {
node.send({ "payload": msg });
});
@ -105,7 +135,7 @@ RED.nodes.registerType("serial in",SerialInNode);
var serialPool = function() {
var connections = {};
return {
get:function(port,baud,newline,callback) {
get:function(port,baud,databits,parity,stopbits,newline,callback) {
var id = port;
if (!connections[id]) {
connections[id] = function() {
@ -123,12 +153,18 @@ var serialPool = function() {
if (newline == "") {
obj.serial = new serialp.SerialPort(port,{
baudrate: baud,
databits: databits,
parity: parity,
stopbits: stopbits,
parser: serialp.parsers.raw
},true, function(err, results) { if (err) obj.serial.emit('error',err); });
}
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); });
}
@ -147,7 +183,7 @@ var serialPool = 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 "+databits+""+parity.charAt(0).toUpperCase()+stopbits);
if (obj.tout) { clearTimeout(obj.tout); }
//obj.serial.flush();
obj._emitter.emit('ready');