Quantex GmbH
您的地区:欧洲

PassThruScanForDevices v5.0

扫描设备

最后修改:

说明

该函数扫描可用的 ScanDoc 适配器并返回找到的设备数量。此函数是 SAE J2534-1 标准的一部分,用于在调用 PassThruOpen() 之前发现设备。

long PassThruScanForDevices(unsigned long* pDeviceCount)
注意:调用此函数后,使用 PassThruGetNextDevice() 获取每个已找到设备的信息。

参数

工作算法

该函数执行以下操作:

  1. 在局域网(LAN/WLAN)中扫描可用的 ScanDoc 设备
  2. 启用 BLE 时扫描 BLE 设备
  3. 将找到的设备数量写入 pDeviceCount
  4. 保存设备列表,以便后续通过 PassThruGetNextDevice() 获取

函数调用顺序

PassThruScanForDevices()  → 扫描可用设备
    ↓
PassThruGetNextDevice()   → 获取每个设备的信息(重复 N 次)
    ↓
PassThruOpen()            → 与选定的设备建立连接
    ↓
...
重要:调用 PassThruScanForDevices() 不是必需的。如果事先已知设备名称,可以直接使用所需的连接参数调用 PassThruOpen()

超时

网络扫描超时:2000 毫秒。BLE 扫描超时:3000 毫秒。启用 BLE 时,函数的总执行时间最长可达 5 秒。

返回的错误代码

代码 说明 可能的原因及解决方法
STATUS_NOERROR 函数执行成功 找到的设备数量已写入 pDeviceCount(可能为 0)
ERR_NULL_PARAMETER 未指定 pDeviceCount 指针 请传入指向 unsigned long 变量的有效指针
ERR_CONCURRENT_API_CALL J2534 API 函数正在执行
  • 另一个 J2534 函数尚未执行完毕
  • 解决方法:请等待上一次调用完成
ERR_NOT_SUPPORTED 函数不受支持
  • DLL 不支持动态设备扫描
  • 解决方法:使用已知参数直接调用 PassThruOpen()
ERR_FAILED 内部错误
  • 网络接口错误
  • BLE 初始化错误
  • 解决方法:使用 PassThruGetLastError() 获取详细信息

示例

C/C++ 示例

#include "j2534_dll.hpp"

unsigned long deviceCount = 0;

// 扫描设备
long ret = PassThruScanForDevices(&deviceCount);
if (ret != STATUS_NOERROR)
{
    char error[256];
    PassThruGetLastError(error);
    printf("扫描错误: %s\n", error);
    return;
}

printf("找到的设备数量: %lu\n", deviceCount);

if (deviceCount == 0)
{
    printf("未找到设备\n");
    return;
}

// 获取每个设备的信息
SDEVICE deviceInfo;
for (unsigned long i = 0; i < deviceCount; i++)
{
    ret = PassThruGetNextDevice(&deviceInfo);
    if (ret == STATUS_NOERROR)
    {
        printf("设备 %lu: %s\n", i + 1, deviceInfo.DeviceName);
    }
}

Kotlin(Android)示例

val j2534 = J2534JNI(context)

// 扫描设备
val scanResult = j2534.ptScanForDevices()

if (scanResult.status == STATUS_NOERROR) {
    Log.i("J2534", "找到的设备数量: ${scanResult.deviceCount}")

    // 获取每个设备的信息
    for (i in 0 until scanResult.deviceCount) {
        val deviceInfo = j2534.ptGetNextDevice()
        if (deviceInfo.status == STATUS_NOERROR) {
            Log.i("J2534", "设备 ${i + 1}: ${deviceInfo.deviceName}")
        }
    }
} else {
    Log.e("J2534", "扫描错误: ${scanResult.status}")
}

Python(ctypes)示例

from ctypes import *
import platform

# 加载库
if platform.system() == "Windows":
    j2534 = windll.LoadLibrary("j2534sd_v05_00_x64.dll")
elif platform.system() == "Darwin":
    j2534 = cdll.LoadLibrary("libj2534_v05_00.dylib")
else:
    j2534 = cdll.LoadLibrary("libj2534_v05_00.so")

device_count = c_ulong()

# 扫描设备
ret = j2534.PassThruScanForDevices(byref(device_count))

if ret == 0:  # STATUS_NOERROR
    print(f"找到的设备数量: {device_count.value}")

    # 用于获取设备信息的 SDEVICE 结构体
    class SDEVICE(Structure):
        _fields_ = [
            ("DeviceName", c_char * 80),
            ("DeviceAvailable", c_ulong),
            ("DeviceDLLFWStatus", c_ulong),
            ("DeviceConnectMedia", c_ulong),
            ("DeviceConnectSpeed", c_ulong),
            ("DeviceSignalQuality", c_ulong),
            ("DeviceSignalStrength", c_ulong)
        ]

    # 获取每个设备的信息
    for i in range(device_count.value):
        device_info = SDEVICE()
        ret = j2534.PassThruGetNextDevice(byref(device_info))
        if ret == 0:
            print(f"设备 {i + 1}: {device_info.DeviceName.decode()}")
else:
    error = create_string_buffer(256)
    j2534.PassThruGetLastError(error)
    print(f"错误: {error.value.decode()}")

C#(P/Invoke)示例

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SDEVICE
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    public string DeviceName;
    public uint DeviceAvailable;
    public uint DeviceDLLFWStatus;
    public uint DeviceConnectMedia;
    public uint DeviceConnectSpeed;
    public uint DeviceSignalQuality;
    public uint DeviceSignalStrength;
}

class J2534
{
    [DllImport("j2534sd_v05_00_x64.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int PassThruScanForDevices(out uint pDeviceCount);

    [DllImport("j2534sd_v05_00_x64.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int PassThruGetNextDevice(out SDEVICE psDevice);

    [DllImport("j2534sd_v05_00_x64.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int PassThruGetLastError(
        [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pErrorDescription);
}

// 用法:
uint deviceCount;
int ret = J2534.PassThruScanForDevices(out deviceCount);

if (ret == 0) // STATUS_NOERROR
{
    Console.WriteLine($"找到的设备数量: {deviceCount}");

    for (uint i = 0; i < deviceCount; i++)
    {
        SDEVICE deviceInfo;
        ret = J2534.PassThruGetNextDevice(out deviceInfo);
        if (ret == 0)
        {
            Console.WriteLine($"设备 {i + 1}: {deviceInfo.DeviceName}");
        }
    }
}
else
{
    var error = new System.Text.StringBuilder(256);
    J2534.PassThruGetLastError(error);
    Console.WriteLine($"错误: {error}");
}

相关函数