SDK 开发
Node.js / TypeScript
QHL-XCI 的 Node.js / TypeScript 薄绑定,基于 Koffi 直接调用 qinch_device_lib.dll 的稳定 C ABI,
不需要 Visual Studio、node-gyp 或本机 C++ 编译器。
支持范围
- Windows x64
- Node.js 22 LTS(最低 22.12)与 Node.js 24 LTS
- JavaScript(CommonJS)与 TypeScript 类型声明
- 设备枚举、型号/版本/SN、CAN/LIN、轮询、异步接收回调、自定义 CAN 位时序
- 批量接收与带超时等待,适合 Electron/桌面应用工作线程
- UDS 诊断(返回 Promise,等待 ECU 应答时不阻塞事件循环)
请使用上面列出的 Node.js 版本。
安装与 DLL
使用 SDK 包里的源码(bindings/node 目录):
cd bindings/node
npm ci
产品 tgz 安装:
npm install .\qinch-xci-sdk-1.1.0.tgz
产品包已内置 win32-x64 原生 DLL,安装后可直接 new QinchXci()。
运行时必须提供同为 x64 的两个文件,并放在同一目录:
qinch_device_lib.dlllibusb-1.0.dll
正式 npm 包内置 native/win32-x64/ 两颗 DLL。查找优先级:构造参数 dllPath →
环境变量 QINCH_XCI_DLL_PATH → npm 包内置 DLL → SDK 目录 → 当前工作目录 → Windows DLL 搜索路径。
$env:QINCH_XCI_DLL_PATH = "D:\sdk\qinch_device_lib.dll"
npm run smoke
快速使用
const {
QinchXci, CanMessage, Channel, Baudrate
} = require('@qinch/xci-sdk');
const xci = new QinchXci({ dllPath: 'D:\\sdk\\qinch_device_lib.dll' });
try {
console.log(xci.getVersion().toString());
console.log(xci.listDevices());
xci.initialize(Channel.CAN1, Baudrate.BAUD_500K);
xci.canSetTerminator(Channel.CAN1, true);
xci.canWrite(Channel.CAN1, new CanMessage(0x123, [1, 2, 3, 4]));
xci.flushTx(Channel.CAN1);
const received = xci.canWaitRead(Channel.CAN1, 100, 256); // 超时返回 []
for (const frame of received) console.log(frame.message, frame.timestamp);
} finally {
xci.close();
}
回调来自原生接收线程,Koffi 会把它排队到 Node 主线程执行。因此回调内不要做长时间阻塞,主线程也必须持续让事件循环运行。完整示例见:
examples/device_info.js:枚举和版本信息examples/can_loopback.js:CAN1↔CAN2 双向轮询回环examples/can_callback.js:跨原生线程的接收回调
UDS 诊断
const { QinchXci, Channel, Baudrate, UdsNegativeResponseError, udsNrcName } = require('@qinch/xci-sdk');
const xci = new QinchXci({ dllPath: 'D:\\sdk\\qinch_device_lib.dll' });
try {
xci.initialize(Channel.CAN1, Baudrate.BAUD_500K);
xci.udsSetConfig(Channel.CAN1, { requestId: 0x7e0, responseId: 0x7e8 });
await xci.udsSessionControl(Channel.CAN1, 0x03); // 扩展会话
xci.udsTesterPresent(Channel.CAN1, 2000); // 后台保活
const vin = await xci.udsReadDid(Channel.CAN1, 0xf190); // Buffer
const { availabilityMask, dtcs } = await xci.udsReadDtc(Channel.CAN1, 0xff);
try {
await xci.udsWriteDid(Channel.CAN1, 0xf190, vin);
} catch (e) {
if (e instanceof UdsNegativeResponseError) console.log(`ECU 拒绝: NRC 0x${e.nrc.toString(16)} ${udsNrcName(e.nrc)}`);
else throw e;
}
} finally {
xci.close();
}
- 要等 ECU 应答的方法(
udsRequest、会话、复位、读写数据标识符、安全访问、读清故障码、例程控制)返回 Promise, 在库的工作线程里执行,等待期间不阻塞事件循环;udsSetConfig、udsGetConfig、udsTesterPresent、udsGetLastNrc立即返回。 - ECU 否定响应以
UdsNegativeResponseError(nrc、response,是QinchXciError的子类)拒绝;超时以QinchXciError拒绝,status为Status.UDS_NO_RESPONSE。 udsRequest发送任意服务,返回完整响应;ECU 回”响应等待中”(NRC 0x78)时自动继续等。同时发起多个请求时依次执行。- 传输层参数在
udsSetConfig里设置,没给的字段用默认值:txPadding、padByte(默认 0xcc)、fcBlockSize、fcStMin、nBsMs、nCrMs。 - 诊断过程中收发的每一帧也会出现在
canRead/canWaitRead/ 回调里(本机发出的帧isEcho为真)。
验证
npm run check
npm run smoke
npm run test:hardware # 需要 CAN1 与 CAN2 已物理连接
can_loopback.js 会把 CAN1/CAN2 都初始化为 500 kbit/s,接入两路板载 120Ω 终端,关闭 Tx echo,
并验证 0x321 与 0x456 两个方向的数据一致性。