SDK v0.1 · read-only
Quickstart: JavaScript
Install
@smongrot/sdkand open a read-only session from a desktop browser or from Node.
Cable and drivers: Hardware. Longer recipes: Examples.
npm i @smongrot/sdk
ESM, zero runtime dependencies, MIT. Node ≥ 18, or desktop Chrome / Edge (WebSerial). Key ON (engine off is fine), cable on the bike's diagnostic connector.
Browser
import { connect, openWebSerial } from '@smongrot/sdk';
button.onclick = async () => {
const port = await navigator.serial.requestPort();
const s = await connect(await openWebSerial(port));
const { ecmId } = await s.identify();
for await (const f of s.live()) {
const rpm = f.fields.rpm;
show(rpm?.status === 'ok' ? rpm.value : '—'); // never show 0 for a missing value
}
};
Node
serialport is an optional peer dependency; install it yourself.
npm i @smongrot/sdk serialport
import { SerialPort } from 'serialport';
import { connect, fromSerialPort } from '@smongrot/sdk';
const port = new SerialPort({ path: '/dev/cu.usbserial-XXXX', baudRate: 10400, autoOpen: false });
const s = await connect(await fromSerialPort(port));
console.log(await s.identify());
console.log(await s.dtc.read()); // beta: result.beta === true
await s.close();
The package ships a runnable examples/node-serialport.mjs. Any other Duplex
stream at 10400 8N1 works with fromDuplex(stream, { setBreak | wake }).
Model name
Add a free API key to get the model name in identify() (see
Identify and API key):
import { smongrotApi } from '@smongrot/sdk';
const api = smongrotApi({ apiKey: process.env.SMONGROT_API_KEY });
const s = await connect(transport, { lookupModel: api.lookupModel });
Test without a bike
import { connect, FakeEcuTransport } from '@smongrot/sdk';
const fake = new FakeEcuTransport({
replies: { '72 07 72 17 00 0F EF': '02 15 72 17 00 0D EE 67 45 F8 0A FF FF FF FF 7D 0C 0D A8 55 28' },
});
const s = await connect(fake, { sleep: async () => {} });
for await (const f of s.live()) { console.log(f.fields.rpm.value); break; } // 3566
Next: Quickstart: Dart