Comparing version 3.0.0 to 3.1.0
'use strict' | ||
'use strict' | ||
let modbus = require('../..') | ||
let Serialport = require('serialport') | ||
let socket = new Serialport('COM6', { | ||
baudRate: 19200, | ||
Parity: 'none', | ||
stopBits: 1, | ||
dataBits: 8 | ||
}) | ||
var ModbusClient = require('../..') | ||
var client = ModbusClient.createSerialClient('/dev/tty0', 9600) | ||
// set Slave PLC ID | ||
let client = new modbus.client.RTU(socket, 1) | ||
client.on('connect', function () { | ||
socket.on('connect', function () { | ||
client.readDiscreteInputs(0, 12).then(function (resp) { | ||
console.log(resp) | ||
}).fail(function (err) { | ||
socket.close() | ||
}, function (err) { | ||
console.log(err) | ||
}).done(function () { | ||
client.close() | ||
socket.close() | ||
}) | ||
}) | ||
client.on('error', function (err) { | ||
socket.on('error', function (err) { | ||
console.log(err) | ||
}) |
'use strict' | ||
var ModbusClient = require('../..') | ||
var client = ModbusClient.createSerialClient('/dev/tty0', 9600) | ||
let modbus = require('../..') | ||
let Serialport = require('serialport') | ||
let socket = new Serialport('COM6', { | ||
baudRate: 19200, | ||
Parity: 'none', | ||
stopBits: 1, | ||
dataBits: 8 | ||
}) | ||
client.on('connect', function () { | ||
// set Slave PLC ID | ||
let client = new modbus.client.RTU(socket, 1) | ||
socket.on('connect', function () { | ||
client.readInputRegister(0, 12).then(function (resp) { | ||
console.log(resp) | ||
}).fail(function (err) { | ||
socket.close() | ||
}, function (err) { | ||
console.log(err) | ||
}).done(function () { | ||
client.close() | ||
socket.close() | ||
}) | ||
}) | ||
client.on('error', function (err) { | ||
socket.on('error', function (err) { | ||
console.log(err) | ||
}) |
'use strict' | ||
var ModbusClient = require('../..') | ||
var client = ModbusClient.createSerialClient('/dev/tty0', 9600) | ||
let modbus = require('../..') | ||
let Serialport = require('serialport') | ||
let socket = new Serialport('COM6', { | ||
baudRate: 19200, | ||
Parity: 'none', | ||
stopBits: 1, | ||
dataBits: 8 | ||
}) | ||
client.on('connect', function () { | ||
// set Slave PLC ID | ||
let client = new modbus.client.RTU(socket, 1) | ||
socket.on('connect', function () { | ||
client.writeSingleCoil(4, true).then(function (resp) { | ||
console.log(resp) | ||
}).fail(function (err) { | ||
socket.close() | ||
}, function (err) { | ||
console.log(err) | ||
}).done(function () { | ||
client.close() | ||
socket.close() | ||
}) | ||
}) | ||
client.on('error', function (err) { | ||
socket.on('error', function (err) { | ||
console.log(err) | ||
}) |
'use strict' | ||
var ModbusClient = require('../..') | ||
var client = ModbusClient.createSerialClient('/dev/tty0', 9600) | ||
let modbus = require('../..') | ||
let Serialport = require('serialport') | ||
let socket = new Serialport('COM6', { | ||
baudRate: 19200, | ||
Parity: 'none', | ||
stopBits: 1, | ||
dataBits: 8 | ||
}) | ||
client.on('connect', function () { | ||
// set Slave PLC ID | ||
let client = new modbus.client.RTU(socket, 1) | ||
socket.on('connect', function () { | ||
client.writeSingleRegister(5, 123).then(function (resp) { | ||
console.log(resp) | ||
socket.close() | ||
}).fail(function (err) { | ||
console.log(err) | ||
}).done(function () { | ||
client.close() | ||
socket.close() | ||
}) | ||
}) | ||
client.on('error', function (err) { | ||
socket.on('error', function (err) { | ||
console.log(err) | ||
}) |
@@ -8,3 +8,3 @@ 'use strict' | ||
'host': '127.0.0.1', | ||
'port': '8502' | ||
'port': '502' | ||
} | ||
@@ -14,8 +14,10 @@ let client = new modbus.client.TCP(socket) | ||
socket.on('connect', function () { | ||
client.readHoldingRegisters(0, 2) | ||
client.readHoldingRegisters(0, 10) | ||
.then(function (resp) { | ||
console.log(resp) | ||
console.log(resp.response._body.valuesAsArray) | ||
socket.end() | ||
}).catch(function () { | ||
console.error(arguments) | ||
console.error(require('util').inspect(arguments, { | ||
depth: null | ||
})) | ||
socket.end() | ||
@@ -22,0 +24,0 @@ }) |
@@ -6,3 +6,6 @@ 'use strict' | ||
let netServer = new net.Server() | ||
let server = new modbus.server.TCP(netServer) | ||
let holding = Buffer.alloc(10000) | ||
let server = new modbus.server.TCP(netServer, { | ||
holding: holding | ||
}) | ||
@@ -45,8 +48,8 @@ server.on('connection', function (client) { | ||
server.on('writeMultipleRegisters', function (value) { | ||
/* server.on('writeMultipleRegisters', function (value) { | ||
console.log('Write multiple registers - Existing: ', value) | ||
}) | ||
}) */ | ||
server.on('postWriteMultipleRegisters', function (value) { | ||
console.log('Write multiple registers - Complete: ', value) | ||
console.log('Write multiple registers - Complete: ', holding.readUInt16BE(0)) | ||
}) | ||
@@ -73,2 +76,3 @@ | ||
netServer.listen(8502) | ||
console.log(process.argv[2]) | ||
netServer.listen(process.argv[2] || 8502) |
@@ -7,4 +7,4 @@ 'use strict' | ||
let options = { | ||
'host': '127.0.0.1', | ||
'port': '8502' | ||
'host': '192.168.56.101', | ||
'port': '502' | ||
} | ||
@@ -14,3 +14,3 @@ let client = new modbus.client.TCP(socket) | ||
socket.on('connect', function () { | ||
client.writeSingleRegister(2, 102) | ||
client.writeSingleRegister(1002, 333) | ||
.then(function (resp) { | ||
@@ -17,0 +17,0 @@ console.log(resp) |
{ | ||
"name": "jsmodbus", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Implementation for the Serial/TCP Modbus protocol.", | ||
@@ -15,3 +15,4 @@ "author": "Stefan Poeter <stefan.poeter@cloud-automation.de>", | ||
"dependencies": { | ||
"crc": "3.4.0" | ||
"crc": "3.4.0", | ||
"debug": "^3.1.0" | ||
}, | ||
@@ -18,0 +19,0 @@ "devDependencies": { |
'use strict' | ||
let debug = require('debug')('modbus tcp server') | ||
let EventEmitter = require('events') | ||
let ModbusTCPClient = require('./tcp-server-client.js') | ||
let ModbusServer = require('./modbus-server') | ||
let ModbusServerClient = require('./modbus-server-client.js') | ||
let Request = require('./tcp-request.js') | ||
let Response = require('./tcp-response.js') | ||
class ModbusTCPServer extends EventEmitter { | ||
class ModbusTCPServer extends ModbusServer { | ||
constructor (server, options) { | ||
super() | ||
super(options) | ||
this._server = server | ||
this._options = options || {} | ||
this._coils = this._options.coils || Buffer.alloc(1024) | ||
this._discrete = this._options.discrete || Buffer.alloc(1024) | ||
this._holding = this._options.holding || Buffer.alloc(1024) | ||
this._input = this._options.input || Buffer.alloc(1024) | ||
server.on('connection', this._onConnection.bind(this)) | ||
@@ -23,24 +19,8 @@ } | ||
debug('new connection coming in') | ||
let client = new ModbusTCPClient(this, socket) | ||
let client = new ModbusServerClient(this, socket, Request, Response) | ||
this.emit('connection', client) | ||
} | ||
get coils () { | ||
return this._coils | ||
} | ||
get discrete () { | ||
return this._discrete | ||
} | ||
get holding () { | ||
return this._holding | ||
} | ||
get input () { | ||
return this._input | ||
} | ||
} | ||
module.exports = ModbusTCPServer |
@@ -37,2 +37,5 @@ 'use strict' | ||
/** module:jsmodbus.server.RTU */ | ||
let ModbusRTUServer = require('./modbus-rtu-server.js') | ||
module.exports = { | ||
@@ -44,4 +47,5 @@ 'client': { | ||
server: { | ||
'TCP': ModbusTCPServer | ||
'TCP': ModbusTCPServer, | ||
'RTU': ModbusRTUServer | ||
} | ||
} |
@@ -43,4 +43,11 @@ let ModbusRequestBody = require('./request-body.js') | ||
} | ||
/** Returns the byte count of this request for the byte representation. | ||
* @returns {Number} | ||
*/ | ||
get byteCount () { | ||
return 2 | ||
} | ||
} | ||
module.exports = ExceptionRequestBody |
let debug = require('debug')('read-coils-response') | ||
let ModbusResponseBody = require('./response-body.js') | ||
const { bufferToArrayStatus, arrayStatusToBuffer } = require('../buffer-utils.js') | ||
const { | ||
bufferToArrayStatus, | ||
arrayStatusToBuffer | ||
} = require('../buffer-utils.js') | ||
@@ -5,0 +8,0 @@ /** Read Coils Response Body |
let ModbusResponseBody = require('./response-body.js') | ||
let debug = require('debug')('ReadHoldingRegistersResponseBody') | ||
@@ -14,9 +15,10 @@ /** Read Holding Registers ResponseBody (Function Code 0x03) | ||
static fromRequest (requestBody, holdingRegisters) { | ||
let startByte = requestBody.start | ||
let endByte = requestBody.start + (requestBody.count * 2) | ||
let startByte = requestBody.start * 2 | ||
let endByte = (requestBody.start * 2) + (requestBody.count * 2) | ||
let bufferSegment = holdingRegisters.slice(startByte, endByte) | ||
let buf = Buffer.from(bufferSegment) | ||
return new ReadHoldingRegistersResponseBody(buf.length, buf) | ||
/* TODO: check wheather holdingRegisters is big enough for this request */ | ||
return new ReadHoldingRegistersResponseBody(bufferSegment.length, bufferSegment) | ||
} | ||
@@ -49,5 +51,9 @@ | ||
this._values = values | ||
this._bufferLength = 2 | ||
debug('ReadHoldingRegistersResponseBody values', values) | ||
if (values instanceof Array) { | ||
this._valuesAsArray = values | ||
this._bufferLength += values.length * 2 | ||
} | ||
@@ -57,2 +63,3 @@ | ||
this._valuesAsBuffer = values | ||
this._bufferLength += values.length | ||
} | ||
@@ -62,3 +69,3 @@ } | ||
get byteCount () { | ||
return this._values.length + 2 | ||
return this._bufferLength | ||
} | ||
@@ -83,11 +90,20 @@ | ||
createPayload () { | ||
let payload = Buffer.alloc(this.byteCount) | ||
if (this._values instanceof Buffer) { | ||
let payload = Buffer.alloc(2) | ||
payload.writeUInt8(this._fc, 0) | ||
payload.writeUInt8(this._byteCount, 1) | ||
payload = Buffer.concat([payload, this._values]) | ||
return payload | ||
} | ||
payload.writeUInt8(this._fc, 0) | ||
payload.writeUInt8(this.length, 1) | ||
this._values.forEach(function (value, i) { | ||
payload.writeUInt8(value, 2 + i) | ||
}) | ||
if (this._values instanceof Array) { | ||
let payload = Buffer.alloc(this.byteCount) | ||
payload.writeUInt8(this._fc, 0) | ||
payload.writeUInt8(this._byteCount, 1) | ||
this._values.forEach(function (value, i) { | ||
payload.writeUInt8(value, 2 + i) | ||
}) | ||
return payload | ||
return payload | ||
} | ||
} | ||
@@ -94,0 +110,0 @@ } |
@@ -9,12 +9,11 @@ let ModbusResponseBody = require('./response-body.js') | ||
/** Create ReadInputRegistersResponseBody from Request | ||
* @param {ReadInputRegistersRequestBody} request | ||
* @param {Buffer} inputRegisters | ||
* @returns ReadInputRegistersResponseBody | ||
*/ | ||
* @param {ReadInputRegistersRequestBody} request | ||
* @param {Buffer} inputRegisters | ||
* @returns ReadInputRegistersResponseBody | ||
*/ | ||
static fromRequest (requestBody, inputRegisters) { | ||
let startByte = requestBody.start | ||
let endByte = requestBody.start + (requestBody.count * 2) | ||
let startByte = requestBody.start * 2 | ||
let endByte = startByte + (requestBody.count * 2) | ||
let bufferSegment = inputRegisters.slice(startByte, endByte) | ||
let buf = Buffer.from(bufferSegment) | ||
let buf = inputRegisters.slice(startByte, endByte) | ||
@@ -24,6 +23,6 @@ return new ReadInputRegistersResponseBody(buf.length, buf) | ||
/** Create ReadInputRegistersResponseBody from Buffer | ||
* @param {Buffer} buffer | ||
* @returns ReadInputRegistersResponseBody | ||
*/ | ||
/** Create ReadInputRegistersResponseBody from Buffer | ||
* @param {Buffer} buffer | ||
* @returns ReadInputRegistersResponseBody | ||
*/ | ||
static fromBuffer (buffer) { | ||
@@ -50,5 +49,7 @@ let fc = buffer.readUInt8(0) | ||
this._values = values | ||
this._bufferLength = 2 | ||
if (values instanceof Array) { | ||
this._valuesAsArray = values | ||
this._bufferLength += values.length * 2 | ||
} | ||
@@ -58,2 +59,3 @@ | ||
this._valuesAsBuffer = values | ||
this._bufferLength += values.length | ||
} | ||
@@ -63,3 +65,3 @@ } | ||
get byteCount () { | ||
return this._values.length + 2 | ||
return this._bufferLength | ||
} | ||
@@ -66,0 +68,0 @@ |
@@ -0,1 +1,2 @@ | ||
'use strict' | ||
@@ -2,0 +3,0 @@ /** Modbus Response Body |
@@ -0,1 +1,3 @@ | ||
'use strict' | ||
let debug = require('debug')('rtu-client-request-handler') | ||
@@ -46,5 +48,7 @@ let ModbusRTURequest = require('./rtu-request.js') | ||
debug('create crc from response') | ||
let crc = CRC.crc16modbus(response.body.createPayload()) | ||
let buf = Buffer.concat([Buffer.from([response.address]), response.body.createPayload()]) | ||
debug('create crc from response', buf) | ||
let crc = CRC.crc16modbus(buf) | ||
if (response.crc !== crc) { | ||
@@ -51,0 +55,0 @@ debug('CRC does not match', response.crc, '!==', crc) |
@@ -0,9 +1,51 @@ | ||
let debug = require('debug')('rtu-request') | ||
let CRC = require('crc') | ||
let CommonRequestBody = require('./request/request-body.js') | ||
class ModbusRTURequest { | ||
constructor (address, body) { | ||
/** Convert a buffer into a new Modbus RTU Request. Returns null if the buffer | ||
* does not contain enough data. | ||
* @param {Buffer} buffer | ||
* @return {ModbusRTURequest} A new Modbus RTU Request or null. | ||
*/ | ||
static fromBuffer (buffer) { | ||
try { | ||
if (buffer.length < 1 /* address */ + 2 /* CRC */) { | ||
debug('not enough data in the buffer yet') | ||
return null | ||
} | ||
const address = buffer.readUInt8(0) | ||
debug(`rtu header complete, address, ${address}`) | ||
debug('buffer', buffer) | ||
// NOTE: This is potentially more than the body; the body length isn't know at this point... | ||
const body = CommonRequestBody.fromBuffer(buffer.slice(1)) | ||
if (!body) { | ||
return null | ||
} | ||
const payloadLength = 1 /* address */ + body.byteCount | ||
const expectedCrc = CRC.crc16modbus(buffer.slice(0, payloadLength)) | ||
const actualCrc = buffer.readUInt16LE(payloadLength) | ||
const corrupted = (expectedCrc !== actualCrc) | ||
return new ModbusRTURequest(address, body, corrupted) | ||
} catch (e) { | ||
debug('not enough data to create a rtu request', e) | ||
return null | ||
} | ||
} | ||
constructor (address, body, corrupted) { | ||
this._address = address | ||
this._body = body | ||
this._corrupted = corrupted | ||
} | ||
get address () { | ||
return this._address | ||
} | ||
get crc () { | ||
@@ -21,12 +63,15 @@ return this._crc | ||
get corrupted () { | ||
return (this._corrupted === true) | ||
} | ||
createPayload () { | ||
let bodyPayload = this._body.createPayload() | ||
this._crc = CRC.crc16modbus(bodyPayload) | ||
this._crc = CRC.crc16modbus(Buffer.concat([Buffer.from([this._address]), bodyPayload])) | ||
let crBu = Buffer.alloc(2) | ||
crBu.writeUInt16LE(this._crc) | ||
let idBuf = Buffer.from([this._address]) | ||
let payload = Buffer.concat([idBuf, bodyPayload, crBu]) | ||
let payload = Buffer.alloc(1 + bodyPayload.length + 2) | ||
payload.writeUInt8(this._address, 0) // address | ||
bodyPayload.copy(payload, 1) // copy body | ||
payload.writeUInt16BE(this._crc, 1 + bodyPayload.length) // crc | ||
return payload | ||
@@ -33,0 +78,0 @@ } |
let debug = require('debug')('rtu-response') | ||
let CRC = require('crc') | ||
let ResponseFactory = require('./response/response-factory.js') | ||
class ModbusRTUResponse { | ||
/** Create Modbus/RTU Response from a Modbus/RTU Request including | ||
* the modbus function body. | ||
* @param {ModbusRTURequest} request | ||
* @param {ModbusResponseBody} body | ||
* @returns {ModbusRTUResponse} | ||
*/ | ||
static fromRequest (rtuRequest, modbusBody) { | ||
return new ModbusRTUResponse( | ||
rtuRequest.address, | ||
undefined, // CRC is calculated when createPayload () is called | ||
modbusBody) | ||
} | ||
static fromBuffer (buffer) { | ||
@@ -22,3 +36,3 @@ if (buffer.length < 1) { | ||
try { | ||
crc = buffer.readUInt16BE(1 + body.bodyCount) | ||
crc = buffer.readUInt16LE(1 + body.byteCount) | ||
} catch (e) { | ||
@@ -53,4 +67,19 @@ debug('If NoSuchIndexException, it is probably serial and not all data has arrived') | ||
} | ||
createPayload () { | ||
/* Payload is a buffer with: | ||
* Address/Unit ID = 1 Byte | ||
* Body = N Bytes | ||
* CRC = 2 Bytes | ||
*/ | ||
let payload = Buffer.alloc(this.byteCount) | ||
payload.writeUInt8(this._address, 0) | ||
const bodyPayload = this._body.createPayload() | ||
bodyPayload.copy(payload, 1) | ||
this._crc = CRC.crc16modbus(payload.slice(0, this.byteCount - 2 /* CRC bytes */)) | ||
payload.writeUInt16LE(this._crc, this.byteCount - 2) | ||
return payload | ||
} | ||
} | ||
module.exports = ModbusRTUResponse |
@@ -26,3 +26,3 @@ let debug = require('debug')('tcp-request') | ||
let body = CommonRequestBody.fromBuffer(buffer.slice(7, 7 + length - 1)) | ||
let body = CommonRequestBody.fromBuffer(buffer.slice(7, 6 + length)) | ||
@@ -85,2 +85,8 @@ if (!body) { | ||
/** For interface compatibility with ModbusRTURequest where data | ||
* integrity checking happens as part of the Modbus protocol **/ | ||
get corrupted () { | ||
return false | ||
} | ||
/** Creates a buffer object representing the modbus tcp request. | ||
@@ -104,3 +110,3 @@ * @returns {Buffer} */ | ||
get byteCount () { | ||
return this._length + 6 + 1 | ||
return this._length + 6 | ||
} | ||
@@ -107,0 +113,0 @@ } |
@@ -7,4 +7,4 @@ 'use strict' | ||
let EventEmitter = require('events') | ||
let ReadCoilsRequest = require('../src/request/read-coils.js') | ||
let ReadCoilsResponse = require('../src/response/read-coils.js') | ||
let ReadCoilsRequest = require('../src/request/read-holding-registers.js') | ||
let ReadHoldingRegistersResponseBody = require('../src/response/read-holding-registers.js') | ||
let ModbusRTUResponse = require('../src/rtu-response.js') | ||
@@ -20,3 +20,3 @@ let ExceptionResponse = require('../src/response/exception.js') | ||
socket = new EventEmitter() | ||
socket.write = function () { } | ||
socket.write = function () {} | ||
@@ -46,7 +46,7 @@ socketMock = sinon.mock(socket) | ||
it('should register an rtu request and handle a response', function (done) { | ||
let ReadCoilsRequest = require('../src/request/read-coils.js') | ||
let handler = new ModbusRTUClientRequestHandler(socket, 4) | ||
let request = new ReadCoilsRequest(0x0000, 0x0008) | ||
let response = new ReadCoilsResponse([0, 1, 0, 1, 0, 1, 0, 1], 1) | ||
let rtuResponse = new ModbusRTUResponse(4, 61345, response) | ||
let ReadHoldingRegistersRequestBody = require('../src/request/read-holding-registers.js') | ||
let handler = new ModbusRTUClientRequestHandler(socket, 1) | ||
let request = new ReadHoldingRegistersRequestBody(0, 1) | ||
let response = new ReadHoldingRegistersResponseBody(1, Buffer.from([0x00, 0x32])) | ||
let rtuResponse = new ModbusRTUResponse(1, 0x91C9, response) | ||
@@ -53,0 +53,0 @@ socket.emit('open') |
@@ -23,3 +23,3 @@ 'use strict' | ||
0x00, | ||
0x00, 0x00 // crc | ||
0xCD, 0xAB // crc | ||
]) | ||
@@ -34,3 +34,3 @@ | ||
assert.equal(1, response.body.fc) | ||
assert.equal(257, response.crc) | ||
assert.equal(0xABCD, response.crc) | ||
assert.equal(7, response.byteCount) | ||
@@ -37,0 +37,0 @@ |
Sorry, the diff of this file is not supported yet
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
225027
100
6543
14
2
+ Addeddebug@^3.1.0
+ Addeddebug@3.2.7(transitive)
+ Addedms@2.1.3(transitive)