Skip to content

lucsoft/web_serial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSerial Polyfill for Deno

This project provides a polyfill for the Web Serial API in Deno, enabling serial port communication in Deno applications using a familiar web-compatible interface.

Usage

Currently, this project does not ship any pre-built binaries. To use the polyfill, you need to build the native adapter yourself:

  1. Clone the repository and navigate to its directory.

  2. Build the native binary using Cargo:

    cargo build --release
  3. Set the WEB_SERIAL_PATH environment variable to the folder containing the compiled binary (typically target/release):

    export WEB_SERIAL_PATH=/path/to/deno_serial/target/release
  4. Use the polyfill in your Deno project as documented.

Make sure you have Rust and Cargo installed.

Example

  1. Arduino Blink Sketch

    Upload the following sketch to your Arduino:

    String inputString = "";
    bool stringComplete = false;
    
    void setup() {
        Serial.begin(115200);
        inputString.reserve(200);
    
        pinMode(2, OUTPUT);
    }
    
    void loop() {
        if (!stringComplete) return;
        inputString.trim();
        if (inputString != "OFF" && inputString != "ON") return;
        digitalWrite(2, inputString == "ON");
    
        inputString = "";
        stringComplete = false;
    }
    
    void serialEvent() {
        while (Serial.available()) {
            char inChar = (char)Serial.read();
            inputString += inChar;
            if (inChar == '\n') {
            stringComplete = true;
            }
        }
    }

    Blinking via Web Serial in Deno:

    import "../mod.ts"; // Import the polyfill
    import { delay } from "jsr:@std/async/delay";
    
    // get first available serial port
    const port = await navigator.serial.requestPort();
    await port.open({ baudRate: 115200 });
    
    // read every chunk from the serial port
    port.readable!
        .pipeThrough(new TextDecoderStream())
        .pipeTo(new WritableStream({
            write(chunk) {
                console.log(chunk.trim());
            }
        }));
    
    // blink fast
    const writer = port.writable!.getWriter();
    
    while (true) {
        await writer.write(new TextEncoder().encode("ON\n"));
        await delay(100);
        await writer.write(new TextEncoder().encode("OFF\n"));
        await delay(100);
    }

License

Apache-2.0. Check LICENSE for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •