-
Notifications
You must be signed in to change notification settings - Fork 0
/
native.serial.js
218 lines (194 loc) · 5.19 KB
/
native.serial.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* global __ser, VM, _serial, vm */
var Serial = function () {
var T = this, A = arguments;
const api = _serial;
T._port = null;
T._baud;
T.active = false;
T.id = null;
T.AutoFree = false;
T.inTmr = null;
T.outTmr = null;
T.reading = false;
T.timeout = 30; // stop read timer if nothing received in xx sec
T.tts = 1000; // time to stabilise
T.interval = 25; // cycle interval, reduce to process faster
T.cache = 30; // sec
var pbuf = [];
T.lastenum = 0;
T.rec = 0;
T.wrt = 0;
T.rdy = false;
T.recent = null;
var sbuf = [];
T.rbuf = "";
const init = function (port, baud) {
T.id = api.open(port, baud);
if (T.id > -1) {
T._port = port;
T._baud = baud;
T.active = true;
} else {
return false;
}
T.lt = time(true);
T._baud = baud;
T._port = port;
T.outTmr = setInterval(function () {
if (!T.rdy) {
if ((time(true) - T.lt) > T.tts) {
T.rdy = true;
}
} else {
for (var i = 0; i < sbuf.length; i++) {
if (typeof sbuf[i] === "undefined") {
continue;
}
if (sbuf[i].sent === false) {
sbuf[i].cb(api.write(T.id, sbuf[i].data));
sbuf[i].sent = true;
} else {
delete sbuf[i];
}
}
}
}, T.interval);
vm.release(function () {
T.close();
});
return true;
};
T.open = function (port, baud) {
if (!port.startsWith('COM')) {
T.find(port, function (p) {
if (p !== null && p.busy === false) {
return init(p.port, baud);
}
return false;
});
} else {
return init(port, baud);
}
};
T.available = function () {
if (T.active === false) {
return false;
}
if (api.inbuf(T.id) > 0) {
return true;
}
return false;
};
T.close = function () {
if (T.active === false) {
return false;
}
if (T.reading) {
T.stop();
}
clearInterval(T.outTmr);
api.close(T.id);
T._port = null;
T._baud = null;
T.active = false;
T.id = null;
if (T.AutoFree === true) {
delete T;
}
};
T.read = function () {
if (T.active === false) {
return "";
}
return api.read(T.id);
};
T.write = function (q, cb) {
if (T.active === false) {
return false;
}
if (typeof cb === "boolean" && cb === true) {
api.write(T.id, q);
return true;
}
if (typeof cb === "undefined") {
cb = function () {};
}
sbuf.push({
data: q,
sent: false,
cb: cb
});
return true;
};
T.start = function (cb, delimiter) {
if (T.active === false) {
return false;
}
if (T.reading === true) {
return false;
}
T.rec = time();
if (!isset(delimiter)) {
delimiter = "\n";
}
T.inTmr = setInterval(function () {
if (T.available() === false) {
return;
}
T.rbuf += T.read();
if (T.rbuf === "") {
return;
}
var str = "";
T.rbuf.split("").forEach(function (c) {
if (((c === delimiter))) {
if (str !== "") {
cb(str.clean());
}
str = "";
} else {
str += c;
}
});
T.rbuf = str;
var t = time();
if ((t - T.rec) > T.timeout) {
T.stop();
}
T.rec = t;
}, T.interval);
T.reading = true;
return true;
};
T.stop = function () {
if (T.reading === false) {
return false;
}
clearInterval(T.inTmr);
T.reading = false;
};
T.enum = function () {
pbuf = JSON.parse(api.enum());
return pbuf;
};
T.find = function (d, cb) {
T.enum(function (arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].desc.toString().indexOf(d) !== -1) {
cb(arr[i]);
return;
}
}
cb(null);
});
};
if (A.length === 2) {
if (T.open(A[0], A[1]) === true) {
T.AutoFree = true;
return T;
} else {
return null;
}
}
return T;
};