Skip to content

Commit c1847f0

Browse files
committed
Merge branch 'murat-dogan-exception-crc-check' into v4.0-dev
2 parents f348a56 + c4a8a89 commit c1847f0

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsmodbus",
3-
"version": "4.0.4",
3+
"version": "4.0.6",
44
"description": "Implementation for the Serial/TCP Modbus protocol.",
55
"author": "Stefan Poeter <stefan.poeter@cloud-automation.de>",
66
"main": "./dist/modbus.js",

src/response/exception.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export default class ExceptionResponseBody extends ModbusResponseBody {
6464

6565
public createPayload () {
6666
const payload = Buffer.alloc(2)
67-
payload.writeUInt8(this._fc, 0)
67+
// This is a exception Response
68+
// Add 0x80 for compatibility (crc check)
69+
payload.writeUInt8(this._fc + 0x80, 0)
6870
payload.writeUInt8(this._code, 1)
6971
return payload
7072
}

test/rtu-client-request-handler.test.js

+38-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ describe('Modbus/RTU Client Request Tests', function () {
6868
it('should register an rtu request and handle a exception response', function (done) {
6969
const handler = new ModbusRTUClientRequestHandler(socket, 4)
7070
const request = new ReadCoilsRequest(0x0000, 0x0008)
71-
const response = new ExceptionResponse(0x81, 0x01)
72-
const rtuResponse = new ModbusRTUResponse(4, 8352, response)
71+
const response = new ExceptionResponse(0x01, 0x01)
72+
const rtuResponse = new ModbusRTUResponse(4, 37265, response)
7373

7474
socket.emit('open')
7575

@@ -80,14 +80,48 @@ describe('Modbus/RTU Client Request Tests', function () {
8080
assert.ok(false)
8181

8282
done()
83-
}).catch(function () {
84-
assert.ok(true)
83+
}).catch(function (err) {
84+
// Exception type should be ModbusException not crcMismatch or any other
85+
assert.equal(err.err, 'ModbusException')
86+
socketMock.verify()
87+
88+
done()
89+
})
90+
91+
handler.handle(rtuResponse)
92+
})
93+
94+
it('should calculate exception response crc correctly', function (done) {
95+
const handler = new ModbusRTUClientRequestHandler(socket, 1)
96+
const request = new ReadHoldingRegistersRequestBody(0x4000, 0x0002)
97+
const responseBuffer = Buffer.from([
98+
0x01, // address
99+
0x83, // fc
100+
0x02, // error code
101+
0xc0, 0xf1 // crc
102+
])
103+
const rtuResponse = ModbusRTUResponse.fromBuffer(responseBuffer)
104+
105+
socket.emit('open')
106+
107+
socketMock.expects('write').once()
108+
109+
handler.register(request)
110+
.then(function (resp) {
111+
assert.ok(false)
112+
113+
done()
114+
}).catch(function (err) {
115+
// Exception type should be ModbusException not crcMismatch or any other
116+
assert.equal(err.err, 'ModbusException')
85117
socketMock.verify()
86118

87119
done()
88120
})
89121

90122
handler.handle(rtuResponse)
123+
// rtuResponse.crc
124+
91125
})
92126
})
93127
})

0 commit comments

Comments
 (0)