-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
103 lines (86 loc) · 2.47 KB
/
app.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
let port;
var keepReading = false;
var reader;
var inputDone;
var outputDone;
var outputStream;
window.onload = function() {
if(!detectBrowser()){
alert("Please Note\nTo access the Serial Port through the Web Serial API, you must use Google Chrome browser or Microsoft Edge for now.");
}
};
function startSerial() {
openSerial()
}
async function closeSerial() {
keepReading = false;
reader.cancel();
await inputDone.catch(() => {});
outputStream.close();
await outputDone;
await port.close();
window.SuccessMsg("Serial Port Closed", "The connected serial port is closed properly.");
window.setSerialStatus(false);
}
function alertMessage(text) {
alert(text)
}
function detectBrowser() {
if(navigator.userAgent.indexOf("Chrome") != -1 ) {
return true;
}else {
return false;
}
}
async function openSerial() {
try {
port = await navigator.serial.requestPort({});
await port.open({ baudRate: 115200, bufferSize: 10000 });
const encoder = new TextEncoderStream();
outputDone = encoder.readable.pipeTo(port.writable);
outputStream = encoder.writable;
let decoder = new TextDecoderStream();
inputDone = port.readable.pipeTo(decoder.writable);
inputStream = decoder.readable.pipeThrough(
new TransformStream(new LineBreakTransformer())
);
reader = inputStream.getReader();
keepReading = true;
readLoop();
} catch (e) {
window.ErrorMsg("Serial Error", e.toString());
}
}
async function readLoop() {
window.SuccessMsg("Serial Read", "Begin to read data from the connected serial port.");
window.setSerialStatus(true);
while (keepReading) {
try{
const { value, done } = await reader.read();
if (value) {
window.readSerialFunc(value);
if (done) {
reader.releaseLock();
break;
}
}
}catch(e){
keepReading = false;
window.ErrorMsg("Serial Error", e.toString());
}
}
}
class LineBreakTransformer {
constructor() {
this.container = "";
}
transform(chunk, controller) {
this.container += chunk;
const lines = this.container.split("\r\n");
this.container = lines.pop();
lines.forEach((line) => controller.enqueue(line));
}
flush(controller) {
controller.enqueue(this.container);
}
}