扫描设备
最后修改:
该函数扫描可用的 ScanDoc 适配器并返回找到的设备数量。此函数是 SAE J2534-1 标准的一部分,用于在调用 PassThruOpen() 之前发现设备。
long PassThruScanForDevices(unsigned long* pDeviceCount)
PassThruGetNextDevice() 获取每个已找到设备的信息。
unsigned long 类型变量的指针。函数执行成功后,该变量将包含找到的设备数量。该函数执行以下操作:
pDeviceCountPassThruGetNextDevice() 获取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 函数正在执行 |
|
| ERR_NOT_SUPPORTED | 函数不受支持 |
|
| ERR_FAILED | 内部错误 |
|
#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);
}
}
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}")
}
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()}")
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}");
}
PassThruGetNextDevice() - 获取已找到设备的信息PassThruOpen() - 与适配器建立连接