-
Notifications
You must be signed in to change notification settings - Fork 0
/
webusb.js
119 lines (101 loc) · 3.06 KB
/
webusb.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
document.addEventListener('DOMContentLoaded', event => {
let button = document.getElementById('connect')
button.addEventListener('click', async() => {
let device
const VENDOR_ID = 0x10C4
const config = new Uint8Array(10)
const setPortConfig = {
requestType: 'vendor',
recipient: 'device',
request: 0x05,
value: 0x00,
index: 0x03
}
const openPort = {
requestType: 'vendor',
recipient: 'device',
request: 0x06,
value: 0x89,
index: 0x03
}
const startPort = {
requestType: 'vendor',
recipient: 'device',
request: 0x08,
value: 0x00,
index: 0x03
}
const closePort = {
requestType: 'vendor',
recipient: 'device',
request: 0x07,
value: 0x00,
index: 0x03
}
async function close () {
let result = await device.controlTransferOut(closePort)
console.log('close port:', result)
await device.releaseInterface(0)
await device.close()
}
try {
device = await navigator.usb.requestDevice({
filters: [{
vendorId: VENDOR_ID
}]
})
console.log('open')
await device.open()
console.log('opened:', device)
console.log('configurations:', device.configurations)
if (device.configuration === null) {
console.log('selectConfiguration')
await device.selectConfiguration(2)
}
console.log('interfaces:', device.configuration.interfaces)
console.log('claimInterface')
await device.claimInterface(0)
console.log('set up port')
let result = await device.controlTransferOut(openPort)
console.log('open port:', result)
result = await device.controlTransferOut(startPort)
console.log('start port:', result)
config.set([
0x00, 0x30, // baud rate (19200 : 0x0030)
0x60, 0x00, // flags ¯\_(ツ)_/¯
0x03, // data bits (8 : 0x03)
0x00, // parity (none : 0)
0x00, // stop bits (none : 0)
0x11, // xon (false : 0)
0x13, // xoff (false : 0)
0x00 // UART mode (RS-232 : 0)
])
result = await device.controlTransferOut(setPortConfig, config)
console.log('set port config:', result)
const data = new Uint8Array(3)
data.set([0x6d, 0x65, 0x6d])
result = await device.transferOut(0x01, data.buffer)
console.log('mem:', result)
const timeoutID = window.setTimeout(async() => {
console.warn('Device not connected')
await close()
}, 5000)
console.log('Receiving...')
while (true) {
let incoming = await device.transferIn(0x01, 1024)
if (incoming.data.byteLength > 0) {
clearTimeout(timeoutID)
let decoder = new TextDecoder() // eslint-disable-line no-undef
const data = decoder.decode(incoming.data)
console.log(data)
if (data.includes('END')) {
break
}
}
}
await close()
} catch (error) {
console.log(error)
}
})
})