与适配器建立连接
最后修改:
该函数与 ScanDoc 适配器建立连接。连接参数(IP 地址或 BLE 设备名称)从配置文件 j2534.json 中读取。
long PassThruOpen(void* pName, unsigned long* pDeviceID)
NULL 或指向空字符串的指针。连接参数从配置文件中读取。PassThruConnect()、PassThruClose() 等函数。
连接设置存储在文件 j2534.json 中。文件位置取决于操作系统:
| 操作系统 | 文件路径 |
|---|---|
| Windows | %APPDATA%\Quantex\j2534.json例如:C:\Users\User\AppData\Roaming\Quantex\j2534.json |
| macOS | ~/Library/Application Support/Quantex/j2534.json |
| Linux | ~/.config/quantex/j2534.json |
如果文件不存在,将以默认设置自动创建。
{
"current_device": 0,
"devices": [
{
"name": "ScanDoc",
"connection_type": "LAN",
"device_ip": "192.168.1.3",
"device_name": ""
}
],
"logs_directory": "",
"debug": false
}
| 字段 | 类型 | 说明 |
|---|---|---|
| current_device | number | devices 数组中活动设备的索引(从 0 开始) |
| devices | array | 设备数组。对于 API v04.04,仅使用索引为 current_device 的一个设备 |
| name | string | 设备的显示名称(便于用户识别) |
| connection_type | string | "LAN" 用于 WLAN/Ethernet,或 "BLE" 用于 BLE Low Energy |
| device_ip | string | 适配器的 IP 地址(在 connection_type = "LAN" 时使用) |
| device_name | string | BLE 设备的序列号或名称(在 connection_type = "BLE" 时使用) |
| logs_directory | string | 日志文件目录。如果为空,日志保存在 <config_dir>/sdlogs |
| debug | boolean | true - 启用日志记录,false - 禁用 |
通过 WLAN/LAN 连接:
{
"current_device": 0,
"devices": [
{
"name": "ScanDoc FD",
"connection_type": "LAN",
"device_ip": "192.168.1.100",
"device_name": ""
}
],
"debug": false
}
通过 BLE 连接:
{
"current_device": 0,
"devices": [
{
"name": "ScanDoc FD",
"connection_type": "BLE",
"device_ip": "",
"device_name": "N4999"
}
],
"debug": true
}
PassThruOpen() → 与适配器建立连接
↓
PassThruConnect() → 打开与 ECU 的通信通道
↓
PassThruReadMsg() / PassThruWriteMsg() → 消息收发
↓
PassThruDisconnect() → 关闭通道
↓
PassThruClose() → 释放适配器资源
PassThruClose()。否则下一次连接将返回错误 ERR_DEVICE_IN_USE。
连接超时:500 毫秒。当适配器不可用时,函数将在超时后返回
ERR_DEVICE_NOT_CONNECTED。
| 代码 | 说明 | 可能的原因与解决方法 |
|---|---|---|
| STATUS_NOERROR | 函数执行成功 | - |
| ERR_DEVICE_NOT_CONNECTED | 无法与适配器建立连接 |
|
| ERR_DEVICE_IN_USE | 设备已在使用中 |
|
| ERR_NULL_PARAMETER | 未指定 pDeviceID 指针 | 请传入指向变量的有效指针 |
| ERR_FAILED | 内部错误 |
|
#include "j2534_dll.hpp"
unsigned long DeviceID;
long ret;
// pName = NULL - 从 j2534.json 读取设置
ret = PassThruOpen(NULL, &DeviceID);
if (ret != STATUS_NOERROR)
{
char error[256];
PassThruGetLastError(error);
printf("Error: %s\n", error);
return;
}
// 设备操作...
// 务必关闭连接
PassThruClose(DeviceID);
本示例使用 JNI 包装器 J2534JNI 从 Kotlin 调用 J2534 的原生函数。
// J2534JNI 是用于 JNI 的包装类
val j2534 = J2534JNI(context)
// 传入 null - 从 j2534.json 读取设置
val deviceResult = j2534.ptOpen(null)
if (deviceResult.status == STATUS_NOERROR) {
val deviceID = deviceResult.deviceId
Log.i("J2534", "适配器已打开,DeviceID: $deviceID")
// 设备操作...
// 务必关闭连接
j2534.ptClose(deviceID)
} else {
Log.e("J2534", "错误: ${deviceResult.status}")
}
from ctypes import *
import platform
# 根据操作系统加载库
if platform.system() == "Windows":
j2534 = windll.LoadLibrary("j2534sd_v04_04_x64.dll")
elif platform.system() == "Darwin":
j2534 = cdll.LoadLibrary("libj2534_v04_04.dylib")
else:
j2534 = cdll.LoadLibrary("libj2534_v04_04.so")
device_id = c_ulong()
# pName = None - 从 j2534.json 读取设置
ret = j2534.PassThruOpen(None, byref(device_id))
if ret == 0: # STATUS_NOERROR
print(f"适配器已打开,DeviceID: {device_id.value}")
# 设备操作...
# 务必关闭连接
j2534.PassThruClose(device_id)
else:
error = create_string_buffer(256)
j2534.PassThruGetLastError(error)
print(f"错误: {error.value.decode()}")
using System;
using System.Runtime.InteropServices;
class J2534
{
[DllImport("j2534sd_v04_04_x64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int PassThruOpen(IntPtr pName, out uint pDeviceID);
[DllImport("j2534sd_v04_04_x64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int PassThruClose(uint DeviceID);
[DllImport("j2534sd_v04_04_x64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int PassThruGetLastError(
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pErrorDescription);
}
// 使用方法:
uint deviceId;
// IntPtr.Zero = NULL - 从 j2534.json 读取设置
int ret = J2534.PassThruOpen(IntPtr.Zero, out deviceId);
if (ret == 0) // STATUS_NOERROR
{
Console.WriteLine($"适配器已打开,DeviceID: {deviceId}");
// 设备操作...
// 务必关闭连接
J2534.PassThruClose(deviceId);
}
else
{
var error = new System.Text.StringBuilder(256);
J2534.PassThruGetLastError(error);
Console.WriteLine($"错误: {error}");
}