// 监听串口数据接收 async function readSerialData() { while (true) { try { const { value, done } = await reader.read(); if (done) { reader.releaseLock(); break; } if (value) { console.log('Received data:', value); } } catch (error) { console.error('Error reading serial data:', error); break; } } }
}); // 发送消息 function sendMessage(message) { let encoder = new TextEncoder(); let data = encoder.encode(message); let writer = port.writable.getWriter(); writer.write(data); writer.releaseLock(); }