Created
July 8, 2023 01:16
-
-
Save phstc/c404967a224ac23efafecb236942e0ad to your computer and use it in GitHub Desktop.
chrome-usb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Test</title> | |
</head> | |
<script async> | |
/* | |
relay | |
usbProductId: 24577 | |
usbVendorId: 1027 | |
barcode | |
usbProductId: 3050 | |
usbVendorId: 3118 | |
*/ | |
async function connectBarcode() { | |
// Prompt user to select any serial port. | |
const port = await navigator.serial.requestPort() | |
const ports = await navigator.serial.getPorts() | |
const { usbProductId, usbVendorId } = port.getInfo() | |
console.log({ usbProductId, usbVendorId }) | |
console.log(ports) | |
// // Wait for the serial port to open. | |
await port.open({ baudRate: 9600 }) | |
const reader = port.readable.getReader() | |
while (true) { | |
const { value, done } = await reader.read() | |
if (done) { | |
// Allow the serial port to be closed later. | |
reader.releaseLock() | |
break | |
} | |
// value is a Uint8Array. | |
const str = new TextDecoder().decode(value) | |
console.log(str) | |
} | |
} | |
let relayPort | |
async function connectRelay() { | |
// Prompt user to select any serial port. | |
relayPort = await navigator.serial.requestPort() | |
const info = relayPort.getInfo() | |
console.log({ info }) | |
await relayPort.open({ baudRate: 9600 }) | |
} | |
let lastStatus = false | |
async function toogleRelay(port) { | |
const writer = relayPort.writable.getWriter() | |
// const data = new Uint8Array([104, 101, 108, 108, 111]) // hello | |
const ON = [0xff, 0x01, 0x01] | |
const OFF = [0xff, 0x01, 0x00] | |
const status = lastStatus ? OFF : ON | |
lastStatus = !lastStatus | |
const data = new Uint8Array(status) | |
await writer.write(data) | |
// Allow the serial port to be closed later. | |
writer.releaseLock() | |
} | |
async function test() { | |
const ports = await navigator.serial.getPorts() | |
for (const port of ports) { | |
const info = port.getInfo() | |
console.log({ info }) | |
} | |
} | |
async function closeRelay(relayPort) { | |
const writer = relayPort.writable.getWriter() | |
// const data = new Uint8Array([104, 101, 108, 108, 111]) // hello | |
const OFF = [0xff, 0x01, 0x00] | |
const data = new Uint8Array(OFF) | |
await writer.write(data) | |
// Allow the serial port to be closed later. | |
writer.releaseLock() | |
} | |
async function openRelay(relayPort) { | |
const writer = relayPort.writable.getWriter() | |
// const data = new Uint8Array([104, 101, 108, 108, 111]) // hello | |
const ON = [0xff, 0x01, 0x01] | |
const data = new Uint8Array(ON) | |
await writer.write(data) | |
// Allow the serial port to be closed later. | |
writer.releaseLock() | |
} | |
async function start() { | |
let relayPort, barcodePort | |
const ports = await navigator.serial.getPorts() | |
console.log({ ports }) | |
for (const port of ports) { | |
const { usbProductId, usbVendorId } = port.getInfo() | |
if (usbProductId === 24577 && usbVendorId === 1027) { | |
relayPort = port | |
} else if (usbProductId === 3050 && usbVendorId === 3118) { | |
barcodePort = port | |
} | |
try { | |
// close if already open | |
await port.close() | |
} catch (e) {} | |
} | |
console.log('openning relay') | |
await relayPort.open({ baudRate: 9600 }) | |
console.log('openning barcode') | |
await barcodePort.open({ baudRate: 9600 }) | |
const reader = barcodePort.readable.getReader() | |
while (true) { | |
const { value, done } = await reader.read() | |
if (done) { | |
// Allow the serial port to be closed later. | |
reader.releaseLock() | |
break | |
} | |
// value is a Uint8Array. | |
const str = new TextDecoder() | |
.decode(value) | |
.replace(/(\r\n|\n|\r)/gm, '') | |
console.log({ str }) | |
if (str === '56a813a7ae18d70003000000') { | |
console.log('openning relay') | |
await openRelay(relayPort) | |
} else { | |
console.log('closing relay') | |
await closeRelay(relayPort) | |
} | |
} | |
} | |
start() | |
</script> | |
<body> | |
<button onclick="connectBarcode()">Connect Bar code</button><br /> | |
<button onclick="connectRelay()">Connect Relay</button><br /> | |
<button onclick="toogleRelay()">Toogle Relay</button><br /> | |
<button onclick="test()">Test</button><br /> | |
<button onclick="start()">Start</button><br /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment