Open frank-dspeed opened 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() {}
}
}
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()
Introducing Web Streams to NodeJS
Accepting Callbacks and register them
Error Handling for connections on the port
Auto Open# If you disable the autoOpen option, you'll need to open the port on your own.
Reading Data#
Get updates about new data arriving through the serial port as follows:
Enjoy and do cool things with this code.