modbus-serial
Advanced tools
Comparing version 3.0.2 to 3.1.2
@@ -7,5 +7,5 @@ // create an empty modbus client | ||
// open connection to a telnet server | ||
client.connectTelnet("127.0.0.1", run); | ||
client.connectTelnet("127.0.0.1"); | ||
client.setID(1); | ||
setInterval(run, 1000); | ||
@@ -17,4 +17,4 @@ | ||
client.readInputRegisters(0, 10) | ||
.then(console.log) | ||
.catch(console.log); | ||
.then(console.log); | ||
} | ||
@@ -12,2 +12,3 @@ // create an empty modbus client | ||
// get msb and lsb of 32bit timestamp | ||
var timestamp = Math.floor(Date.now() / 1000); | ||
@@ -29,8 +30,15 @@ var msb = timestamp >> 16; | ||
.readHoldingRegisters(0, 2) | ||
.then( function(data) { | ||
.then(function(data) { | ||
// use the data buffer to build one 32 bit int | ||
// from two 16 bit ints | ||
var timestamp = data.buffer.readUInt32BE(); | ||
console.log('Read: ', timestamp); | ||
} ); | ||
}) | ||
.then(exit); | ||
} | ||
function exit() { | ||
client._port.close(); | ||
process.exit(); | ||
} |
@@ -8,7 +8,4 @@ // Create serial port | ||
var ModbusRTU = require("../index"); | ||
var modbusRTU = new ModbusRTU(serialPort); | ||
var client = new ModbusRTU(serialPort); | ||
// Open modbus communication. | ||
modbusRTU.open(); | ||
/* Write coil: | ||
@@ -19,6 +16,10 @@ * 1 - The Slave Address. | ||
*/ | ||
setTimeout(function() { | ||
modbusRTU.writeFC5(1, 11, true); | ||
}, 500); | ||
function stage1() { | ||
client.writeFC5(1, 11, true); | ||
}; | ||
function stage2() { | ||
client.writeFC5(1, 12, true); | ||
}; | ||
/* read discreet inputs: | ||
@@ -29,11 +30,24 @@ * 1 - The Slave Address. | ||
*/ | ||
setTimeout(function() { | ||
modbusRTU.writeFC2(1, 10, 8, function(err, data) { | ||
console.log(data); | ||
function stage3() { | ||
client.writeFC1(1, 10, 8, function(err, d) { | ||
d.data.forEach(function(e, i) { | ||
console.log(i + 10, e); | ||
}); | ||
}); | ||
}, 1000); | ||
}; | ||
// Close communication. | ||
setTimeout(function() { | ||
serialPort.close(); | ||
}, 2000); | ||
/* exit process | ||
*/ | ||
function exit() { | ||
client._port.close(); | ||
process.exit(); | ||
} | ||
// Open modbus communication. | ||
client.open(); | ||
// call the program stages using timeouts | ||
setTimeout(stage1, 250); | ||
setTimeout(stage2, 500); | ||
setTimeout(stage3, 1250); | ||
setTimeout(exit, 1500); |
@@ -9,8 +9,9 @@ // create an empty modbus client | ||
// use callback, if function get a callback it | ||
// will not return a promise | ||
// use callback | ||
function stage1() { | ||
// same as using client.writeFC5 | ||
client.writeCoil(13, false, function(err, d) { | ||
console.log("writeCoil: ", d.state); | ||
// set client id | ||
client.setID(1); | ||
client.writeCoil(13, true, function(err, d) { | ||
console.log("writeCoil 13: ", d.state); | ||
stage2(); | ||
@@ -20,11 +21,11 @@ }); | ||
// use promise 1 | ||
// use promise | ||
function stage2() { | ||
client.writeCoil(11, true) | ||
.then(function(d) { | ||
console.log("writeCoil: ", d.state);}) | ||
console.log("writeCoil 11: ", d.state);}) | ||
.then(stage3); | ||
} | ||
// use promise 2 | ||
// use promise | ||
function stage3() { | ||
@@ -34,8 +35,10 @@ client.readDiscreteInputs(10, 8) | ||
console.log("readDiscreteInputs: ", d.data);}) | ||
.then(end); | ||
.then(exit); | ||
} | ||
function end() { | ||
serialPort.close(); | ||
// exit process | ||
function exit() { | ||
client._port.close(); | ||
process.exit(); | ||
} | ||
@@ -10,7 +10,10 @@ // create an empty modbus client | ||
function write() { | ||
var data = [0 , 0xffff]; | ||
client.setID(1); | ||
console.log("Send: ", data); | ||
// write the values 0, 0xffff to registers starting at address 5 | ||
// on device number 1. | ||
client.writeRegisters(5, [0 , 0xffff]) | ||
client.writeRegisters(5, data) | ||
.then(read); | ||
@@ -23,4 +26,11 @@ } | ||
client.readHoldingRegisters(5, 2) | ||
.then(console.log); | ||
.then(function(d) { | ||
console.log("Recive:", d); }) | ||
.then(exit); | ||
} | ||
function exit() { | ||
client._port.close(); | ||
process.exit(); | ||
} | ||
{ | ||
"name": "modbus-serial", | ||
"version": "3.0.2", | ||
"version": "3.1.2", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -67,2 +67,5 @@ 'use strict'; | ||
this._client.on('message', function(data) { | ||
// check expected length | ||
if (modbus.length < 6) return; | ||
// check message length | ||
@@ -69,0 +72,0 @@ if (data.length < (116 + modbus.length)) return; |
@@ -50,2 +50,5 @@ 'use strict'; | ||
// check data length | ||
if (data.length < 6) return; | ||
// cut 6 bytes of mbap, copy pdu and add crc | ||
@@ -52,0 +55,0 @@ buffer = new Buffer(data.length - 6 + 2); |
@@ -82,2 +82,5 @@ 'use strict'; | ||
// check data length | ||
if (bufferLength.length < 6 || length < 6) return; | ||
// loop and check length-sized buffer chunks | ||
@@ -84,0 +87,0 @@ for (var i = 0; i < (bufferLength - length + 1); i++) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
69140
1484
3