mirror of
https://github.com/sergeyyarkov/avr128da48_web-serial-example.git
synced 2026-10-11 18:28:58 +03:00
refactor
This commit is contained in:
parent
6e5e770bd8
commit
1f631cecd0
132
web/app.js
132
web/app.js
@ -1,69 +1,9 @@
|
||||
/**
|
||||
* If this character is included in the received data chunk,
|
||||
* it means that transfer completed.
|
||||
*/
|
||||
var EOT = '\u0004';
|
||||
|
||||
class SerialPortHandler {
|
||||
constructor(options, onConnect, onDisconnect) {
|
||||
this.encoder = new TextEncoder();
|
||||
this.decoder = new TextDecoder();
|
||||
this.onConnect = onConnect;
|
||||
this.onDisconnect = onDisconnect;
|
||||
this.options = options;
|
||||
this.port = null;
|
||||
this.isOpened = false;
|
||||
this.#setupListeners();
|
||||
}
|
||||
|
||||
async open() {
|
||||
try {
|
||||
const port = await navigator.serial.requestPort();
|
||||
await port.open(this.options);
|
||||
|
||||
this.port = port;
|
||||
this.isOpened = true;
|
||||
|
||||
return this.port.getInfo();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async close() {
|
||||
await this.port.close();
|
||||
this.isOpened = false;
|
||||
}
|
||||
|
||||
async write(data) {
|
||||
const writer = this.port.writable.getWriter();
|
||||
const encoded = this.encoder.encode(data);
|
||||
await writer.write(encoded);
|
||||
writer.releaseLock();
|
||||
}
|
||||
|
||||
async read() {
|
||||
const reader = this.port.readable.getReader();
|
||||
let chunks = '';
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
const decoded = this.decoder.decode(value);
|
||||
|
||||
chunks += decoded;
|
||||
|
||||
if (done || decoded.includes('\u0004')) {
|
||||
console.log('Reading done.');
|
||||
reader.releaseLock();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
#setupListeners() {
|
||||
navigator.serial.addEventListener('connect', this.onConnect);
|
||||
navigator.serial.addEventListener('disconnect', this.onDisconnected);
|
||||
}
|
||||
}
|
||||
|
||||
class Application {
|
||||
constructor(root) {
|
||||
if (!('serial' in navigator)) {
|
||||
@ -148,4 +88,68 @@ class Application {
|
||||
}
|
||||
}
|
||||
|
||||
class SerialPortHandler {
|
||||
constructor(options, onConnect, onDisconnect) {
|
||||
this.encoder = new TextEncoder();
|
||||
this.decoder = new TextDecoder();
|
||||
this.onConnect = onConnect;
|
||||
this.onDisconnect = onDisconnect;
|
||||
this.options = options;
|
||||
this.port = null;
|
||||
this.isOpened = false;
|
||||
this.#setupListeners();
|
||||
}
|
||||
|
||||
async open() {
|
||||
try {
|
||||
const port = await navigator.serial.requestPort();
|
||||
await port.open(this.options);
|
||||
|
||||
this.port = port;
|
||||
this.isOpened = true;
|
||||
|
||||
return this.port.getInfo();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async close() {
|
||||
await this.port.close();
|
||||
this.isOpened = false;
|
||||
}
|
||||
|
||||
async write(data) {
|
||||
const writer = this.port.writable.getWriter();
|
||||
const encoded = this.encoder.encode(data);
|
||||
await writer.write(encoded);
|
||||
writer.releaseLock();
|
||||
}
|
||||
|
||||
async read() {
|
||||
const reader = this.port.readable.getReader();
|
||||
let chunks = '';
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
const decoded = this.decoder.decode(value);
|
||||
|
||||
chunks += decoded;
|
||||
|
||||
if (done || decoded.includes('\u0004')) {
|
||||
console.log('Reading done.');
|
||||
reader.releaseLock();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
#setupListeners() {
|
||||
navigator.serial.addEventListener('connect', this.onConnect);
|
||||
navigator.serial.addEventListener('disconnect', this.onDisconnected);
|
||||
}
|
||||
}
|
||||
|
||||
new Application(document.getElementById('app'));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user