modbus-serial
Advanced tools
Comparing version 7.7.4 to 7.8.0-no-serial-port
@@ -203,2 +203,29 @@ "use strict"; | ||
/** | ||
* Connect to a communication port, using modbus-udp. | ||
* | ||
* @param {string} ip the ip of the UDP Port - required. | ||
* @param {Object} options - the serial port options - optional. | ||
* @param {Function} next the function to call next. | ||
*/ | ||
cl.connectUDP = function(ip, options, next) { | ||
// check if we have options | ||
if (typeof next === "undefined" && typeof options === "function") { | ||
next = options; | ||
options = {}; | ||
} | ||
// check if we have options | ||
if (typeof options === "undefined") { | ||
options = {}; | ||
} | ||
// create the UdpPort | ||
var UdpPort = require("../ports/udpport"); | ||
this._port = new UdpPort(ip, options); | ||
// open and call next | ||
return open(this, next); | ||
}; | ||
/** | ||
* Connect to a communication port, using Bufferd Serial port. | ||
@@ -205,0 +232,0 @@ * |
@@ -84,2 +84,3 @@ "use strict"; | ||
cl.writeRegisters = _convert(cl.writeFC16); | ||
cl.readDeviceIdentification = _convert(cl.writeFC43); | ||
}; | ||
@@ -86,0 +87,0 @@ |
84
index.js
@@ -161,3 +161,43 @@ "use strict"; | ||
/** | ||
* Parse the data for a Modbus - | ||
* Read Device Identification (FC=43) | ||
* | ||
* @param {Buffer} data the data buffer to parse. | ||
* @param {Modbus} modbus the client in case we need to read more device information | ||
* @param {Function} next the function to call next. | ||
*/ | ||
function _readFC43(data, modbus, next) { | ||
var address = parseInt(data.readUInt8(0)); | ||
var readDeviceIdCode = parseInt(data.readUInt8(3)); | ||
var conformityLevel = parseInt(data.readUInt8(4)); | ||
var moreFollows = parseInt(data.readUInt8(5)); | ||
var nextObjectId = parseInt(data.readUInt8(6)); | ||
var numOfObjects = parseInt(data.readUInt8(7)); | ||
var startAt = 8; | ||
var result = {}; | ||
for (var i = 0; i < numOfObjects; i++) { | ||
var objectId = parseInt(data.readUInt8(startAt)); | ||
var objectLength = parseInt(data.readUInt8(startAt + 1)); | ||
const startOfData = startAt + 2; | ||
result[objectId] = data.toString("ascii", startOfData, startOfData + objectLength); | ||
startAt = startOfData + objectLength; | ||
} | ||
// is it saying to follow and did you previously get data | ||
// if you did not previously get data go ahead and halt to prevent an infinite loop | ||
if (moreFollows && numOfObjects) { | ||
const cb = function(err, data) { | ||
data.data = Object.assign(data.data, result); | ||
return next(err, data); | ||
}; | ||
modbus.writeFC43(address, readDeviceIdCode, nextObjectId, cb); | ||
} else if (next) { | ||
next(null, { data: result, conformityLevel }); | ||
} | ||
} | ||
/** | ||
* Wrapper method for writing to a port with timeout. <code><b>[this]</b></code> has the context of ModbusRTU | ||
@@ -270,3 +310,3 @@ * @param {Buffer} buffer The data to send | ||
*/ | ||
if (data.length < 5) { | ||
if (!transaction.lengthUnknown && data.length < 5) { | ||
error = "Data length error, expected " + | ||
@@ -311,3 +351,3 @@ transaction.nextLength + " got " + data.length; | ||
*/ | ||
if (data.length !== transaction.nextLength) { | ||
if (!transaction.lengthUnknown && data.length !== transaction.nextLength) { | ||
error = "Data length error, expected " + | ||
@@ -362,2 +402,5 @@ transaction.nextLength + " got " + data.length; | ||
break; | ||
case 43: | ||
// read device identification | ||
_readFC43(data, modbus, transaction.next); | ||
} | ||
@@ -767,2 +810,39 @@ }); | ||
/** | ||
* Write a Modbus "Read Device Identification" (FC=43) to serial port. | ||
* | ||
* @param {number} address the slave unit address. | ||
* @param {number} deviceIdCode the read device access code. | ||
* @param {number} objectId the array of values to write to registers. | ||
* @param {Function} next the function to call next. | ||
*/ | ||
ModbusRTU.prototype.writeFC43 = function(address, deviceIdCode, objectId, next) { | ||
// check port is actually open before attempting write | ||
if (this.isOpen !== true) { | ||
if (next) next(new PortNotOpenError()); | ||
return; | ||
} | ||
var code = 0x2B; // 43 | ||
// set state variables | ||
this._transactions[this._port._transactionIdWrite] = { | ||
nextAddress: address, | ||
nextCode: code, | ||
lengthUnknown: true, | ||
next: next | ||
}; | ||
var codeLength = 5; | ||
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes | ||
buf.writeUInt8(address, 0); | ||
buf.writeUInt8(code, 1); | ||
buf.writeUInt8(0x0E, 2); // 16 MEI Type | ||
buf.writeUInt8(deviceIdCode, 3); | ||
buf.writeUInt8(objectId, 4); | ||
// add crc bytes to buffer | ||
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength); | ||
// write buffer to serial port | ||
_writeBufferToPort.call(this, buf, this._port._transactionIdWrite); | ||
}; | ||
// add the connection shorthand API | ||
@@ -769,0 +849,0 @@ require("./apis/connection")(ModbusRTU); |
@@ -22,2 +22,4 @@ export class ModbusRTU { | ||
connectTCP(ip: string, options: TcpPortOptions): Promise<void>; | ||
connectUDP(ip: string, options: UdpPortOptions, next: Function): void; | ||
connectUDP(ip: string, options: UdpPortOptions): Promise<void>; | ||
connectTcpRTUBuffered(ip: string, options: TcpRTUPortOptions, next: Function): void; | ||
@@ -107,2 +109,8 @@ connectTcpRTUBuffered(ip: string, options: TcpRTUPortOptions): Promise<void>; | ||
export interface UdpPortOptions { | ||
port?: number; | ||
localAddress?: string; | ||
family?: number; | ||
} | ||
export interface TcpRTUPortOptions { | ||
@@ -109,0 +117,0 @@ port?: number; |
{ | ||
"name": "modbus-serial", | ||
"version": "7.7.4", | ||
"version": "7.8.0-no-serial-port", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.", | ||
@@ -40,5 +40,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"debug": "^4.1.1", | ||
"serialport": "^8.0.6" | ||
"debug": "^4.1.1" | ||
} | ||
} |
@@ -12,3 +12,3 @@ "use strict"; | ||
var MIN_DATA_LENGTH = 8; | ||
var MIN_DATA_LENGTH = 7; | ||
@@ -92,3 +92,2 @@ /** | ||
var crc = data[data.length - 2] + data[data.length - 1] * 0x100; | ||
// if crc is bad, ignore message | ||
@@ -245,2 +244,16 @@ if (crc !== crc16(data.slice(0, -2))) { | ||
if (functionCode === 43) { | ||
const productCode = "MyProductCode1234"; | ||
buffer = Buffer.alloc(12 + productCode.length); | ||
buffer.writeUInt8(16, 2); // MEI Type | ||
buffer.writeUInt8(data.readInt8(3), 3); // read device ID code | ||
buffer.writeUInt8(0x01, 4); // conformity level | ||
buffer.writeUInt8(0, 5); // number of follows left | ||
buffer.writeUInt8(0, 6); // next object ID | ||
buffer.writeUInt8(1, 7); // number of objects | ||
buffer.writeUInt8(data.readInt8(4), 8); | ||
buffer.writeUInt8(productCode.length, 9); | ||
buffer.write(productCode, 10, productCode.length, "ascii"); | ||
} | ||
// send data back | ||
@@ -247,0 +260,0 @@ if (buffer) { |
@@ -68,3 +68,3 @@ # modbus-serial | ||
* FC16 "Preset Multiple Registers" | ||
* FC43/14 "Read Device Identification" (server only) | ||
* FC43/14 "Read Device Identification" | ||
@@ -210,2 +210,21 @@ ###### Client Serial: | ||
---- | ||
###### Logger UDP | ||
``` javascript | ||
// create an empty modbus client | ||
var ModbusRTU = require("modbus-serial"); | ||
var client = new ModbusRTU(); | ||
// open connection to a udp line | ||
client.connectUDP("127.0.0.1", { port: 8502 }); | ||
client.setID(1); | ||
// read the values of 10 registers starting at address 0 | ||
// on device number 1. and log the values to the console. | ||
setInterval(function() { | ||
client.readHoldingRegisters(0, 10, function(err, data) { | ||
console.log(data.data); | ||
}); | ||
}, 1000); | ||
``` | ||
---- | ||
###### ModbusTCP Server | ||
@@ -212,0 +231,0 @@ ``` javascript |
@@ -18,2 +18,4 @@ "use strict"; | ||
var value = 1; | ||
var deviceIdCode = 1; | ||
var objectId = 2; | ||
@@ -28,2 +30,3 @@ modbusRTU.open(); | ||
expect(modbusRTU.writeRegisters(address, [value])).to.be.instanceOf(Promise); | ||
expect(modbusRTU.readDeviceIdentification(deviceIdCode, objectId)).to.be.instanceOf(Promise); | ||
}); | ||
@@ -30,0 +33,0 @@ }); |
@@ -36,2 +36,7 @@ "use strict"; | ||
Socket.prototype.bind = function() { | ||
this.emit("listening"); | ||
}; | ||
// Obsolete? It doesn't reflect the dgram interface | ||
Socket.prototype.listen = function() { | ||
@@ -42,3 +47,3 @@ this.emit("listening"); | ||
Socket.prototype.receive = function(buffer) { | ||
this.emit("message", buffer); | ||
this.emit("message", buffer, { address: this._address, port: this._port, size: this._data.length }); | ||
}; | ||
@@ -45,0 +50,0 @@ |
@@ -338,2 +338,13 @@ "use strict"; | ||
describe("#writeFC43() - read device identification", function() { | ||
it("should return a device identificationm without errors", function(done) { | ||
modbusRTU.writeFC43(1, 4, 1, function(err, data) { | ||
expect(err).to.be.a("null"); | ||
expect(data.conformityLevel).to.equal(1); | ||
expect(data.data["1"]).to.equal("MyProductCode1234"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe("Timeout", function() { | ||
@@ -340,0 +351,0 @@ var timeout = 1000; |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
276411
1
60
7281
295
1
9
- Removedserialport@^8.0.6
- Removed@serialport/binding-abstract@8.0.6(transitive)
- Removed@serialport/binding-mock@8.0.6(transitive)
- Removed@serialport/bindings@8.0.8(transitive)
- Removed@serialport/parser-byte-length@8.0.6(transitive)
- Removed@serialport/parser-cctalk@8.0.6(transitive)
- Removed@serialport/parser-delimiter@8.0.6(transitive)
- Removed@serialport/parser-readline@8.0.6(transitive)
- Removed@serialport/parser-ready@8.0.6(transitive)
- Removed@serialport/parser-regex@8.0.6(transitive)
- Removed@serialport/stream@8.0.6(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedaproba@1.2.0(transitive)
- Removedare-we-there-yet@1.1.7(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbindings@1.5.0(transitive)
- Removedbl@4.1.0(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedchownr@1.1.4(transitive)
- Removedcode-point-at@1.1.0(transitive)
- Removedconsole-control-strings@1.1.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddecompress-response@4.2.1(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removeddelegates@1.0.0(transitive)
- Removeddetect-libc@1.0.3(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexpand-template@2.0.3(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removedfs-constants@1.0.0(transitive)
- Removedgauge@2.7.4(transitive)
- Removedgithub-from-package@0.0.0(transitive)
- Removedhas-unicode@2.0.1(transitive)
- Removedieee754@1.2.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedini@1.3.8(transitive)
- Removedis-fullwidth-code-point@1.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedmimic-response@2.1.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp-classic@0.5.3(transitive)
- Removednan@2.22.0(transitive)
- Removednapi-build-utils@1.0.2(transitive)
- Removednode-abi@2.30.1(transitive)
- Removednoop-logger@0.1.1(transitive)
- Removednpmlog@4.1.2(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedprebuild-install@5.3.6(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedpump@3.0.2(transitive)
- Removedrc@1.2.8(transitive)
- Removedreadable-stream@2.3.83.6.2(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsemver@5.7.2(transitive)
- Removedserialport@8.0.8(transitive)
- Removedset-blocking@2.0.0(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedsimple-concat@1.0.1(transitive)
- Removedsimple-get@3.1.1(transitive)
- Removedstring-width@1.0.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedtar-fs@2.1.1(transitive)
- Removedtar-stream@2.2.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwhich-pm-runs@1.1.0(transitive)
- Removedwide-align@1.1.5(transitive)
- Removedwrappy@1.0.2(transitive)