Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Web Serial API #1

Open
frank-dspeed opened this issue Apr 21, 2021 · 2 comments
Open

New Web Serial API #1

frank-dspeed opened this issue Apr 21, 2021 · 2 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@frank-dspeed
Copy link
Owner

frank-dspeed commented Apr 21, 2021

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 frank-dspeed added documentation Improvements or additions to documentation enhancement New feature or request labels Apr 21, 2021
@frank-dspeed
Copy link
Owner Author

@frank-dspeed
Copy link
Owner Author

serialport-webserial.js

//https://unpkg.com/browse/[email protected]/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/[email protected]/dist/ponyfill.es2018.mjs
        readable: {
            getReader(){
                return {
                    read() {}
                }
            }
        },
        writeable: {
            getWriter(){
                return {
                    write() {}
                }
            }
        },
        open(serialOptions) {
            _internalPort = new SerialPort();
        },
        getPorts(){},
        requestPort() {},
        closed() {},
        ready() {},
        onconnect() {},
        ondisconnect() {}
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant