SDK 开发

Python

基于 qinch_device_lib.dll 对外 C ABI 的 ctypes 绑定。纯 Python,用户侧零编译。

产品 wheel 安装:

pip install qinch_xci_sdk-1.1.0-py3-none-any.whl
$env:QINCH_XCI_DLL_PATH = "<SDK解压目录>\bin\win-x64\qinch_device_lib.dll"

wheel 是跨 Python 版本的纯绑定包;两颗原生 DLL 由统一 SDK ZIP 按 x86/x64 分发。

安装

把 3 个文件放在一起(或把前两个 DLL 放到你的程序目录 / os.add_dll_directory 指向的目录):

你的项目/
├── qinch_xci.py            # 本绑定
├── qinch_device_lib.dll    # SDK DLL (自包含)
└── libusb-1.0.dll          # libusb (动态, LGPL)

要求 Python 3.11 – 3.14,DLL 位宽须与 Python 一致(64 位 Python 用 win-x64,32 位用 win-x86)。绑定会自动把 qinch_device_lib.dll 所在目录加入 DLL 搜索路径,故同目录的 libusb-1.0.dll 能被找到。

快速上手

from qinch_xci import QinchXci, Channel, Baudrate, CanMsg

with QinchXci() as xci:                       # 默认找同目录的 qinch_device_lib.dll
    print(xci.get_version())                  # -> Version(1.1.0)
    for d in xci.list_devices():
        print(d)                              # DeviceInfo(id=..., model=..., can=2, lin=1)

    xci.initialize(Channel.CAN1, Baudrate.BAUD_500K)
    xci.can_write(Channel.CAN1, CanMsg(0x123, b"\x01\x02\x03"))

    msg, ts = xci.can_read(Channel.CAN1)      # 轮询; 队列空返回 (None, None)
    if msg:
        print(msg, ts)

    frames = xci.can_wait_read(Channel.CAN1, 100, capacity=256)  # 超时返回 []

    xci.uninitialize(Channel.CAN1)

指定 DLL 路径:构造参数 QinchXci(dll_path=...),或设置环境变量 QINCH_XCI_DLL_PATH。

接收方式

回调 (库的接收线程触发, 回调里别做重活/别再调阻塞 API):

def on_can(channel, msg, ts):
    print(channel, msg)

xci.can_register_callback(Channel.CAN1, on_can)
# ...
xci.can_unregister_callback(Channel.CAN1)

轮询:单帧用 can_read / lin_read;PC 工作线程优先用 can_wait_read / lin_wait_read,一次返回多帧并支持毫秒超时。

错误处理

默认非 OK 状态码抛 QinchXciError(带库返回的错误文本)。需要方法直接返回状态码时, 构造时传 QinchXci(raise_on_error=False),再自行比对 Status.OK。

from qinch_xci import QinchXciError
try:
    xci.initialize(Channel.CAN1, Baudrate.BAUD_500K)
except QinchXciError as e:
    print("打开失败:", e.status, e.text)

UDS 诊断

from qinch_xci import QinchXci, Channel, Baudrate, UdsNegativeResponse

with QinchXci() as xci:
    xci.initialize(Channel.CAN1, Baudrate.BAUD_500K)
    xci.uds_set_config(Channel.CAN1, request_id=0x7E0, response_id=0x7E8)
    xci.uds_session_control(Channel.CAN1, 0x03)          # 扩展会话
    xci.uds_tester_present(Channel.CAN1, 2000)           # 后台保活
    vin = xci.uds_read_did(Channel.CAN1, 0xF190)
    avail, dtcs = xci.uds_read_dtc(Channel.CAN1, 0xFF)
    try:
        xci.uds_write_did(Channel.CAN1, 0xF190, b"...")
    except UdsNegativeResponse as e:
        print("ECU 拒绝: NRC 0x%02X" % e.nrc)            # 如 0x33 未解锁
  • ECU 否定响应抛 UdsNegativeResponse(nrc、response);超时抛 QinchXciError,status 为 Status.UDS_NO_RESPONSE。
  • uds_request 发送任意服务,返回完整响应;ECU 回”响应等待中”时自动继续等。
  • 诊断方法会阻塞到收到响应或超时;多个线程同时调用时依次执行。
  • 传输层参数在 uds_set_config 里设置,如 uds_set_config(ch, pad_byte=0x00)、uds_set_config(ch, fc_st_min=10)。
  • 诊断过程中收发的每一帧也会出现在 can_read / can_wait_read / 回调里(本机发出的帧 is_echo 为真)。

完整示例见 example_uds.py(SDK 包内 bindings/python/)。

API 速览

类别方法
设备/版本get_version · get_device_count · get_device_info · list_devices · enumerate_channels
通道initialize · initialize_ex · uninitialize · close_device · reset · flush_tx · get_channel_status
CAN 控制can_set_terminator (120Ω) · can_set_tx_echo · can_filter
CAN 收发can_write · can_write_batch · can_read · can_read_batch · can_wait_read · callback
LINlin_initialize · lin_uninitialize · lin_write · lin_read · lin_read_batch · lin_wait_read · callback
参数get_value · set_value · get_serial_number
UDS 诊断uds_set_config · uds_get_config · uds_request · uds_session_control · uds_ecu_reset · uds_tester_present · uds_read_did · uds_write_did · uds_security_request_seed · uds_security_send_key · uds_read_dtc · uds_clear_dtc · uds_routine_control · uds_get_last_nrc
工具get_error_text

常量命名空间:Channel · Baudrate · LinBaudrate · MsgType · LinFlags · Param · Status。

完整示例见 example_can.py(SDK 包内 bindings/python/);batch/wait 回环见 example_can_wait.py(SDK 包内 bindings/python/)。