-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodbus-browser-read-coils.js
77 lines (69 loc) · 2.86 KB
/
modbus-browser-read-coils.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
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
const _ = require('lodash');
const program = require('commander');
const hexdump = require('hexdump-nodejs');
const Chain = require('middleware-chain-js');
const chalk = require('chalk');
// Instanciating the middleware chain.
const chain = new Chain();
/**
* Command-line interface.
*/
program
.name('modbus-browser read-coils')
.description('Reads the value of the coils between the given start and end address.')
.option('-s, --server <hostname>', 'The hostname or IP address of the Modbus server to initiate a connection to.')
.option('-p, --port <port>', 'The port of the Modbus server to initiate a connection to (set to `502` by default).')
.option('-u, --unit-id <unitId>', 'The unit identifier to perform the action on (set to `1` by default).')
.option('-a, --start-address <address>', 'The start address of the coil(s) to be read.')
.option('-c, --count <count>', 'The amount of bits to be read.')
.option('-m, --monitor', 'Causes the `mobus-browser` to continuously monitor the coils at the given address.')
.option('-i, --interval <interval>', 'Specifies the interval in milliseconds at which `mobus-browser` is continuously dumping the coils values when monitoring is enabled.')
.parse(process.argv);
/**
* Injecting the initialization routines into the `chain`.
*/
chain.use(require('./lib/middlewares/initialization-routines'));
/**
* Verifying that the given arguments are valid.
*/
chain.use((input, output, next) => {
if (!_.isString(program.server)) {
return (output.fail(`The hostname or IP address of the Modbus server to connect to is expected.`));
}
if (!_.isString(program.startAddress) || !_.isString(program.count)) {
return (output.fail(`The start address and the count options must be valid numbers.`));
}
if (_.isString(program.unitId) && isNaN(parseInt(program.unitId))) {
return (output.fail(`The unit identifier must be a valid number.`));
}
next();
});
/**
* Injecting the connection routines into the `chain`.
*/
chain.use(require('./lib/middlewares/connection-routines'));
/**
* Reading the coils
*/
chain.use((input, output) => {
const intervalFn = program.monitor ? setInterval : setTimeout;
const interval = program.monitor ? (program.interval || 2000) : 0;
// Reading the coils between the given addresses.
intervalFn(() => {
input.client.readCoils(
parseInt(program.startAddress),
parseInt(program.count)
).then((res) => {
// Dumping the date at which the response was received.
console.log(`Received at : ${chalk.underline.white.bold(res.metrics.receivedAt)}`);
// Dumping the hexdump of the values.
console.log(`\n${hexdump(res.response._body._coils)}\n`);
if (!program.monitor) {
// Closing the socket.
input.socket.destroy();
}
}).catch(output.fail);
}, interval);
});
// Triggering the `chain`.
chain.handle({}, {});