feat: added list command

This commit is contained in:
Sergey Yarkov 2022-09-20 00:25:44 +03:00
parent d3a5aadcd4
commit 956a4dd21c

View File

@ -1,12 +1,15 @@
class SerialPortHandler { class SerialPortHandler {
constructor(options) { constructor(options, onConnect, onDisconnect) {
this.encoder = new TextEncoder(); this.encoder = new TextEncoder();
this.decoder = new TextDecoder(); this.decoder = new TextDecoder();
this.onConnect = onConnect;
this.onDisconnect = onDisconnect;
this.options = options; this.options = options;
this.port = null; this.port = null;
this.writer = null; this.writer = null;
this.reader = null; this.reader = null;
this.isOpened = false; this.isOpened = false;
this.#setupListeners();
} }
async open() { async open() {
@ -36,19 +39,24 @@ class SerialPortHandler {
async write(data) { async write(data) {
const encoded = this.encoder.encode(data); const encoded = this.encoder.encode(data);
return await this.writer.write(encoded); await this.writer.write(encoded);
} }
async read() { async read() {
let data = '';
while (true) { while (true) {
const { value, done } = await this.reader.read(); const { value, done } = await this.reader.read();
if (done) { if (done) {
this.reader.releaseLock(); this.reader.releaseLock();
break; break;
} }
data += this.decoder.decode(value); console.log(this.decoder.decode(value));
console.log(data); }
}
#setupListeners() {
if ('serial' in navigator) {
navigator.serial.addEventListener('connect', this.onConnect);
navigator.serial.addEventListener('disconnect', this.onDisconnected);
} }
} }
} }
@ -61,7 +69,14 @@ const $status = document.getElementById('status');
const $vendorId = document.getElementById('vendorId'); const $vendorId = document.getElementById('vendorId');
const $productId = document.getElementById('productId'); const $productId = document.getElementById('productId');
const serialPortHandler = new SerialPortHandler({ baudRate: 9600 }); const onConnected = (e) => console.log('device inserted!');
const onDisconnected = (e) => console.log('device ejected!');
const serialPortHandler = new SerialPortHandler(
{ baudRate: 9600 },
onConnected,
onDisconnected
);
async function onConnect() { async function onConnect() {
try { try {