-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserial-and-polyfill.html
187 lines (128 loc) · 6.26 KB
/
webserial-and-polyfill.html
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
<h6> Version 0.1.1-15 </h6>
<h1 align=center>Both WebSerial and Web-Serial-Polyfill</h1>
<script>
//Global Variables
var exports = {};
</script>
<script src="https://cdn.jsdelivr.net/npm/web-serial-polyfill@1.0.13/dist/serial.js"> </script>
<button id="connectButton">Connect via Serial Port</button>
<input type=text id="mySendText" value="a">
<input type=button value="send" onclick="{ mySendIt(document.getElementById('mySendText').value) }"><br><br>
<input type=button value="send 'a' LED On" onclick="{mySendIt('a')}"><br><br>
<input type=button value="send 'b' LED Off" onclick="{mySendIt('b')}"><br><br>
<div id="target">...</div> <br><br>
<div id="myDiv01">...</div> <br><br><br>
<script>
/////////////////////////////////////// webSerial code first ///////////////////////////////////////////
//Global Variables
//let writer;
let port
document.getElementById('connectButton').addEventListener('click', () => {
if (navigator.serial) {
connectSerial();
} else {
document.getElementById('myDiv01').innerHTML = 'Web Serial API not supported. Switching to Polyfill<br>'
myPoly()
}
});
async function connectSerial() {
const log = document.getElementById('target');
try {
port = await navigator.serial.requestPort();
await port.open({ baudRate: 9600 });
const decoder = new TextDecoderStream();
port.readable.pipeTo(decoder.writable);
//setup reader stream
const inputStream = decoder.readable;
const reader = inputStream.getReader();
//setup writer stream
// writer = port.writable.getWriter();
while (true) {
const { value, done } = await reader.read();
if (value) {
log.textContent += value + '\n';
}
if (done) {
console.log('[readLoop] DONE', done);
reader.releaseLock();
break;
}
}
} catch (error) {
log.innerHTML = error;
}
}
async function mySendIt(myData) {
if (navigator.serial) {
const encoder = new TextEncoder();
const writer = port.writable.getWriter();
await writer.write(encoder.encode(myData));
writer.releaseLock();
} else {
mySend(myData)
}
}
///////////////////////////////////////// end webSerial //////////////////////////////////////////
///////////////////////////////////////// start polyfill for Android ////////////////////////////
//Global Variables for Polyfill
var serial = exports.serial; // is this needed as defined in the npm
let myLooping // for read setinterval
var mySerial;
//let receivedText = '';
let reader = {};
let writer = {};
str2ab = function(str) {
var buf = new Uint8Array(str.length); // 1 byte for each char
for (var i=0, strLen=str.length; i < strLen; i++) {
buf[i] = str.charCodeAt(i);
}
return buf;
}
ab2str = function(buf) {
return String.fromCharCode.apply(null, buf);
}
async function myRead(){
reader.read().then(({value}) => {
let receivedText = ab2str(value);
document.getElementById('target').innerHTML = receivedText + '<br>' + document.getElementById('target').innerHTML // latest on top
},
error => {
console.error('error from read', error)
document.getElementById('myDiv01').innerHTML = 'error from read' + error
}
);
}
async function myPoly(){
mySerial = await serial.requestPort()
document.getElementById('myDiv01').innerHTML += await '<b>mySerial: </b><br><pre>' + JSON.stringify(mySerial, null, 3) + '</pre><br><br>'
console.log('mySerial')
console.log(mySerial)
const myOpen = await mySerial.open({baudRate: 115200});
reader = mySerial.readable.getReader();
writer = mySerial.writable.getWriter();
const results = mySerial.getInfo();
//document.getElementById('myDiv01').innerHTML += await 'Results:<b>: </b><br><pre>' + JSON.stringify(results, null, 3) + '</pre><br><br>'
console.log('get info results', results);
document.getElementById('myDiv01').innerHTML += 'results.usbVendorId: ' + results.usbVendorId + '<br>'
document.getElementById('myDiv01').innerHTML += 'results.usbProductId: ' + results.usbProductId + '<br>'
// start looping the serial read. Is there a better way to do this?
clearInterval(myLooping)
myLooping = setInterval(myRead, 1000);
}
async function mySend(myData2){
writer.ready.then(() => {
let inputArrayBuffer = str2ab(myData2);
const myWritten = writer.write(inputArrayBuffer);
console.log('myWritten')
console.log(myWritten)
//document.getElementById('myDiv01').innerHTML = '<br><br><b>myWriter: </b><br><pre>' + JSON.stringify(myWritten, null, 3) + '</pre><br><br>'
})
}
/////////////////////////////////////// end Polyfill for Android ////////////////////////////////
</script>
Here is the latest sketch I am using. The .txt is for viewing the .ino is to download<br>
<li><a href="arduino04-webserial.txt">arduino04-webserial.txt</a> This arduino sketch can be viewed
<li><a href="arduino04-webserial.ino">arduino04-webserial.ino</a> Click to download: Note: New ArduinoWebSerial testing code. Make sure in Arduino Serial Monitor you don't send a new-line
This Github file is at <a href="https://github.com/hpssjellis/my-examples-of-arduino-webUSB-webSerial/blob/main/public/webserial-and-polyfill.html">https://github.com/hpssjellis/my-examples-of-arduino-webUSB-webSerial/blob/main/public/webserial-and-polyfill.html</a>
And most of my original work was done at <a href="https://github.com/hpssjellis/web-serial-polyfill">https://github.com/hpssjellis/web-serial-polyfill</a>
<p><small>Original Demo from <a href="https://codelabs.developers.google.com/codelabs/web-serial/" target="_blank" rel="noopener">Google Developers</a> codelabs.</small></p>