forked from Ursalink-CN/ursalink-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uc11-n1.js
132 lines (118 loc) · 3.98 KB
/
uc11-n1.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
// N1: Payload Decoder
function Decoder(bytes, port) {
var decoded = {};
for (i = 0; i < bytes.length;) {
// BATTERY
if (bytes[i] == 0x01) {
decoded.battery = bytes[i + 2];
i += 3;
continue;
}
// GPIO
if (bytes[i] == 0x03) {
decoded.gpio1 = bytes[i + 2] === 0 ? "off" : "on";
i += 3;
continue;
}
if (bytes[i] == 0x04 && bytes[i + 1] !== 0xc8) {
decoded.gpio2 = bytes[i + 2] === 0 ? "off" : "on";
i += 3;
continue;
}
if (bytes[i] == 0x04 && bytes [i + 1] == 0xc8){
//Pulse Counter
decoded.counter = readUInt32LE(bytes.slice(i + 2, i + 6)) ;
i += 6;
continue;
}
// ADC
if (bytes[i] == 0x05) {
decoded.adc1 = {};
decoded.adc1.cur = readInt16LE(bytes.slice(i + 2, i + 4)) / 100;
decoded.adc1.min = readInt16LE(bytes.slice(i + 4, i + 6)) / 100;
decoded.adc1.max = readInt16LE(bytes.slice(i + 6, i + 8)) / 100;
decoded.adc1.avg = readInt16LE(bytes.slice(i + 8, i + 10)) / 100;
i += 10;
continue;
}
if (bytes[i] == 0x06) {
decoded.adc2 = {};
decoded.adc2.cur = readInt16LE(bytes.slice(i + 2, i + 4)) / 100;
decoded.adc2.min = readInt16LE(bytes.slice(i + 4, i + 6)) / 100;
decoded.adc2.max = readInt16LE(bytes.slice(i + 6, i + 8)) / 100;
decoded.adc2.avg = readInt16LE(bytes.slice(i + 8, i + 10)) / 100;
i += 10;
continue;
}
// MODBUS
if (bytes[i] == 0xFF && bytes[i + 1] == 0x0E) {
var chnId = bytes[i + 2];
var packageType = bytes[i + 3];
var dataType = packageType & 7;
var dataLength = packageType >> 3;
var chn = 'chn' + chnId;
switch (dataType) {
case 0:
decoded[chn] = bytes[i + 4] ? "on" : "off";
i += 5;
break;
case 1:
decoded[chn] = bytes[i + 4];
i += 5;
break;
case 2:
case 3:
decoded[chn] = readUInt16LE(bytes.slice(i + 4, i + 6));
i += 6;
break;
case 4:
case 6:
decoded[chn] = readUInt32LE(bytes.slice(i + 4, i + 8));
i += 8;
break;
case 5:
case 7:
decoded[chn] = readFloatLE(bytes.slice(i + 4, i + 8));
i += 8;
break;
}
}
}
return decoded;
}
/* ******************************************
* bytes to number
********************************************/
function readUInt8LE(bytes) {
return (bytes & 0xFF);
}
function readInt8LE(bytes) {
var ref = readUInt8LE(bytes);
return (ref > 0x7F) ? ref - 0x100 : ref;
}
function readUInt16LE(bytes) {
var value = (bytes[1] << 8) + bytes[0];
return (value & 0xFFFF);
}
function readInt16LE(bytes) {
var ref = readUInt16LE(bytes);
return (ref > 0x7FFF) ? ref - 0x10000 : ref;
}
function readUInt32LE(bytes) {
var value = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];
return (value & 0xFFFFFFFF);
}
function readInt32LE(bytes) {
var ref = readUInt32LE(bytes);
return (ref > 0x7FFFFFFF) ? ref - 0x100000000 : ref;
}
function readFloatLE(bytes) {
// JavaScript bitwise operators yield a 32 bits integer, not a float.
// Assume LSB (least significant byte first).
var bits = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
var sign = (bits >>> 31 === 0) ? 1.0 : -1.0;
var e = bits >>> 23 & 0xff;
var m = (e === 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;
var f = sign * m * Math.pow(2, e - 150);
return f;
}