Skip to content

Connection and recovery with serial-core ​

JSD uses serial-core@^0.3.0 for the serial session and serialport@^13 for native device access. Install them as runtime peers in your application. You configure them through the JSD constructor; you do not need to modify either library.

Configure recovery in your application ​

ts
import { JSD } from '@danidoble/serial-node-jsd';

const jsd = new JSD({
  baudRate: 9600,
  vendorId: '0403',
  productId: '6001',
  serialNumber: 'YOUR-UNIT-SERIAL',
  autoConnect: false,
  autoReconnect: true,
  reconnectInterval: 1_000,
  reconnectBackoffFactor: 2,
  maxReconnectInterval: 10_000,
  maxReconnectAttempts: 5,
  writeTimeout: 3_000,
  maxQueueSize: 100
});

jsd.on('serial:reconnecting', ({ attempt, delay }) => {
  console.log(`Retry ${attempt} scheduled in ${delay} ms`);
});
jsd.on('serial:reconnectFailed', ({ attempts }) => {
  console.error(`Recovery stopped after ${attempts} retries`);
});
jsd.on('serial:error', error => console.error(error.message));
jsd.start();

Omit path when selecting a device by USB identity. IDs match exactly after normalization; serial numbers match exactly. A fixed path takes priority over selectors.

Find a port before connecting ​

ts
const ports = await JSD.listPorts({ vendorId: '0403', availableOnly: true });
console.table(ports);

listPorts() and findPorts() are available as static and instance methods. They enumerate metadata without opening devices. Filters combine with AND. availableOnly checks local library reservations, not OS-wide availability. Enumeration errors reject; no match returns an empty list.

What to expect on interruption ​

  • serial:connected signals a verified session, after opening and handshake.
  • serial:reconnecting includes the one-based retry attempt and capped delay.
  • serial:reconnectFailed signals that the retry budget is exhausted.
  • Pending commands are discarded when the session ends; they are not replayed.
  • Pending logs, channel assignments, carts and dispenses reject and release their timers.
  • Cached machine availability is invalidated. Obtain fresh status before dispensing again.
  • stop() ends recovery and awaits closure. If closing fails, handle its rejection.

A rejected dispense does not prove that the machine did nothing. Reconcile its state before deciding whether to repeat a physical operation.

Parsers and deadlines ​

JSD supplies a new delimiter parser per session. You can provide parserFactory for a custom parser, or a shared parser, but not both. The configuration object you pass to JSD is preserved.

writeTimeout bounds physical writing and drain. The JSD ACK wait starts only after successful writing, and is separate from core's response timeout. Use an application deadline for domain operations that can remain pending when the device stays connected but never completes its response.

See all configuration options, the complete application example and failure handling.

Explicit protocol, typed events, isolated transport.