-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmServer.js
More file actions
39 lines (31 loc) · 1.08 KB
/
mServer.js
File metadata and controls
39 lines (31 loc) · 1.08 KB
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
'use strict';
const logger = require('./utils/logger.js')('mServer');
const modbus = require('modbus-tcp');
const driveAdaptor = require('./drives/driveAdaptor.js');
/**
* @param {string} driveName
* @param {number} mPort
* @param {string|undefined} mHost
*/
module.exports = function(driveName, mPort, mHost) {
const net = require('net');
net.createServer(function(socket) {
logger.info('New connection.');
const mServer = new modbus.Server();
socket.on('error', function(err) {
console.error(err);
socket.destroy();
});
mServer.writer().pipe(socket);
socket.pipe(mServer.reader());
mServer.on('read-holding-registers', function(from, to, reply) {
const val = driveAdaptor.getBuffers(from, to);
return reply(null, val);
});
}).on('error', function(err) {
console.error(err);
}).listen(mPort, mHost, function() {
console.log('Listening Modbus TCP for ' + driveName + ' on ' + ((mHost)? mHost : 'all_interfaces' ) + ':' + mPort);
});
return {}
};