This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialpkt.js
144 lines (129 loc) · 2.97 KB
/
serialpkt.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
const serial_start = '02';
const serial_stop = '03';
const serial_id_enable = 'f0';
const serial_id_control = 'f1';
const serial_id_kill = 'f2';
const serial_id_data_len_1 = 'e1';
const serial_id_data_len_2 = 'e2';
const serial_id_data_len_3 = 'e3';
const enable_ack = 'ENABLE_ACK';
const control_ack = 'CONTROL_ACK';
const kill_ack = 'KILL_ACK';
function SerialPkt() {
this.start = false;
this.id = null;
this.data_len = null;
this.data = []; //for control data
this.crc = null;
this.parsed = null;
this.stop = false;
this.reset = function () {
// console.log('reset parser vars');
this.start = false;
this.id = null;
this.data_len = null;
this.data = [];
this.crc = null;
this.parsed = null;
this.stop = false;
};
this.addbyte = function (byte) {
let res;
if (this.start == false) {
res = this.getStart(byte);
} else if (this.id == null) {
res = this.getID(byte);
} else if (this.id == control_ack && this.data_len == null) {
res = this.getDataLength(byte);
} else if (this.id == control_ack && this.data_len > this.data.length) {
res = this.getData(byte);
} else if (this.crc == null) {
res = this.getCRC(byte);
} else if (this.stop == false) {
res = this.getStop(byte);
}
if (res != true) {
console.log('Error during Parsing: ', res);
this.reset();
}
};
this.getStart = function (byte) {
if (byte == serial_start) {
this.start = true;
return true;
} else {
return 'invalid start byte';
}
};
this.getID = function (byte) {
if (byte == serial_id_enable) {
this.id = enable_ack;
} else if (byte == serial_id_control) {
this.id = control_ack;
} else if (byte == serial_id_kill) {
this.id = kill_ack;
} else {
return 'invalid id byte';
}
return true;
};
this.getDataLength = function (byte) {
if (byte == serial_id_data_len_1) {
this.data_len = 1;
} else if (byte == serial_id_data_len_2) {
this.data_len = 2;
} else if (byte == serial_id_data_len_3) {
this.data_len = 3;
} else {
return 'invalid data length';
}
return true;
};
this.getData = function (byte) {
let percent = parseInt(byte, 16);
percent = (percent / 0xff) * 100;
percent = percent.toFixed(2);
percent += '% ';
this.data.push(percent);
return true;
};
this.getCRC = function (byte) {
this.crc = 'no implementation';
return true;
};
this.getStop = function (byte) {
if (byte == serial_stop) {
this.stop = true;
return true;
} else {
return 'invalid stop byte';
}
};
this.getParsedPkt = function () {
if (this.stop == true) {
let res;
if (this.id == enable_ack || this.id == kill_ack) {
res = this.id;
} else if (this.id == control_ack) {
res = `${this.id} [${this.data}]`;
} else {
return 'parser err';
}
this.reset();
return res;
} else {
return '';
}
};
}
module.exports = {
SerialPkt,
serial_start,
serial_stop,
serial_id_enable,
serial_id_control,
serial_id_kill,
serial_id_data_len_1,
serial_id_data_len_2,
serial_id_data_len_3,
};