forked from anerdins/nibepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus-client.js
executable file
·171 lines (156 loc) · 7.2 KB
/
modbus-client.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
const getQueue = [];
var regQueue = [30001];
var red = false;
var getTimer = {};
var setTimer = {};
async function writeData(item) {
let address = item.register;
const promise = new Promise((resolve,reject) => {
let register = Number(address)-40000;
let size = 0xFFFF;
if(item.size=="u8" || item.size=="s8") size = 0xFF;
if(item.size=="u32" || item.size=="s32") size = 0xFFFFFFFF;
process.send({type:"log",data:JSON.stringify(item,null,2),level:"core",kind:"WRITE"});
setTimer[address] = setTimeout((address) => {
process.send({type:"log",data:"5 sec timeout from register: "+address,level:"core",kind:"ERROR"});
reject(new Error('No respond when writing to register ('+address+')'));
}, 5000, address);
client.writeRegisters(register, [item.data,size])
.then(data => {
clearTimeout(setTimer[address]);
console.log(data);
resolve(data)
});
}).catch((err => {
clearTimeout(getTimer[address]);
reject(err)
}));
return promise;
}
async function requestData(address) {
const promise = new Promise((resolve,reject) => {
process.send({type:"log",data:"Requesting data from register: "+address,level:"core",kind:"REQ"});
if(address.toString().charAt(0)=="3") {
let register = Number(address)-30000;
// Get input register
if(client!==undefined) {
getTimer[address] = setTimeout((address) => {
process.send({type:"log",data:"5 sec timeout from register: "+address,level:"core",kind:"ERROR"});
reject(new Error('No respond from register ('+address+')'));
}, 5000, address);
client.readInputRegisters(register, 1, function(err, data) {
clearTimeout(getTimer[address]);
if(err) {
reject(new Error("Could not read data from register"))
} else if(data!==undefined) {
if(process.connected===true) {
process.send({type:"log",data:"Got data from register: "+address+", "+JSON.stringify(data),level:"core",kind:"SUCCESS"});
process.send({type:"data",data:{register:address,data:data.data}});
resolve(data.data)
//process.send({type:"log",data:data.data,level:"debug",kind:"OK"});
} else {
reject(new Error("Core has been disconnected"))
}
} else {
reject(new Error("Could not read data from register"))
}
});
} else {
reject(new Error("Heatpump not connected yet"))
}
} else if(address.toString().charAt(0)=="4") {
// Get holding register
let register = Number(address)-40000;
if(client!==undefined) {
getTimer[address] = setTimeout((address) => {
process.send({type:"log",data:"5 sec timeout from register: "+address,level:"core",kind:"ERROR"});
reject(new Error('No respond from register ('+address+')'));
}, 5000, address);
client.readHoldingRegisters(register, 1, function(err, data) {
clearTimeout(getTimer[address]);
if(err) {
reject(new Error("Could not read data from register"))
} else if(data!==undefined) {
if(process.connected===true) {
process.send({type:"log",data:"Got data from register: "+address+", "+JSON.stringify(data),level:"core",kind:"SUCCESS"});
process.send({type:"data",data:{register:address,data:data.data}});
resolve(data.data)
//process.send({type:"log",data:data.data,level:"debug",kind:"OK"});
} else {
reject(new Error("Core has been disconnected"))
}
} else {
reject(new Error("Could not read data from register"))
}
});
} else {
reject(new Error("Heatpump not connected yet"))
}
} else {
reject(new Error("Can't request register which is not in the 30000-49999 range"))
}
});
return promise;
}
process.on('message', (m) => {
if(m.start===true) {
// open connection to a tcp line
client.connectTCP(m.host, { port: m.port });
client.setID(1);
setTimeout(async () => {
process.send({type:"started",data:true});
async function startLoop() {
var len = regQueue.length;
for( var i = 0; i < regQueue.length; i++){
if(getQueue!==undefined && getQueue.length!==0) {
var lastMsg = getQueue.pop();
await requestData(lastMsg).catch((err) => {
process.send({type:"log",data:JSON.stringify(err),level:"core",kind:"ERROR"});
});
i--;
} else {
await requestData(regQueue[i]).catch((err) => {
process.send({type:"log",data:JSON.stringify(err),level:"core",kind:"ERROR"});
});;
}
if(len!==regQueue.length) {
process.send({type:"log",data:'Starting over the regQueue again, register added',level:"core",kind:"LOOP"});
i = regQueue.length;
startLoop();
} else {
if(i===regQueue.length-1) i=-1;
process.send({type:"log",data:JSON.stringify(regQueue,null,2),level:"core",kind:"LOOP"});
}
}
}
startLoop();
}, 2000);
} else if(m.type=="reqData") {
getQueue.unshift(m.data);
} else if(m.type=="regRegister") {
regQueue = m.data;
} else if(m.type=="setData") {
let item = m.data;
writeData(item)
.then(data => {
if(process.connected===true) {
process.send({type:"ack",data:{register:item.register,ack:true}});
}
}).catch(err => {
if(process.connected===true) {
process.send({type:"ack",data:{register:item.register,ack:false}});
}
})
} else if(m.type=="red") {
//red = m.data;
}
});
process.on('disconnect', (m) => {
if(red===false) {
console.log('Shutting down the core.')
process.exit(99);
}
});