nodbus-plus 0.8.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.readthedocs.yaml +35 -0
- package/LICENSE.md +21 -21
- package/README.md +163 -48
- package/docs/Makefile +20 -0
- package/docs/client/channel.rst +209 -0
- package/docs/client/nodbus_master_serial.rst +559 -0
- package/docs/client/nodbus_master_tcp.rst +573 -0
- package/docs/conf.py +41 -0
- package/docs/images/01-readcoils.png +0 -0
- package/docs/images/02-readinputs.png +0 -0
- package/docs/images/03-readholding.png +0 -0
- package/docs/images/04-readinputsreg.png +0 -0
- package/docs/images/05-writecoil.png +0 -0
- package/docs/images/06-writeregister.png +0 -0
- package/docs/images/15-writecoil.png +0 -0
- package/docs/images/16.png +0 -0
- package/docs/images/22-mask.png +0 -0
- package/docs/images/23.png +0 -0
- package/docs/images/7.png +0 -0
- package/docs/images/modbus_pdu.png +0 -0
- package/docs/images/serial_adu.png +0 -0
- package/docs/images/tcp_adu.png +0 -0
- package/docs/index.rst +30 -0
- package/docs/make.bat +35 -0
- package/docs/protocol/modbus_master.rst +276 -0
- package/docs/protocol/modbus_master_serial.rst +290 -0
- package/docs/protocol/modbus_master_tcp.rst +296 -0
- package/docs/protocol/modbus_server.rst +469 -0
- package/docs/protocol/modbus_server_serial.rst +543 -0
- package/docs/protocol/modbus_server_tcp.rst +365 -0
- package/docs/requirements.txt +4 -0
- package/docs/server/net_server.rst +242 -0
- package/docs/server/nodbus_serial_server.rst +652 -0
- package/docs/server/nodbus_tcp_server.rst +505 -0
- package/docs/starting.rst +192 -0
- package/docs/static/simple logo.jpg +0 -0
- package/package.json +39 -30
- package/samples/mb_serial_server.js +77 -0
- package/samples/mb_tcp_client.js +114 -0
- package/samples/mb_tcp_server.js +58 -0
- package/src/client/net/serialchannel.js +195 -0
- package/src/client/net/tcpchannel.js +233 -0
- package/src/client/net/udpchannel.js +243 -0
- package/src/client/nodbus_serial_client.js +577 -0
- package/src/client/nodbus_tcp_client.js +542 -0
- package/src/nodbus-plus.js +157 -110
- package/src/protocol/modbus_master.js +298 -961
- package/src/protocol/modbus_master_serial.js +247 -0
- package/src/protocol/modbus_master_tcp.js +219 -0
- package/src/protocol/modbus_server.js +936 -0
- package/src/protocol/modbus_server_serial.js +368 -0
- package/src/protocol/modbus_server_tcp.js +129 -0
- package/src/protocol/utils.js +296 -0
- package/src/server/net/serialserver.js +184 -0
- package/src/server/net/tcpserver.js +290 -0
- package/src/server/net/udpserver.js +242 -0
- package/src/server/nodbus_serial_server.js +238 -0
- package/src/server/nodbus_tcp_server.js +249 -0
- package/test/modbus_master.test.js +279 -0
- package/test/modbus_master_serial.test.js +124 -0
- package/test/modbus_master_tcp.test.js +178 -0
- package/test/modbus_server.test.js +506 -0
- package/test/modbus_server_serial.test.js +328 -0
- package/test/modbus_server_tcp.test.js +91 -0
- package/test/nodbus_client_serial.test.js +307 -0
- package/test/nodbus_client_tcp.test.js +334 -0
- package/test/nodbus_server_serial.test.js +255 -0
- package/test/nodbus_server_tcp.test.js +216 -0
- package/CHANGELOG.md +0 -27
- package/src/client/m_stcp_client.js +0 -214
- package/src/client/m_tcp_client.js +0 -234
- package/src/net/tcpclient.js +0 -173
- package/src/net/tcpserver.js +0 -329
- package/src/protocol/adu.js +0 -40
- package/src/protocol/ascii_adu.js +0 -139
- package/src/protocol/boolean_register.js +0 -78
- package/src/protocol/functions/Force_Multiple_Coils.js +0 -76
- package/src/protocol/functions/Force_Single_Coil.js +0 -54
- package/src/protocol/functions/Mask_Holding_Register.js +0 -47
- package/src/protocol/functions/Preset_Multiple_Registers.js +0 -53
- package/src/protocol/functions/Preset_Single_Register.js +0 -39
- package/src/protocol/functions/Read_Coil_Status.js +0 -59
- package/src/protocol/functions/Read_Holding_Registers.js +0 -52
- package/src/protocol/functions/Read_Input_Registers.js +0 -52
- package/src/protocol/functions/Read_Input_Status.js +0 -58
- package/src/protocol/mbap.js +0 -60
- package/src/protocol/modbus_device.js +0 -35
- package/src/protocol/modbus_slave.js +0 -522
- package/src/protocol/pdu.js +0 -70
- package/src/protocol/rtu_adu.js +0 -122
- package/src/protocol/serial_adu.js +0 -29
- package/src/protocol/tcp_adu.js +0 -84
- package/src/protocol/word_register.js +0 -122
- package/src/server/m_stcp_server.js +0 -310
- package/src/server/m_tcp_server.js +0 -295
- package/test/modbus-stcp-server-test.js +0 -72
- package/test/modbus-stcp-server-test1.js +0 -72
- package/test/modbus-tcp-client-test.js +0 -159
- package/test/modbus-tcp-server-test.js +0 -75
- package/test/modbus-tcp-server-test2.js +0 -75
- package/test/modbus_stcp_client.js +0 -149
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Modbus Tcp server module.
|
|
3
|
-
* @module server/m_tcp_server.
|
|
4
|
-
* @author Hector E. Socarras.
|
|
5
|
-
* @version 0.4.0
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const ModbusSlave = require('../protocol/modbus_slave');
|
|
9
|
-
const TcpServer = require('../net/tcpserver');
|
|
10
|
-
const ADU = require('../protocol/tcp_adu');
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Class representing a modbus tcp server.
|
|
15
|
-
* @extends ModbusSlave
|
|
16
|
-
*/
|
|
17
|
-
class ModbusTCPServer extends ModbusSlave {
|
|
18
|
-
/**
|
|
19
|
-
* Create a Modbus Tcp Server.
|
|
20
|
-
* @param {number} p Port to listen.
|
|
21
|
-
*/
|
|
22
|
-
constructor(p=502){
|
|
23
|
-
super();
|
|
24
|
-
|
|
25
|
-
var self = this;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* network layer
|
|
29
|
-
* @type {object}
|
|
30
|
-
*/
|
|
31
|
-
this.tcpServer = new TcpServer();
|
|
32
|
-
|
|
33
|
-
//Adding listeners to tcpServer events
|
|
34
|
-
|
|
35
|
-
this.tcpServer.onData = this.ProcessModbusIndication.bind(this);
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @fires ModbusTCPServer#connection
|
|
39
|
-
*/
|
|
40
|
-
this.tcpServer.onConnection = function EmitConnection (socket) {
|
|
41
|
-
/**
|
|
42
|
-
* connection event.
|
|
43
|
-
* Emited when new connecton is sablished
|
|
44
|
-
* @event ModbusTCPServer#connection
|
|
45
|
-
* @type {object}
|
|
46
|
-
* @see https://nodejs.org/api/net.html
|
|
47
|
-
*/
|
|
48
|
-
this.emit('connection',socket);
|
|
49
|
-
}.bind(this);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Event connection closed
|
|
54
|
-
* Emited when socket closed
|
|
55
|
-
* @see https://nodejs.org/api/net.html
|
|
56
|
-
* @fires ModbusTCPServer#connection-closed
|
|
57
|
-
*/
|
|
58
|
-
this.tcpServer.onConnectionClose = function EmitConnectionClosed(socket){
|
|
59
|
-
/**
|
|
60
|
-
* connection-closed event.
|
|
61
|
-
* @event ModbusTCPServer#connection-closed
|
|
62
|
-
* @type {object}
|
|
63
|
-
*/
|
|
64
|
-
this.emit('connection-closed', socket)
|
|
65
|
-
}.bind(this);
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Event access denied
|
|
69
|
-
* Emited when new connecton is rejected by filter rules
|
|
70
|
-
* @fires ModbusTCPServer#access-denied
|
|
71
|
-
*/
|
|
72
|
-
this.tcpServer.onAccessDenied = function EmitAccesDenied(socket){
|
|
73
|
-
/**
|
|
74
|
-
* access-denied event.
|
|
75
|
-
* @event ModbusTCPServer#access-denied
|
|
76
|
-
* @type {object}
|
|
77
|
-
*/
|
|
78
|
-
this.emit('access-denied', socket);
|
|
79
|
-
}.bind(this);
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Event listening
|
|
83
|
-
* Emited when server is listening
|
|
84
|
-
* @see https://nodejs.org/api/net.html
|
|
85
|
-
* @fires ModbusTCPServer#listening
|
|
86
|
-
*/
|
|
87
|
-
this.tcpServer.onListening = function EmitListening(port){
|
|
88
|
-
/**
|
|
89
|
-
* listening event.
|
|
90
|
-
* @event ModbusTCPServer#listening
|
|
91
|
-
* @type {number}
|
|
92
|
-
*/
|
|
93
|
-
this.emit('listening',self.port);
|
|
94
|
-
}.bind(this);
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Event closed
|
|
98
|
-
* Emited when server is closed
|
|
99
|
-
* @see https://nodejs.org/api/net.html
|
|
100
|
-
* @fires ModbusTCPServer#closed
|
|
101
|
-
*/
|
|
102
|
-
this.tcpServer.onServerClose = function EmitClosed(){
|
|
103
|
-
/**
|
|
104
|
-
* closed event.
|
|
105
|
-
* @event ModbusTCPServer#closed
|
|
106
|
-
*/
|
|
107
|
-
this.emit('closed');
|
|
108
|
-
}.bind(this);
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Event error
|
|
112
|
-
* Emited when error hapen
|
|
113
|
-
* @fires ModbusTCPServer#error
|
|
114
|
-
*/
|
|
115
|
-
this.tcpServer.onError = function EmitError(err){
|
|
116
|
-
/**
|
|
117
|
-
* error event.
|
|
118
|
-
* @event ModbusTCPServer#error
|
|
119
|
-
*/
|
|
120
|
-
this.emit('error', err);
|
|
121
|
-
}.bind(this);
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Event response
|
|
125
|
-
* Emited when response is send to master
|
|
126
|
-
* @fires ModbusTCPServer#response
|
|
127
|
-
*/
|
|
128
|
-
this.tcpServer.onWrite = function EmitResponse(resp){
|
|
129
|
-
/**
|
|
130
|
-
* response event.
|
|
131
|
-
* @event ModbusTCPServer#response
|
|
132
|
-
*/
|
|
133
|
-
this.emit('response', resp);
|
|
134
|
-
}.bind(this);
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Event client disconnect
|
|
138
|
-
* Emited when client send fin packet
|
|
139
|
-
* @fires ModbusTCPServer#client-disconnect
|
|
140
|
-
*/
|
|
141
|
-
this.tcpServer.onClientEnd = function EmitClientDisconnect(socket){
|
|
142
|
-
/**
|
|
143
|
-
* client-disconnect event.
|
|
144
|
-
* @event ModbusTCPServer#client-disconnect
|
|
145
|
-
*/
|
|
146
|
-
this.emit('client-disconnect',socket);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* port
|
|
151
|
-
* @type {number}
|
|
152
|
-
* @public
|
|
153
|
-
*/
|
|
154
|
-
Object.defineProperty(self, 'port', {
|
|
155
|
-
get : function(){
|
|
156
|
-
return self.tcpServer.port;
|
|
157
|
-
},
|
|
158
|
-
set : function(p){
|
|
159
|
-
self.tcpServer.port = p;
|
|
160
|
-
}
|
|
161
|
-
})
|
|
162
|
-
this.port = p;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* listening status
|
|
166
|
-
* @type {bool}
|
|
167
|
-
* @public
|
|
168
|
-
*/
|
|
169
|
-
Object.defineProperty(self, 'isListening',{
|
|
170
|
-
get: function(){
|
|
171
|
-
return self.tcpServer.isListening;
|
|
172
|
-
}
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* max client
|
|
177
|
-
* @type {number}
|
|
178
|
-
* @public
|
|
179
|
-
*/
|
|
180
|
-
Object.defineProperty(self, 'maxConnections',{
|
|
181
|
-
get: function(){
|
|
182
|
-
return self.tcpServer.maxConnections;
|
|
183
|
-
},
|
|
184
|
-
set: function(max){
|
|
185
|
-
self.tcpServer.maxConnections = max;
|
|
186
|
-
}
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
//Sellando el tcpServer
|
|
190
|
-
Object.defineProperty(self, 'tcpServer', {
|
|
191
|
-
enumerable:false,
|
|
192
|
-
writable:false,
|
|
193
|
-
configurable:false
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Function to start the server
|
|
200
|
-
*/
|
|
201
|
-
Start(){
|
|
202
|
-
this.tcpServer.Start();
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Function to stop the server
|
|
207
|
-
*/
|
|
208
|
-
Stop(){
|
|
209
|
-
this.tcpServer.Stop();
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Function to execute when data are recive
|
|
214
|
-
* @param {Buffer} aduBuffer frame received by server
|
|
215
|
-
* @return {Buffer} response;
|
|
216
|
-
* @fires ModbusTCPServer#indication {Buffer}
|
|
217
|
-
*/
|
|
218
|
-
ProcessModbusIndication(aduBuffer){
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* indication event.
|
|
222
|
-
* @event ModbusTCPServer#indication
|
|
223
|
-
*/
|
|
224
|
-
this.emit('indication', aduBuffer);
|
|
225
|
-
|
|
226
|
-
var indicationADU = new ADU(aduBuffer);
|
|
227
|
-
|
|
228
|
-
//checking header
|
|
229
|
-
if (this.AnalizeADU(indicationADU)){
|
|
230
|
-
//Si retorna 1
|
|
231
|
-
return Buffer.alloc(0);
|
|
232
|
-
}
|
|
233
|
-
else{
|
|
234
|
-
//Header ok
|
|
235
|
-
indicationADU.ParseBuffer();
|
|
236
|
-
//creando la respuesta
|
|
237
|
-
var responsePDU = this.BuildResponse(indicationADU.pdu);
|
|
238
|
-
|
|
239
|
-
if(indicationADU.address == 0){
|
|
240
|
-
//broadcast no response
|
|
241
|
-
return Buffer.alloc(0);
|
|
242
|
-
}
|
|
243
|
-
else{
|
|
244
|
-
//response
|
|
245
|
-
if(responsePDU != null){
|
|
246
|
-
var modbusResponse = new ADU();
|
|
247
|
-
modbusResponse.address = indicationADU.mbap.unitID;
|
|
248
|
-
modbusResponse.transactionCounter = indicationADU.mbap.transactionID;
|
|
249
|
-
modbusResponse.pdu = responsePDU;
|
|
250
|
-
modbusResponse.MakeBuffer();
|
|
251
|
-
|
|
252
|
-
return modbusResponse.aduBuffer;
|
|
253
|
-
}
|
|
254
|
-
else{
|
|
255
|
-
return Buffer.alloc(0);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Make the response modbus tcp header
|
|
266
|
-
* @param {buffer} adu frame off modbus indication
|
|
267
|
-
* @return {number} error code. 1- error, 0-no errror
|
|
268
|
-
* @fires ModbusTCPServer#error {object}
|
|
269
|
-
*/
|
|
270
|
-
AnalizeADU(adu){
|
|
271
|
-
try{
|
|
272
|
-
adu.ParseBuffer();
|
|
273
|
-
|
|
274
|
-
if (adu.mbap.protocolID != 0){
|
|
275
|
-
//si el protocolo no es modbus standard
|
|
276
|
-
this.emit('modbus_exeption','Protocol not Suported');
|
|
277
|
-
return 1;
|
|
278
|
-
}
|
|
279
|
-
else if (adu.mbap.length != adu.aduBuffer.length-6){
|
|
280
|
-
//Verificando el campo length
|
|
281
|
-
this.emit('modbus_exeption','ByteCount error');
|
|
282
|
-
return 1;
|
|
283
|
-
}
|
|
284
|
-
else{
|
|
285
|
-
return 0;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
catch(e){
|
|
289
|
-
this.emit('error', e);
|
|
290
|
-
return 1;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
module.exports = ModbusTCPServer;
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
var nodbus = require('..');
|
|
2
|
-
|
|
3
|
-
var modbusSTCPServer = nodbus.CreateSlave(502, 2);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
modbusSTCPServer.on('listening', function(port){
|
|
7
|
-
console.log('Server listening on: ' + port);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
modbusSTCPServer.on('values', function(reference, address){
|
|
11
|
-
console.log('data write on: ' + (reference == '0x' ? 'coils' : 'holding registers'))
|
|
12
|
-
address.forEach(function(value, key){
|
|
13
|
-
console.log(key + ': ' + value);
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
modbusSTCPServer.on('connection', function(socket){
|
|
18
|
-
console.log('New connection form: ' + socket.remoteAddress);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
modbusSTCPServer.on('error', function(err){
|
|
22
|
-
console.log(err)
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
modbusSTCPServer.on('closed', function(){
|
|
26
|
-
console.log('server closed');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
modbusSTCPServer.on('client-disconnect', function(socket){
|
|
30
|
-
console.log('Client ' + socket.remoteAddress + ' disconnected');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
modbusSTCPServer.on('indication', function(adubuffer){
|
|
34
|
-
console.log('Indication Recieved');
|
|
35
|
-
console.log(adubuffer);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
modbusSTCPServer.on('response', function(resp){
|
|
39
|
-
console.log('Response:');
|
|
40
|
-
console.log(resp);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
modbusSTCPServer.SetData(1, 'inputs', 2);
|
|
44
|
-
modbusSTCPServer.SetData(1, 'inputs', 3);
|
|
45
|
-
modbusSTCPServer.SetData(1, 'inputs', 7);
|
|
46
|
-
modbusSTCPServer.SetData(1, 'inputs', 11);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
modbusSTCPServer.SetData(1, 'coils', 1);
|
|
50
|
-
modbusSTCPServer.SetData(1, 'coils', 6);
|
|
51
|
-
modbusSTCPServer.SetData(1, 'coils', 9);
|
|
52
|
-
modbusSTCPServer.SetData(1, 'coils', 15);
|
|
53
|
-
|
|
54
|
-
modbusSTCPServer.SetData(59, 'holding', 0, 'uint');
|
|
55
|
-
modbusSTCPServer.SetData(-358, 'holding', 2, 'int');
|
|
56
|
-
modbusSTCPServer.SetData(3.14, 'holding', 3, 'float');
|
|
57
|
-
modbusSTCPServer.SetData(3598.59, 'holding', 9, 'double');
|
|
58
|
-
|
|
59
|
-
modbusSTCPServer.SetData(-27, 'inputs-registers', 1, 'int');
|
|
60
|
-
modbusSTCPServer.SetData(-34752648, 'inputs-registers', 2, 'int32');
|
|
61
|
-
modbusSTCPServer.SetData(123456789, 'inputs-registers', 5, 'uint32');
|
|
62
|
-
|
|
63
|
-
console.log(modbusSTCPServer.GetData('holding',0,'uint'));
|
|
64
|
-
console.log(modbusSTCPServer.GetData('holding',2,'int'));
|
|
65
|
-
console.log(modbusSTCPServer.GetData('holding',3,'float'));
|
|
66
|
-
console.log(modbusSTCPServer.GetData('holding',9,'double'));
|
|
67
|
-
console.log(modbusSTCPServer.GetData(3,2,'int32'));
|
|
68
|
-
console.log(modbusSTCPServer.GetData(3,5,'uint32'));
|
|
69
|
-
console.log(modbusSTCPServer.GetData(1,7));
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
modbusSTCPServer.Start();
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
var nodbus = require('..');
|
|
2
|
-
|
|
3
|
-
var modbusSTCPServer = nodbus.CreateSlave(501, 1);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
modbusSTCPServer.on('listening', function(port){
|
|
7
|
-
console.log('Server listening on: ' + port);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
modbusSTCPServer.on('values', function(reference, address){
|
|
11
|
-
console.log('data write on: ' + (reference == '0x' ? 'coils' : 'holding registers'))
|
|
12
|
-
address.forEach(function(value, key){
|
|
13
|
-
console.log(key + ': ' + value);
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
modbusSTCPServer.on('connection', function(socket){
|
|
18
|
-
console.log('New connection form: ' + socket.remoteAddress);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
modbusSTCPServer.on('error', function(err){
|
|
22
|
-
console.log(err)
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
modbusSTCPServer.on('closed', function(){
|
|
26
|
-
console.log('server closed');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
modbusSTCPServer.on('client-disconnect', function(socket){
|
|
30
|
-
console.log('Client ' + socket.remoteAddress + ' disconnected');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
modbusSTCPServer.on('indication', function(adubuffer){
|
|
34
|
-
console.log('Indication Recieved');
|
|
35
|
-
console.log(adubuffer);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
modbusSTCPServer.on('response', function(resp){
|
|
39
|
-
console.log('Response:');
|
|
40
|
-
console.log(resp);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
modbusSTCPServer.SetData(1, 'inputs', 2);
|
|
44
|
-
modbusSTCPServer.SetData(1, 'inputs', 3);
|
|
45
|
-
modbusSTCPServer.SetData(1, 'inputs', 7);
|
|
46
|
-
modbusSTCPServer.SetData(1, 'inputs', 11);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
modbusSTCPServer.SetData(1, 'coils', 1);
|
|
50
|
-
modbusSTCPServer.SetData(1, 'coils', 6);
|
|
51
|
-
modbusSTCPServer.SetData(1, 'coils', 9);
|
|
52
|
-
modbusSTCPServer.SetData(1, 'coils', 15);
|
|
53
|
-
|
|
54
|
-
modbusSTCPServer.SetData(59, 'holding', 0, 'uint');
|
|
55
|
-
modbusSTCPServer.SetData(-358, 'holding', 2, 'int');
|
|
56
|
-
modbusSTCPServer.SetData(3.14, 'holding', 3, 'float');
|
|
57
|
-
modbusSTCPServer.SetData(3598.59, 'holding', 9, 'double');
|
|
58
|
-
|
|
59
|
-
modbusSTCPServer.SetData(-27, 'inputs-registers', 1, 'int');
|
|
60
|
-
modbusSTCPServer.SetData(-34752648, 'inputs-registers', 2, 'int32');
|
|
61
|
-
modbusSTCPServer.SetData(123456789, 'inputs-registers', 5, 'uint32');
|
|
62
|
-
|
|
63
|
-
console.log(modbusSTCPServer.GetData('holding',0,'uint'));
|
|
64
|
-
console.log(modbusSTCPServer.GetData('holding',2,'int'));
|
|
65
|
-
console.log(modbusSTCPServer.GetData('holding',3,'float'));
|
|
66
|
-
console.log(modbusSTCPServer.GetData('holding',9,'double'));
|
|
67
|
-
console.log(modbusSTCPServer.GetData(3,2,'int32'));
|
|
68
|
-
console.log(modbusSTCPServer.GetData(3,5,'uint32'));
|
|
69
|
-
console.log(modbusSTCPServer.GetData(1,7));
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
modbusSTCPServer.Start();
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
var nodbus = require('..');
|
|
2
|
-
|
|
3
|
-
var modbusTCPClient = new nodbus.ModbusTcpClient();
|
|
4
|
-
|
|
5
|
-
var value;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
modbusTCPClient.AddSlave ('plc1', {port:502, ip:'127.0.0.1', timeout:50, modbusAddress:10});
|
|
9
|
-
modbusTCPClient.AddSlave ('plc2', {port:500, ip:'127.0.0.1', timeout:100, modbusAddress:15});
|
|
10
|
-
//modbusTCPClient.AddSlave ('plc3', {port:505, ip:'127.0.0.1', timeout:50, modbusAddress:12});
|
|
11
|
-
//modbusTCPClient.AddSlave ('plc4', {port:506, ip:'127.0.0.1', timeout:100, modbusAddress:17});
|
|
12
|
-
//modbusTCPClient.AddSlave ('plc5', {port:507, ip:'127.0.0.1', timeout:100, modbusAddress:19});
|
|
13
|
-
|
|
14
|
-
//Configurando todos los listeners
|
|
15
|
-
|
|
16
|
-
modbusTCPClient.on('data', function(id, data){
|
|
17
|
-
console.log('Data from' + id + ': \n');
|
|
18
|
-
console.log(data);
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
modbusTCPClient.on('timeout', function(id){
|
|
22
|
-
console.log(id+':timeout');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
modbusTCPClient.on('error', function(id, err){
|
|
26
|
-
console.log(id + ':error')
|
|
27
|
-
console.log(err)
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
modbusTCPClient.on('modbus_exception', function(id, modbusError){
|
|
31
|
-
console.log(id + ': '+ modbusError);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
modbusTCPClient.on('connect', function(id){
|
|
35
|
-
console.log('connection stablished whit ' + id);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
modbusTCPClient.on('indication', function(id, data){
|
|
39
|
-
console.log('indication send to ' + id);
|
|
40
|
-
console.log(data)
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
modbusTCPClient.on('disconnect', function(id, err){
|
|
44
|
-
console.log(id+' disconnected');
|
|
45
|
-
console.log(err)
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
modbusTCPClient.on('raw_data', function(id, data){
|
|
49
|
-
console.log('buffer from ' + id)
|
|
50
|
-
console.log(data);
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
modbusTCPClient.on('idle', function(id){
|
|
54
|
-
console.log(id +' is waiting');
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
function Test(){
|
|
58
|
-
console.log('starting test')
|
|
59
|
-
//provando evento modbus exeption
|
|
60
|
-
//modbusTCPClient.ReadHoldingRegisters(15265, 1);
|
|
61
|
-
|
|
62
|
-
//provando funcion 1
|
|
63
|
-
setTimeout(function(){
|
|
64
|
-
console.log('leyendo coils de la 0 a la 7');
|
|
65
|
-
modbusTCPClient.ReadCoilStatus('plc1', 0, 8);
|
|
66
|
-
modbusTCPClient.ReadCoilStatus('plc2', 0, 8);
|
|
67
|
-
modbusTCPClient.ReadCoilStatus('plc5', 0, 8);
|
|
68
|
-
}, 100);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
//provando funcion 2
|
|
72
|
-
setTimeout(function(){
|
|
73
|
-
console.log('leyendo inputs de la 3 a la 8');
|
|
74
|
-
modbusTCPClient.ReadInputStatus('plc1', 3, 6);
|
|
75
|
-
modbusTCPClient.ReadInputStatus('plc2', 3, 6);
|
|
76
|
-
}, 150);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//provando funcion 3
|
|
80
|
-
setTimeout(function(){
|
|
81
|
-
console.log('leyendo holdingRegisters del 0 al 3');
|
|
82
|
-
modbusTCPClient.ReadHoldingRegisters('plc1', 0, 4);
|
|
83
|
-
modbusTCPClient.ReadHoldingRegisters('plc2', 0, 4);
|
|
84
|
-
}, 200);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//provando funcion 4
|
|
88
|
-
setTimeout(function(){
|
|
89
|
-
console.log('leyendo inputsRegisters del 1 al 5');
|
|
90
|
-
modbusTCPClient.ReadInputRegisters('plc1', 1, 5);
|
|
91
|
-
modbusTCPClient.ReadInputRegisters('plc2', 1, 5);
|
|
92
|
-
}, 250);
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
//provando funcion 5
|
|
96
|
-
setTimeout(function(){
|
|
97
|
-
console.log('forzando la coil 5 a 1');
|
|
98
|
-
modbusTCPClient.ForceSingleCoil('plc1', true, 5);
|
|
99
|
-
modbusTCPClient.ForceSingleCoil('plc2', true, 5);
|
|
100
|
-
}, 300)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
//provando funcion 6
|
|
105
|
-
setTimeout(function(){
|
|
106
|
-
console.log('forzando el registro 14 a 12536');
|
|
107
|
-
modbusTCPClient.PresetSingleRegister('plc1', 12536, 14);
|
|
108
|
-
modbusTCPClient.PresetSingleRegister('plc2', 12536, 14);
|
|
109
|
-
}, 350)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
//provando funcion 15
|
|
113
|
-
setTimeout(function(){
|
|
114
|
-
console.log('forzando las coils 3 al 12 a 1011001010');
|
|
115
|
-
values = [1, 0, 1, 1, 0, 0, 1, 0, 1, 0];
|
|
116
|
-
modbusTCPClient.ForceMultipleCoils('plc1', values, 3);
|
|
117
|
-
modbusTCPClient.ForceMultipleCoils('plc2', values, 3);
|
|
118
|
-
}, 400)
|
|
119
|
-
|
|
120
|
-
//provando funcion 16
|
|
121
|
-
setTimeout(function(){
|
|
122
|
-
console.log('forzando los registros 16 al 21 a [3.14, -54, 0, 7852689]');
|
|
123
|
-
values = [3.14, -54, 0, 7852689];
|
|
124
|
-
modbusTCPClient.PresetMultipleRegisters('plc1', values , 16);
|
|
125
|
-
modbusTCPClient.PresetMultipleRegisters('plc2', values , 16);
|
|
126
|
-
}, 450)
|
|
127
|
-
|
|
128
|
-
//provando funcion 22
|
|
129
|
-
setTimeout(function(){
|
|
130
|
-
console.log('mask registro 5');
|
|
131
|
-
values = [1, 0, 0, 1, -1, 0, 1, -1, -1, -1, 0, 0, 1, 1, -1, 0];
|
|
132
|
-
modbusTCPClient.MaskHoldingRegister('plc1', values , 5);
|
|
133
|
-
modbusTCPClient.MaskHoldingRegister('plc2', values , 5);
|
|
134
|
-
}, 500)
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
let prom;
|
|
138
|
-
setTimeout(function(){
|
|
139
|
-
console.log('check connection');
|
|
140
|
-
console.log(modbusTCPClient.isReady('plc1'));
|
|
141
|
-
prom = modbusTCPClient.Stop('plc1');
|
|
142
|
-
prom.then(function(id){
|
|
143
|
-
console.log(`isconected from ${id}`);
|
|
144
|
-
})
|
|
145
|
-
},2400);
|
|
146
|
-
|
|
147
|
-
let promise = modbusTCPClient.Start();
|
|
148
|
-
|
|
149
|
-
promise.then(function(value){
|
|
150
|
-
console.log('conected to:');
|
|
151
|
-
console.log(value);
|
|
152
|
-
Test();
|
|
153
|
-
}, function(value){
|
|
154
|
-
console.log('fail to conect to');
|
|
155
|
-
console.log(value);
|
|
156
|
-
Test();
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
var nodbus = require('..');
|
|
2
|
-
|
|
3
|
-
var modbusTCPServer = new nodbus.ModbusTcpServer(502);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
modbusTCPServer.on('listening', function(port){
|
|
7
|
-
console.log('Server listening on: ' + port);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
modbusTCPServer.on('values', function(reference, address){
|
|
11
|
-
console.log('data write on: ' + (reference == '0x' ? 'coils' : 'holding registers'))
|
|
12
|
-
address.forEach(function(value, key){
|
|
13
|
-
console.log(key + ': ' + value);
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
modbusTCPServer.on('connection', function(socket){
|
|
18
|
-
console.log('New connection form: ' + socket.remoteAddress);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
modbusTCPServer.on('error', function(err){
|
|
22
|
-
console.log(err)
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
modbusTCPServer.on('closed', function(){
|
|
26
|
-
console.log('server closed');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
modbusTCPServer.on('client-disconnect', function(socket){
|
|
30
|
-
console.log('Client ' + socket.remoteAddress + ' disconnected');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
modbusTCPServer.on('indication', function(adubuffer){
|
|
34
|
-
console.log('Indication Recieved');
|
|
35
|
-
console.log(adubuffer);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
modbusTCPServer.on('response', function(resp){
|
|
39
|
-
console.log('Response:');
|
|
40
|
-
console.log(resp);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
modbusTCPServer.SetData(1, 'inputs', 2);
|
|
45
|
-
modbusTCPServer.SetData(1, 'inputs', 3);
|
|
46
|
-
modbusTCPServer.SetData(1, 'inputs', 7);
|
|
47
|
-
modbusTCPServer.SetData(1, 'inputs', 11);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
modbusTCPServer.SetData(1, 'coils', 1);
|
|
51
|
-
modbusTCPServer.SetData(1, 'coils', 6);
|
|
52
|
-
modbusTCPServer.SetData(1, 'coils', 9);
|
|
53
|
-
modbusTCPServer.SetData(1, 'coils', 15);
|
|
54
|
-
|
|
55
|
-
modbusTCPServer.SetData(59, 'holding', 0, 'uint');
|
|
56
|
-
modbusTCPServer.SetData(-358, 'holding', 2, 'int');
|
|
57
|
-
modbusTCPServer.SetData(3.14, 'holding', 3, 'float');
|
|
58
|
-
modbusTCPServer.SetData(0x12, 'holding', 5);
|
|
59
|
-
modbusTCPServer.SetData(0x9E36, 'holding', 6);
|
|
60
|
-
modbusTCPServer.SetData(3598.59, 'holding', 9, 'double');
|
|
61
|
-
|
|
62
|
-
modbusTCPServer.SetData(-27, 'inputs-registers', 1, 'int');
|
|
63
|
-
modbusTCPServer.SetData(-34752648, 'inputs-registers', 2, 'int32');
|
|
64
|
-
modbusTCPServer.SetData(123456789, 'inputs-registers', 5, 'uint32');
|
|
65
|
-
|
|
66
|
-
console.log(modbusTCPServer.GetData('holding',0,'uint'));
|
|
67
|
-
console.log(modbusTCPServer.GetData('holding',2,'int'));
|
|
68
|
-
console.log(modbusTCPServer.GetData('holding',3,'float'));
|
|
69
|
-
console.log(modbusTCPServer.GetData('holding',9,'double'));
|
|
70
|
-
console.log(modbusTCPServer.GetData(3,2,'int32'));
|
|
71
|
-
console.log(modbusTCPServer.GetData(3,5,'uint32'));
|
|
72
|
-
console.log(modbusTCPServer.GetData(1,7));
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
modbusTCPServer.Start();
|