script update
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
// main.ts
|
||||
const connectButton = document.getElementById('connectButton') as HTMLButtonElement;
|
||||
const outputTextarea = document.getElementById('output') as HTMLTextAreaElement;
|
||||
|
||||
let port: SerialPort | null = null;
|
||||
|
||||
async function connect() {
|
||||
try {
|
||||
port = await navigator.serial.requestPort();
|
||||
await port.open({ baudRate: 9600 }); // Adjust baud rate as per your device settings
|
||||
outputTextarea.value = ''; // Clear previous output
|
||||
readLoop();
|
||||
connectButton.disabled = true;
|
||||
} catch (error) {
|
||||
console.error('Error connecting to the serial port:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function readLoop() {
|
||||
while (port && port.readable) {
|
||||
try {
|
||||
const reader = port.readable.getReader();
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) {
|
||||
reader.releaseLock();
|
||||
break;
|
||||
}
|
||||
// Display the received data
|
||||
outputTextarea.value += new TextDecoder().decode(value);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error reading from the serial port:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
connectButton.addEventListener('click', connect);
|
||||
|
Reference in New Issue
Block a user