必须登录才能发言。

  • 40
    @Ta 03-29 15:04
    /
  • 39
    @Ta 03-28 21:42
    /
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>WebSerial Example</title>
    </head>

    <body>
        <button id="openPort">打开串口</button>
        <script>
            document.addEventListener('DOMContentLoaded', () => {
                const openPortButton = document.getElementById('openPort');
                let serialPort;
                let reader;

                openPortButton.addEventListener('click', async () => {
                    try {
                        // 请求访问串口
                        port = await navigator.serial.requestPort();
                        serialPort = port;

                        // 打开串口
                        await port.open({ baudRate: 9600 });

                        // 创建TextDecoderStream实例并连接到串口可读流
                        let decoder = new TextDecoderStream();
                        port.readable.pipeTo(decoder.writable);

                        // 创建一个ReadableStreamDefaultReader实例来读取解码后的数据
                        reader = decoder.readable.getReader();

                        // 开始监听数据
                        readSerialData();

                        // 示例发送一条消息
                        sendMessage("Hello from WebSerial!");

                    } catch (error) {
                        console.error('Error accessing serial port:', error);
                    }
                });

                // 监听串口数据接收
                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();
                }

                // 关闭串口
                function closePort() {
                    if (serialPort && serialPort.isOpen && reader) {
                        reader.cancel();
                        serialPort.close();
                    }
                }
            // 可能的话,在窗口卸载时关闭串口
            window.addEventListener('beforeunload', (event) => {
                closePort();
            });
        </script>
    </body>

    </html>
  • 38
    @Ta 03-28 18:35
    /
    层主 @5258 删除了该楼层。
  • 37
    @Ta 03-28 14:33
    /
  • 36
    @Ta 03-25 16:52
    /

    @ChatGPT 你好

  • 35
    @Ta 03-17 23:34
    /
    层主 @咯叽 删除了该楼层。
  • 34
    @Ta 03-16 14:46
    /
    Eloquent ORM
  • 33
    @Ta 03-14 10:49
    /
    @张小强,可以正常打开
  • 32
    @Ta 03-13 20:20
    /
  • 31
    @Ta 03-13 15:43
    /
    ed2k://|file|zh-cn_windows_server_2022_updated_feb_2024_x64_dvd_95120870.iso|5562255360|82027A964E1A091F9C1C83FB8CA7ABB7|/
  • 30
    @Ta 03-13 10:09
    /
    层主 @胡图图 删除了该楼层。
  • 29
    @Ta 03-12 22:08
    /
  • 28
    @Ta 03-12 22:08
    /
  • 27
    @Ta 03-12 22:08
    /
  • 26
    @Ta 03-12 22:08
    /
  • 25
    @Ta 03-12 22:08
    /

    @ChatGPT 44444444444444444444、

    4444

  • 24
    @Ta 03-12 22:08
    /
  • 23
    @Ta 03-12 22:08
    /

    @ChatGPT 44444

  • 22
    @Ta 03-12 22:08
    /

    @ChatGPT 44444

  • 21
    @Ta 03-12 22:08
    /

    @ChatGPT 44444