-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetModbusData.js
More file actions
141 lines (117 loc) · 4.12 KB
/
getModbusData.js
File metadata and controls
141 lines (117 loc) · 4.12 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
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
var ModbusRTU = require("modbus-serial")
const TB = require('thingsboard_api')
const config = require('dotenv').config();
// name || TB_id
async function main(){
let options = {
TB_HOST: config.parsed.TB_HOST,
TB_PORT: config.parsed.TB_PORT,
TB_USERNAME: config.parsed.TB_USERNAME,
TB_PASSWORD: config.parsed.TB_PASSWORD,
}
await TB.createConnection(options);
collectAndPush = require("./collectAndPush")
let keys = ['ip','port','machineType']
var machines = await TB.get.allObjectsIDandKeysByType('machine','device',keys)
var ourMachines = []
for(let i=0; i<machines.length;i++){
if (typeof machines[i].ip !== 'undefined' && typeof machines[i].port !== 'undefined' ) {
machines[i].deviceID = await TB.get.getDeviceToken(machines[i].id)
ourMachines.push(machines[i]);
}
}
getModbusData(ourMachines)
}
main()
function getModbusData(machines) {
clients = []
machineData = {}
for (let i = 0; i < machines.length; i++) {
clients[i] = {
client: new ModbusRTU(),
ip: machines[i].ip,
port: machines[i].port,
id: machines[i].id,
// name || TB_id
};
machineData[machines[i].id] = { name: machines[i].name, deviceID: machines[i].deviceID }
clients[i].client.connectTCP(machines[i].ip, {port: machines[i].port})
clients[i].client.setID(i);
}
//Read machine data
setInterval(function () {
for (let i = 0; i < clients.length; i++) {
//la
clients[i].client.readHoldingRegisters(6, 2, function (err, data) {
try {
machineData[clients[i].id].la = currentToFloat(data.data)
machineData[clients[i].id].lastUpdate = new Date().getTime();
} catch (e) {
}
});
//lb
clients[i].client.readHoldingRegisters(8, 2, function (err, data) {
try {
machineData[clients[i].id].lb = currentToFloat(data.data)
machineData[clients[i].id].lastUpdate = new Date().getTime();
} catch (e) {
}
});
//lc
clients[i].client.readHoldingRegisters(0x0A, 2, function (err, data) {
try {
machineData[clients[i].id].lc = currentToFloat(data.data)
machineData[clients[i].id].lastUpdate = new Date().getTime();
} catch (e) {
}
});
//input
clients[i].client.readHoldingRegisters(0x8000, 2, function (err, data) {
try {
machineData[clients[i].id].input = data.data
machineData[clients[i].id].lastUpdate = new Date().getTime();
} catch (e) {
}
});
//output
clients[i].client.readHoldingRegisters(0xA000, 2, function (err, data) {
try {
machineData[clients[i].id].output = data.data
machineData[clients[i].id].lastUpdate = new Date().getTime();
} catch (e) {
}
});
}
}, 1000);
}
//Collected machine data
setInterval(function () {
//console.log(machineData['df2b7ae0-9473-11ea-a209-497b85c10bbd'])
if(typeof machineData !='undefined'){}
collectParseData(machineData)
}, 1000)
//Current array from modbus to float
function currentToFloat(currentArr){
let stringValue = currentArr[0] + (currentArr[1]<<16);
return toFloat(stringValue);
}
function toFloat (n) {
var sign = (n >> 31) * 2 + 1; // +1 or -1.
var exp = (n >>> 23) & 0xff;
var mantissa = n & 0x007fffff;
if (exp === 0xff) {
// NaN or Infinity
return mantissa ? NaN : sign * Infinity;
}
else if (exp) {
// Normalized value
exp -= 127;
// Add implicit bit in normal mode.
mantissa |= 0x00800000;
}
else {
// Subnormal number
exp = -126;
}
return sign * mantissa * Math.pow(2, exp - 23);
}