uart.js connect directly to espruino?

Posted on
  • Hello,

    i'm playing with the uart.js lib to connect an espruino to a webpage.
    Is it possible to connect to an espruino by command line (js) without the menu "bt4 or webserial", if i know the name of my espruino?
    and can i use connection.close() (line 3) to disconnect the espruino from the page (without closing the webpage)? (to update the code in the webIDE)

    i use this example

    let connection;
        if (connection) {
          connection.close();
          connection = undefined;
        }
        UART.connect((c) => {
          if (!c) {
            console.log("Pas de connexion");
            alert("Impossible de se connecter");
            return;
          }
          console.log("Connecté");
          connection = c;
          // Handle the data we get back, and call 'onLine'
          // whenever we get a line
          resultData = "";
          connection.on("data", (d) => {
            resultData = d;
            resultData  = resultData.replace(/\r\n|>|\[J/g, '');
            if (resultData.length > 1) {
              document.querySelector("article").innerH­TML = resultData;
            }
          });
        });
    

    regards

    é.

  • i tried connection.close() but received an error

    Uncaught (in promise) TypeError: Failed to execute 'close' on 'SerialPort': Cannot cancel a locked stream
    at Object.connection.close (uart.js:272)

    how to unlockStream?

  • I'm not sure about Cannot cancel a locked stream - I haven't come across that one before :(

    If you want to use the command-line and not the browser (eg Node.js) then there's https://serialport.io/ - not quite the same API but it's a very similar idea.

    But from the Web Browser itself, for security they say that you have to bring up the port chooser at least once (but there's no need for the BT/USB menu).

    UART.js is based on Web Serial, so you could use that directly and you'd have a lot more flexibility (and avoid having the menu) - check out https://codelabs.developers.google.com/c­odelabs/web-serial/#3

  • Cannot cancel a locked stream

    this is the error from the browser... sorry ;)
    the page is connected to espruino
    on the espruino:

    const onInit  = () => {
      var step = 0;
      require("Encoder").connect(A8,A5, (dir) => {
        step += dir;
        print(step);
        if (dir > 0) {
          //allume la led 20ms
          digitalPulse(LED1, 1, 20);
        } else {
          digitalPulse(LED2, 1, 20);
        }
      });
    };
    

    i suppose the port is in use and therfor browser can not cancel connection...

    thanks for your time.
    é.

  • i suppose the port is in use

    Could it be open in another browser window? sometimes you can do that with things like Web Bluetooth, and that might cause the error?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

uart.js connect directly to espruino?

Posted by Avatar for Mrbbp @Mrbbp

Actions