Comparing version 3.1.3 to 3.1.4
{ | ||
"name": "jsmodbus", | ||
"version": "3.1.3", | ||
"version": "3.1.4", | ||
"description": "Implementation for the Serial/TCP Modbus protocol.", | ||
@@ -5,0 +5,0 @@ "author": "Stefan Poeter <stefan.poeter@cloud-automation.de>", |
@@ -99,7 +99,7 @@ const ModbusResponseBody = require('./response-body.js') | ||
if (this._values instanceof Array) { | ||
const payload = Buffer.alloc(this.byteCount) | ||
const payload = Buffer.alloc(this._byteCount + 2) | ||
payload.writeUInt8(this._fc, 0) | ||
payload.writeUInt8(this._byteCount, 1) | ||
this._values.forEach(function (value, i) { | ||
payload.writeUInt8(value, 2 + i) | ||
payload.writeUInt16BE(Math.max(0, Math.min(0xFFFF, value)), 2 * i + 2) | ||
}) | ||
@@ -106,0 +106,0 @@ |
@@ -85,11 +85,20 @@ const ModbusResponseBody = require('./response-body.js') | ||
createPayload () { | ||
const 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) { | ||
const payload = Buffer.alloc(this._byteCount + 2) | ||
payload.writeUInt8(this._fc, 0) | ||
payload.writeUInt8(this._byteCount, 1) | ||
this._values.forEach(function (value, i) { | ||
payload.writeUInt16BE(Math.max(0, Math.min(0xFFFF, value)), 2 + 2 * i) | ||
}) | ||
return payload | ||
return payload | ||
} | ||
} | ||
@@ -96,0 +105,0 @@ } |
@@ -7,6 +7,22 @@ 'use strict' | ||
const ReadHoldingRegistersRequest = require('../src/request/read-holding-registers.js') | ||
const ReadHoldingRegistersResponse = require('../src/response/read-holding-registers.js') | ||
describe('ReadHoldingRegisters Tests.', function () { | ||
describe('ReadHoldingRegisters Response', function () { | ||
it('should create a response from a buffer', function () { | ||
const request = new ReadHoldingRegistersRequest(0, 1) | ||
const holdingRegisters = Buffer.from([0x01, 0x00, 0x02, 0x00, 0xFF, 0xFF]) | ||
const response = ReadHoldingRegistersResponse.fromRequest(request, holdingRegisters) | ||
const respPayload = response.createPayload() | ||
const expected = Buffer.from([0x03, 0x02, 0x01, 0x00]) | ||
assert.deepEqual(respPayload, expected) | ||
}) | ||
it('should create a response with constructor from array', function () { | ||
const response = new ReadHoldingRegistersResponse(6, [0x01, 0x02, 0xFFFE]) | ||
const respPayload = response.createPayload() | ||
const expected = Buffer.from([0x03, 0x06, 0x00, 0x01, 0x00, 0x02, 0xFF, 0xFE]) | ||
assert.deepEqual(respPayload, expected) | ||
}) | ||
}) | ||
@@ -13,0 +29,0 @@ describe('ReadHoldingRegisters Request', function () { |
@@ -7,6 +7,22 @@ 'use strict' | ||
const ReadInputRegistersRequest = require('../src/request/read-input-registers.js') | ||
const ReadInputRegistersResponse = require('../src/response/read-input-registers.js') | ||
describe('ReadInputRegisters Tests.', function () { | ||
describe('ReadInputRegisters Response', function () { | ||
it('should create a response from a buffer', function () { | ||
const request = new ReadInputRegistersRequest(0, 1) | ||
const inputRegisters = Buffer.from([0x01, 0x00, 0x02, 0x00, 0xFF, 0xFF]) | ||
const response = ReadInputRegistersResponse.fromRequest(request, inputRegisters) | ||
const respPayload = response.createPayload() | ||
const expected = Buffer.from([0x04, 0x02, 0x01, 0x00]) | ||
assert.deepEqual(respPayload, expected) | ||
}) | ||
it('should create a response with constructor from array', function () { | ||
const response = new ReadInputRegistersResponse(6, [0x01, 0x02, 0xFFFE]) | ||
const respPayload = response.createPayload() | ||
const expected = Buffer.from([0x04, 0x06, 0x00, 0x01, 0x00, 0x02, 0xFF, 0xFE]) | ||
assert.deepEqual(respPayload, expected) | ||
}) | ||
}) | ||
@@ -13,0 +29,0 @@ describe('ReadInputRegisters Request', function () { |
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
212499
6065