frank-dspeed / WebSerialToSerialPort-API

A Draft for a new Web Serial API as also a Pollyfill for existing WebSerial API that algins with node-serialport
0 stars 0 forks source link

New Web Serial API #1

Open frank-dspeed opened 3 years ago

frank-dspeed commented 3 years ago

Deprecated Example in both Locations as using the port Path or Address Directly is not Possible with the WebSerialApi

and it is also not usefull for Linux in Software that gets Shipped also the productId vendorId pattern is the new successor

the old functionality does not need to get removed but examples should show how to use find() alternatives ,getPort, requestPort()

const SerialPort = require('serialport')
const port = new SerialPort('/dev/tty-usbserial1', {
  baudRate: 57600
})

// gets
const SerialPort = navigator.serial || require('serialport');
const usbVendorId = ...;
SerialPort.requestPort({ filters: [{ usbVendorId }]}).then((port) => {
  // Connect to `port` or add it to the list of available ports.
}).catch((e) => {
  // The user didn't select a port.
});

Introducing Web Streams to NodeJS

{ value, done } = await port.read() 

Accepting Callbacks and register them

port.write('main screen turn on', function(err) {
  if (err) {
    return console.log('Error on write: ', err.message)
  }
  console.log('message written')
})

// Open errors will be emitted as an error event
port.on('error', function(err) {
  console.log('Error: ', err.message)
})

Error Handling for connections on the port

const SerialPort = require('serialport')
const port = new SerialPort('/dev/tty-usbserial1', function (err) {
  if (err) {
    return console.log('Error: ', err.message)
  }
})

port.write('main screen turn on', function(err) {
  if (err) {
    return console.log('Error on write: ', err.message)
  }
  console.log('message written')
})

Auto Open# If you disable the autoOpen option, you'll need to open the port on your own.

const SerialPort = require('serialport')
const port = new SerialPort('/dev/tty-usbserial1', { autoOpen: false })

port.open(function (err) {
  if (err) {
    return console.log('Error opening port: ', err.message)
  }

  // Because there's no callback to write, write errors will be emitted on the port:
  port.write('main screen turn on')
})

// The open event is always emitted
port.on('open', function() {
  // open logic
})

Reading Data#

Get updates about new data arriving through the serial port as follows:

Enjoy and do cool things with this code.

frank-dspeed commented 3 years ago

https://github.com/serialport/node-serialport/issues/2238

frank-dspeed commented 3 years ago

serialport-webserial.js

//https://unpkg.com/browse/web-streams-polyfill@3.0.3/dist/ponyfill.es2018.mjs
/**
 * Complet interfaces
 * interface EventTarget {
  constructor();

  undefined addEventListener(DOMString type, EventListener? callback, optional (AddEventListenerOptions or boolean) options = {});
  undefined removeEventListener(DOMString type, EventListener? callback, optional (EventListenerOptions or boolean) options = {});
  boolean dispatchEvent(Event event);
};

callback interface EventListener {
  undefined handleEvent(Event event);
};

dictionary EventListenerOptions {
  boolean capture = false;
};

dictionary AddEventListenerOptions : EventListenerOptions {
  boolean passive = false;
  boolean once = false;
  AbortSignal signal;
};

interface SerialPort : EventTarget {
  attribute EventHandler onconnect;
  attribute EventHandler ondisconnect;
  readonly attribute ReadableStream readable;
  readonly attribute WritableStream writable;

  SerialPortInfo getInfo();

  Promise<undefined> open(SerialOptions options);
  Promise<undefined> setSignals(optional SerialOutputSignals signals = {});
  Promise<SerialInputSignals> getSignals();
  Promise<undefined> close();
};
  // serialOptions
  required [EnforceRange] unsigned long baudRate;
  [EnforceRange] octet dataBits = 8;
  [EnforceRange] octet stopBits = 1;
  ParityType parity = "none";
  [EnforceRange] unsigned long bufferSize = 255;
  FlowControlType flowControl = "none";

 * @param {*} SerialPort 
 * @param {*} errorHandler 
 */

function WebSerialPort(SerialPort,serialOptions,errorHandler) {
    const onconnect, ondisconnect

    const SerialPort = {
        //https://unpkg.com/browse/web-streams-polyfill@3.0.3/dist/ponyfill.es2018.mjs
        readable: {
            getReader(){
                return {
                    read() {}
                }
            }
        },
        writeable: {
            getWriter(){
                return {
                    write() {}
                }
            }
        },
        open(serialOptions) {
            _internalPort = new SerialPort();
        },
        getPorts(){},
        requestPort() {},
        closed() {},
        ready() {},
        onconnect() {},
        ondisconnect() {}
    }
}