-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcpServer.js
48 lines (35 loc) · 1.73 KB
/
tcpServer.js
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
var Net = require('net');
function TCPModbusServerRaw(options, callbackRequest) {
var context = this;
context.port = options.port?options.port:502;
Net.createServer(function (socket) {
socket.namePad = (' ' + socket.remoteAddress + ":" + socket.remotePort).slice(-26);
console.log(new Date().toISOString() + socket.namePad + ' client connected');
socket.on('data', function (request) {
console.log(new Date().toISOString() + socket.namePad + '->' + request.toString('hex'));
var tcpHeader = request.slice(0,6);
callbackRequest(request.slice(6,request.length), function(reply) {
var buf = Buffer.concat([tcpHeader, reply]);
buf.writeUInt16BE(reply.length, 4);
socket.write(buf);
console.log(new Date().toISOString() + socket.namePad + '<-' + buf.toString('hex'));
});
});
socket.on('end', function () {
console.log(new Date().toISOString() + socket.namePad + ' client disconnected');
});
socket.on('close', function () {
console.log(new Date().toISOString() + socket.namePad + ' connection closed');
});
socket.on('error', function () {
console.log(new Date().toISOString() + socket.namePad + ' socket error');
});
socket.on('timeout', function () {
console.log(new Date().toISOString() + socket.namePad + ' connection timeout');
socket.destroy();
});
socket.setTimeout(10000);
}).listen(context.port);
console.log(new Date().toISOString() + ' Modbus TCP server is waiting for connections at 502 port');
}
module.exports = TCPModbusServerRaw;