forked from ZHONG007/node-modbus-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·53 lines (48 loc) · 1.69 KB
/
server.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
/* eslint-disable no-console, no-unused-vars, spaced-comment */
// create an empty modbus client
//var ModbusRTU = require("modbus-serial");
var ModbusRTU = require("../index");
var vector = {
getInputRegister: function(addr) { return addr; },
getHoldingRegister: function(addr) { return addr + 8000; },
getMultipleInputRegisters: function(startAddr, length) {
var values = [];
for (var i = 0; i < length; i++) {
values[i] = startAddr + i;
}
return values;
},
getMultipleHoldingRegisters: function(startAddr, length) {
var values = [];
for (var i = 0; i < length; i++) {
values[i] = startAddr + i + 8000;
}
return values;
},
getCoil: function(addr) { return (addr % 2) === 0; },
setRegister: function(addr, value) { console.log("set register", addr, value); return; },
setCoil: function(addr, value) { console.log("set coil", addr, value); return; },
readDeviceIdentification: function(addr) {
return {
0x00: "MyVendorName",
0x01: "MyProductCode",
0x02: "MyMajorMinorRevision",
0x05: "MyModelName",
0x97: "MyExtendedObject1",
0xAB: "MyExtendedObject2"
};
}
};
// set the server to answer for modbus requests
console.log("ModbusTCP listening on modbus://0.0.0.0:8502");
var serverTCP = new ModbusRTU.ServerTCP(vector, { host: "0.0.0.0", port: 8502, debug: true, unitID: 1 });
serverTCP.on("initialized", function() {
console.log("initialized");
});
serverTCP.on("socketError", function(err) {
console.error(err);
serverTCP.close(closed);
});
function closed() {
console.log("server closed");
}