-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathtcp-request.ts
128 lines (106 loc) · 3.26 KB
/
tcp-request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import Debug = require('debug'); const debug = Debug('tcp-request')
import ModbusAbstractRequest from './abstract-request.js'
import ModbusRequestBody from './request/request-body.js'
import RequestFactory from './request/request-factory.js'
/** Class representing a Modbus TCP Request */
export default class ModbusTCPRequest<ReqBody extends ModbusRequestBody = ModbusRequestBody>
extends ModbusAbstractRequest<ReqBody> {
/** The Transaction ID */
get id () {
return this._id
}
/** The protocol version */
get protocol () {
return this._protocol
}
/** The body length in bytes including the unit ID */
get length () {
return this._length
}
/** The unit ID */
get unitId () {
return this._unitId
}
get address () {
return this.unitId
}
get slaveId () {
return this.unitId
}
/** get function name */
get name () {
return this._body.name
}
public get body () {
return this._body
}
/** For interface compatibility with ModbusRTURequest where data
* integrity checking happens as part of the Modbus protocol
*/
get corrupted () {
return false
}
get byteCount () {
return this._length + 6
}
/** Convert a buffer into a new Modbus TCP Request. Returns null if the buffer
* does not contain enough data.
* @param {Buffer} buffer
* @return A new Modbus TCP Request or Null.
*/
public static fromBuffer (buffer: Buffer) {
try {
if (buffer.length < 7) {
debug('no enough data in the buffer yet')
return null
}
const id = buffer.readUInt16BE(0)
const protocol = buffer.readUInt16BE(2)
const length = buffer.readUInt16BE(4)
const unitId = buffer.readUInt8(6)
debug('tcp header complete, id', id, 'protocol', protocol, 'length', length, 'unitId', unitId)
debug('buffer', buffer)
const body = RequestFactory.fromBuffer(buffer.slice(7, 6 + length))
if (!body) {
return null
}
return new ModbusTCPRequest(id, protocol, length, unitId, body)
} catch (e) {
debug('not enough data to create a tcp request', e)
return null
}
}
protected _id: number
protected _protocol: number
protected _length: number
protected _unitId: number
protected _body: ReqBody
/** Creates a new Modbus TCP Request
* @param {number} Transaction ID
* @param {number} Protocol Type
* @param {number} Byte count of the following data (inc. unitId)
* @param {number} Unit ID
* @param {ReqBody} Actual modbus request containing function code and parameters.
*/
constructor (id: number, protocol: number, length: number, unitId: number, body: ReqBody) {
super()
this._id = id
this._protocol = protocol
this._length = length
this._unitId = unitId
this._body = body
}
/** Creates a buffer object representing the modbus tcp request.
* @returns {Buffer}
*/
public createPayload () {
const body = this._body.createPayload()
const payload = Buffer.alloc(7 + this._body.byteCount)
payload.writeUInt16BE(this._id, 0) // transaction id
payload.writeUInt16BE(0x0000, 2) // protocol version
payload.writeUInt16BE(this._body.byteCount + 1, 4) // length
payload.writeUInt8(this._unitId, 6) // unit id
body.copy(payload, 7)
return payload
}
}