-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.js
More file actions
71 lines (52 loc) · 1.22 KB
/
serial.js
File metadata and controls
71 lines (52 loc) · 1.22 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
class SerialRS232 extends Terminal {
constructor(args) {
super();
this.args = Object.assign({
ansi: true,
autoScroll: true,
bell: false,
smoothCursor: false
}, args);
this.SetTitle("Serial RS232");
this.SetIcon("mono/serialconsole.svg");
this.Connect();
}
Close() { //override
if (this.port) {
this.port.close();
this.port = null;
}
super.Close();
}
async Connect() {
let reader;
let writer;
this.port = await navigator.serial.requestPort();
console.log(this.port);
await this.port.open({
baudRate: 9600,
dataBits: 8,
stopBits: 1,
parity: "none",
flowControl: "none"
});
// Get reader and writer
const decoder = new TextDecoderStream();
this.port.readable.pipeTo(decoder.writable);
reader = decoder.readable.getReader();
console.log(decoder);
const encoder = new TextEncoderStream();
encoder.readable.pipeTo(this.port.writable);
writer = encoder.writable.getWriter();
console.log(encoder);
// Example: write to serial
await writer.write("\n");
await writer.write("\n");
// Example: read from serial
while (true) {
const { value, done } = await reader.read();
if (done) break;
console.log("Received:", value);
}
}
}