-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
107 lines (91 loc) · 2.5 KB
/
script.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
// /*
// * Webserial basic demo
// */
'use strict';
// Global variables
let port = null; //object
let outputDone;
let outputStream;
let reader;
let inputDone;
let inputStream;
// check if broswer supports webserial
function check_compatability() {
if ('serial' in navigator) {
console.log('webserial supported');
return true
} else {
console.log('webserial not supported');
return false
}
}
// Disconnect serial connection
async function disconnect() {
if (reader) { //close input stream
await reader.cancel();
await inputDone.catch(() => {});
reader = null;
inputDone = null;
}
if (outputStream) { //close output stream
await outputStream.getWriter().close();
await outputDone;
outputStream = null;
outputDone = null;
}
await port.close();
port = null;
console.log('port closed');
};
// Connect serial port
async function connect() {
try { //Prompt user to pick serial (COM) port
port = await navigator.serial.requestPort();
try { //If successful, proceed to open COM port and enable read & write streams
await port.open({ 'baudRate': 9600 });
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;
reader = inputStream.getReader();
return true;
} catch (err) { //Could not open port
console.error('Error opening port:', err)
}
}
catch (err) { //User closed/cancelled out of prompt
console.error('User cancelled selection:', err)
}
return false;
}
// Write a character to the stream (and the port)
function writeStream(sendString) {
const writer = outputStream.getWriter();
console.log('[SENDING]', sendString);
writer.write(sendString + '\n');
writer.releaseLock();
}
// Monitor the port for incoming data. Fire off a custom event
// every time a temperature reading is successfully decoded.
async function readLoop() {
let fullStr = '';
while (true) {
const { value, done } = await reader.read();
if (value) {
fullStr += value;
// Could proably just check the last character instead...
const res = fullStr.match(/(.*)\r\n/);
if (res) {
fullStr = '';
emitEvent('readevent', res[1]);
}
}
if (done) {
console.log('[readLoop] DONE', done);
reader.releaseLock();
break;
}
}
}