forked from Cloud-Automation/node-modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus-server.ts
132 lines (118 loc) · 7.36 KB
/
modbus-server.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
129
130
131
132
import { EventEmitter } from 'events'
import { Socket } from 'net'
import ModbusAbstractRequest from './abstract-request'
import { BooleanArray } from './constants'
import ModbusServerClient from './modbus-server-client'
type AbstractRequest = ModbusAbstractRequest
export interface IModbusServerOptions {
coils: Buffer
discrete: Buffer
holding: Buffer
input: Buffer
}
const DEFAULT_MODBUS_SERVER_OPTIONS: IModbusServerOptions = {
coils: Buffer.alloc(1024),
discrete: Buffer.alloc(1024),
holding: Buffer.alloc(1024),
input: Buffer.alloc(1024)
}
export type BufferCB = (buffer: Buffer) => void
export default class ModbusServer extends EventEmitter {
private _options: IModbusServerOptions
private get _coils () {
return this._options.coils
}
private get _discrete () {
return this._options.discrete
}
private get _holding () {
return this._options.holding
}
private get _input () {
return this._options.input
}
constructor (options: Partial<IModbusServerOptions> = DEFAULT_MODBUS_SERVER_OPTIONS) {
super()
this._options = {
...DEFAULT_MODBUS_SERVER_OPTIONS,
...options
}
}
get coils () {
return this._coils
}
get discrete () {
return this._discrete
}
get holding () {
return this._holding
}
get input () {
return this._input
}
public on (event: 'connection', listener: (client: ModbusServerClient<any, any, any>) => void): this
public on (event: 'readCoils', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preReadCoils', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'postReadCoils', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'readDiscreteInputs', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preReadDiscreteInputs', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'postReadDiscreteInputs', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'readHoldingRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preReadHoldingRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'postReadHoldingRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'readInputRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preReadInputRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'postReadInputRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'writeSingleCoil', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preWriteSingleCoil', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'postWriteSingleCoil', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'writeSingleRegister', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preWriteSingleRegister', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'postWriteSingleRegister', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'writeMultipleCoils', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preWriteMultipleCoils', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'writeMultipleCoils', listener: (coils: Buffer, oldStatus: BooleanArray) => void): this
public on (event: 'postWriteMultipleCoils', listener: (coils: Buffer, newStatus: BooleanArray) => void): this
public on (event: 'postWriteMultipleCoils', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'writeMultipleRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'preWriteMultipleRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'writeMultipleRegisters', listener: (holdingRegisters: Buffer) => void): this
public on (event: 'postWriteMultipleRegisters', listener: (holdingRegisters: Buffer) => void): this
public on (event: 'postWriteMultipleRegisters', listener: (request: AbstractRequest, cb: BufferCB) => void): this
public on (event: 'connection', listener: (socket: Socket) => void): this
public on (event: string | symbol, listener: (...args: any[]) => void): this {
return super.on(event, listener)
}
public emit (event: 'connection', client: ModbusServerClient<any, any, any>): boolean
public emit (event: 'readCoils', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preReadCoils', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'postReadCoils', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'readDiscreteInputs', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preReadDiscreteInputs', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'postReadDiscreteInputs', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'readHoldingRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preReadHoldingRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'postReadHoldingRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'readInputRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preReadInputRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'postReadInputRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'writeSingleCoil', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preWriteSingleCoil', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'postWriteSingleCoil', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'writeSingleRegister', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preWriteSingleRegister', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'postWriteSingleRegister', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'writeMultipleCoils', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preWriteMultipleCoils', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'writeMultipleCoils', coils: Buffer, oldStatus: BooleanArray): boolean
public emit (event: 'postWriteMultipleCoils', coils: Buffer, newStatus: BooleanArray): boolean
public emit (event: 'postWriteMultipleCoils', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'writeMultipleRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'preWriteMultipleRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: 'writeMultipleRegisters', holdingRegisters: Buffer): boolean
public emit (event: 'postWriteMultipleRegisters', holdingRegisters: Buffer): boolean
public emit (event: 'postWriteMultipleRegisters', request: AbstractRequest, cb: BufferCB): boolean
public emit (event: string | symbol, ...args: any[]): boolean {
return super.emit(event, ...args)
}
}