Function serial

Create a serial-connected Espruino device.

import { Espruino } from 'https://unpkg.com/ixfx/dist/io.js'
const e = await Espruio.serial();
e.connect();

Options:

import { Espruino } from 'https://unpkg.com/ixfx/dist/io.js'
const e = await Espruino.serial({ debug: true, evalTimeoutMs: 1000, name: `My Pico` });
e.connect();

Listen for events:

e.addEventListener(`change`, evt => {
console.log(`State change ${evt.priorState} -> ${evt.newState}`);
if (evt.newState === `connected`) {
// Do something when connected...
}
});

Reading incoming data:

// Parse incoming data as JSON
s.addEventListener(`data`, evt => {
try {
const o = JSON.parse(evt.data);
// If we get this far, JSON is legit
} catch (ex) {
}
});

Writing to the microcontroller

s.write(JSON.stringify({msg:"hello"}));
  • Parameters

    • opts: {
          debug?: boolean;
          evalTimeoutMs?: number;
          name?: string;
      } = {}
      • Optional Readonlydebug?: boolean
      • Optional ReadonlyevalTimeoutMs?: number
      • Optional Readonlyname?: string

    Returns Promise<EspruinoSerialDevice>

    Returns a connected instance, or throws exception if user cancelled or could not connect.