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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
'use strict';
/// Web Serial API Implementation
/// https://developer.mozilla.org/en-US/docs/Web/API/SerialPort
class SerialPort {
constructor(port) {
this._port = port;
this._readLoopPromise = null;
this._reader = null;
this._writer = null;
this._initialized = false;
this._keepReading = true;
this.isConnected = false;
}
/// Connect and start reading loop
async connect(options = { baudRate: 9600 }) {
if (this._initialized) {
try {
await this.disconnect();
} catch (error) {
console.error('Error disconnecting previous port:', error);
}
if (this._readLoopPromise) {
try {
await this._readLoopPromise;
} catch (error) {
console.error('Error in read loop:', error);
}
}
this._readLoopPromise = null;
}
this._initialized = true;
this.isConnected = true;
this._keepReading = true;
try {
await this._port.open(options);
} catch (error) {
this.isConnected = false;
throw error;
}
this._readLoopPromise = this._readLoop();
}
/// Internal continuous read loop
async _readLoop() {
try {
while (this._port.readable && this._keepReading) {
this._reader = this._port.readable.getReader();
try {
while (true) {
const { value, done } = await this._reader.read();
if (done) {
// |reader| has been canceled.
break;
}
if (this.onReceive) {
this.onReceive(value);
}
}
} catch (error) {
if (this.onReceiveError) this.onReceiveError(error);
} finally {
this._reader.releaseLock();
}
}
} finally {
this.isConnected = false;
await this._port.close();
}
}
/// Stop reading and release port
async disconnect() {
this._keepReading = false;
if (this._reader) {
try {
await this._reader.cancel();
} catch (error) {
console.error('Error cancelling reader:', error);
}
this._reader.releaseLock();
}
if (this._writer) {
try {
await this._writer.abort();
} catch (error) {
console.error('Error closing writer:', error);
}
this._writer.releaseLock();
}
try {
await this._port.close();
} catch (error) {
console.error('Error closing port:', error);
}
if (this._readLoopPromise) {
try {
await this._readLoopPromise;
} catch (error) {
console.error('Error in read loop:', error);
}
}
}
/// Send data to port
send(data) {
if (!this._port.writable) {
throw new Error('Port is not writable');
}
this._writer = this._port.writable.getWriter();
if (!this._writer) {
throw new Error('Failed to get writer from port');
}
try {
return this._writer.write(data);
} finally {
this._writer.releaseLock();
}
}
async forgetDevice() {}
}
/// WebUSB Implementation
class WebUsbSerialPort {
constructor(device) {
this._device = device;
this._interfaceNumber = 0;
this._endpointIn = 0;
this._endpointOut = 0;
this.isConnected = false;
this._readLoopPromise = null;
this._initialized = false;
this._keepReading = true;
this._vendorId = device.vendorId;
this._productId = device.productId;
}
_isSameWebUsbSerialPort(webUsbSerialPort) {
return this._vendorId === webUsbSerialPort._vendorId && this._productId === webUsbSerialPort._productId;
}
/// Connect and start reading loop
async connect() {
if (this._initialized) {
try {
await this.disconnect();
} catch (error) {
console.error('Error disconnecting previous device:', error);
}
const webUsbSerialPorts = await serial.getWebUsbSerialPorts();
const webUsbSerialPort = webUsbSerialPorts.find(serialPort => this._isSameWebUsbSerialPort(serialPort));
this._device = webUsbSerialPort ? webUsbSerialPort._device : this._device;
}
this._initialized = true;
this.isConnected = true;
this._keepReading = true;
try {
await this._device.open();
if (!this._device.configuration) {
await this._device.selectConfiguration(1);
}
// Find interface with vendor-specific class (0xFF) and endpoints
for (const iface of this._device.configuration.interfaces) {
for (const alternate of iface.alternates) {
if (alternate.interfaceClass === 0xff) {
this._interfaceNumber = iface.interfaceNumber;
for (const endpoint of alternate.endpoints) {
if (endpoint.direction === 'out') this._endpointOut = endpoint.endpointNumber;
else if (endpoint.direction === 'in') this._endpointIn = endpoint.endpointNumber;
}
}
}
}
if (this._interfaceNumber === undefined) {
throw new Error('No suitable interface found.');
}
await this._device.claimInterface(this._interfaceNumber);
await this._device.selectAlternateInterface(this._interfaceNumber, 0);
// Set device to ENABLE (0x22 = SET_CONTROL_LINE_STATE, value 0x01 = activate)
await this._device.controlTransferOut({
requestType: 'class',
recipient: 'interface',
request: 0x22,
value: 0x01,
index: this._interfaceNumber,
});
} catch (error) {
this.isConnected = false;
throw error;
}
this._readLoopPromise = this._readLoop();
}
/// Internal continuous read loop
async _readLoop() {
try {
while (this._keepReading && this.isConnected) {
try {
const result = await this._device.transferIn(this._endpointIn, 16384);
if (result.data && this.onReceive) {
this.onReceive(result.data);
}
} catch (error) {
this.isConnected = false;
if (this.onReceiveError) {
this.onReceiveError(error);
}
}
}
} finally {
this.isConnected = false;
await this._device.close();
}
}
/// Stop reading and release device
async disconnect() {
this._keepReading = false;
try {
await this._device.controlTransferOut({
requestType: 'class',
recipient: 'interface',
request: 0x22,
value: 0x00,
index: this._interfaceNumber,
});
} catch (error) {
console.error('Error sending control transfer:', error);
}
await this._device.releaseInterface(this._interfaceNumber);
if (this._readLoopPromise) {
try {
await this._readLoopPromise;
} catch (error) {
console.error('Error in read loop:', error);
}
}
}
/// Send data to device
send(data) {
return this._device.transferOut(this._endpointOut, data);
}
async forgetDevice() {
await this.disconnect();
await this._device.forget();
}
}
// Utility Functions
const serial = {
isWebSerialSupported: () => 'serial' in navigator,
isWebUsbSupported: () => 'usb' in navigator,
async getSerialPorts() {
if (!this.isWebSerialSupported()) return [];
const ports = await navigator.serial.getPorts();
return ports.map(port => new SerialPort(port));
},
async getWebUsbSerialPorts() {
if (!this.isWebUsbSupported()) return [];
const devices = await navigator.usb.getDevices();
return devices.map(device => new WebUsbSerialPort(device));
},
async requestSerialPort() {
const port = await navigator.serial.requestPort();
return new SerialPort(port);
},
async requestWebUsbSerialPort() {
const filters = [
{ vendorId: 0xcafe }, // TinyUSB
{ vendorId: 0x239a }, // Adafruit
{ vendorId: 0x2e8a }, // Raspberry Pi
{ vendorId: 0x303a }, // Espressif
{ vendorId: 0x2341 }, // Arduino
];
const device = await navigator.usb.requestDevice({ filters });
return new WebUsbSerialPort(device);
}
};
|