An API to query a Serial Scale which supports the NCI protocal (SCP-12, NCI 3825, NCI 3835). Tested against the Salter Brecknell PS-USB scale. Depends on the Web Serial API.
Page Hosting the library must be served over HTTPS. Demo is available at https://chris-rudmin.github.io/serial-nci-scale/
npm install serial-nci-scale --save
import SerialNCIScale from 'serial-nci-scale';
const scale = new SerialNCIScale();
const { weight, units, status } = await scale.getWeight();
const scale = new SerialNCIScale( [config] )
- Create a new instance with optional device filter array or port config object
config.filters
- Optional - Array of filters to use when initializing the port. Defaults to [].config.portConfig
- Optional - Object containing serial configuration to use for initializing port. Defaults to 7E1 byte format.
scale.getWeight()
- Returns a promise which resolves with scale weight data. Required to be called from user actionscale.getStatus()
- Returns a promise which resolves with scale status data. Required to be called from user actionscale.zero()
- Returns a promise which resolves when the scale is zeroed. Required to be called from user actionscale.startPolling([pollInterval])
- Starts polling the scale for weight data. Default polling interval is 500 ms Required to be called from user actionscale.stopPolling()
- Stops polling the scalescale.disconnect()
- Returns a promise which resolves once disconnected. Closes the read and write streams and frees the serial port
SerialNCIScale.isWebSerialSupported
-true
if Web Serial API exists in your browserSerialNCIScale.supportedScaleFilters
- Array of devices which are known to be supported
The serial-nci-scale instance is an EventTarget and can be listened to for custom events. Scale data is contained the in the detail
property.
weight
- Dispatched for every weight readstatus
- Dispatched for every status readsettled
- Dispatched when the scale is no longer in motion and if the weight is different than the last settled weight
scale.addEventListener('settled', ({ detail }) => foo(detail.status));
scale.startPolling();
Currently only Chrome 89+ and Edge 89+ support the Web Serial API. Earlier versions of Chrome and Edge can use the API if it is enabled in chrome://flags