jsmodbus
Version:
Implementation for the Serial/TCP Modbus protocol.
59 lines (51 loc) • 1.56 kB
JavaScript
/* global describe, it */
const assert = require('assert')
const ResponseBody = require('../src/response/response-body.js')
const ReadCoilsResponseBody = require('../src/response/read-coils.js')
describe('Modbus Response Tests.', function () {
/* with the read coils tests we test most of the common errors
* like modbus exceptions, outOfSync errors, timeouts and so on */
describe('Read Coils Tests.', function () {
it('should create request from buffer', function () {
const buffer = Buffer.from([
0x01, // fc
0x02, // byte count
0xdd, // coils
0x00
])
const response = ResponseBody.fromBuffer(buffer)
assert.ok(response !== null)
assert.ok(response instanceof ReadCoilsResponseBody)
assert.equal(0x01, response.fc)
assert.equal(0x02, response.numberOfBytes)
assert.equal(0x04, response.byteCount)
assert.deepEqual(
[true,
false,
true,
true,
true,
false,
true,
true,
false,
false,
false,
false,
false,
false,
false,
false], response.valuesAsArray)
})
it('should handle invalid buffer content', function () {
const buffer = Buffer.from([
0x01, // fc
0x02, // byte count
0xdd // coils
])
const response = ReadCoilsResponseBody.fromBuffer(buffer)
assert.ok(response === null)
})
})
})