Escaneo de dispositivos
Última modificación:
La función escanea los adaptadores ScanDoc disponibles y devuelve el número de dispositivos encontrados. Esta función forma parte del estándar SAE J2534-1 y está destinada a la detección de dispositivos antes de llamar a PassThruOpen().
long PassThruScanForDevices(unsigned long* pDeviceCount)
PassThruGetNextDevice() para obtener información sobre cada dispositivo encontrado.
unsigned long, asignada por la aplicación. Tras la ejecución correcta de la función, la variable contendrá el número de dispositivos encontrados.La función realiza las siguientes acciones:
pDeviceCountPassThruGetNextDevice()PassThruScanForDevices() → Escanear los dispositivos disponibles
↓
PassThruGetNextDevice() → Obtener información de cada dispositivo (repetir N veces)
↓
PassThruOpen() → Abrir conexión con el dispositivo seleccionado
↓
...
PassThruScanForDevices() no es obligatoria. Si el nombre del dispositivo se conoce de antemano, se puede llamar directamente a
PassThruOpen() con los parámetros de conexión necesarios.
Tiempo de espera del escaneo de red: 2000 ms. Tiempo de espera del escaneo BLE: 3000 ms. El tiempo total de ejecución de la función puede ser de hasta 5 segundos con BLE activado.
| Código | Descripción | Posibles causas y soluciones |
|---|---|---|
| STATUS_NOERROR | La función se ejecutó correctamente | El número de dispositivos encontrados se ha registrado en pDeviceCount (puede ser 0) |
| ERR_NULL_PARAMETER | No se ha indicado el puntero pDeviceCount | Pase un puntero válido a una variable unsigned long |
| ERR_CONCURRENT_API_CALL | Ya se está ejecutando una función de la API J2534 |
|
| ERR_NOT_SUPPORTED | La función no es compatible |
|
| ERR_FAILED | Error interno |
|
#include "j2534_dll.hpp"
unsigned long deviceCount = 0;
// Escaneamos los dispositivos
long ret = PassThruScanForDevices(&deviceCount);
if (ret != STATUS_NOERROR)
{
char error[256];
PassThruGetLastError(error);
printf("Error de escaneo: %s\n", error);
return;
}
printf("Dispositivos encontrados: %lu\n", deviceCount);
if (deviceCount == 0)
{
printf("No se encontraron dispositivos\n");
return;
}
// Obtenemos información de cada dispositivo
SDEVICE deviceInfo;
for (unsigned long i = 0; i < deviceCount; i++)
{
ret = PassThruGetNextDevice(&deviceInfo);
if (ret == STATUS_NOERROR)
{
printf("Dispositivo %lu: %s\n", i + 1, deviceInfo.DeviceName);
}
}
val j2534 = J2534JNI(context)
// Escaneamos los dispositivos
val scanResult = j2534.ptScanForDevices()
if (scanResult.status == STATUS_NOERROR) {
Log.i("J2534", "Dispositivos encontrados: ${scanResult.deviceCount}")
// Obtenemos información de cada dispositivo
for (i in 0 until scanResult.deviceCount) {
val deviceInfo = j2534.ptGetNextDevice()
if (deviceInfo.status == STATUS_NOERROR) {
Log.i("J2534", "Dispositivo ${i + 1}: ${deviceInfo.deviceName}")
}
}
} else {
Log.e("J2534", "Error de escaneo: ${scanResult.status}")
}
from ctypes import *
import platform
# Carga de la biblioteca
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()
# Escaneamos los dispositivos
ret = j2534.PassThruScanForDevices(byref(device_count))
if ret == 0: # STATUS_NOERROR
print(f"Dispositivos encontrados: {device_count.value}")
# Estructura SDEVICE para obtener información del dispositivo
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)
]
# Obtenemos información de cada dispositivo
for i in range(device_count.value):
device_info = SDEVICE()
ret = j2534.PassThruGetNextDevice(byref(device_info))
if ret == 0:
print(f"Dispositivo {i + 1}: {device_info.DeviceName.decode()}")
else:
error = create_string_buffer(256)
j2534.PassThruGetLastError(error)
print(f"Error: {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);
}
// Uso:
uint deviceCount;
int ret = J2534.PassThruScanForDevices(out deviceCount);
if (ret == 0) // STATUS_NOERROR
{
Console.WriteLine($"Dispositivos encontrados: {deviceCount}");
for (uint i = 0; i < deviceCount; i++)
{
SDEVICE deviceInfo;
ret = J2534.PassThruGetNextDevice(out deviceInfo);
if (ret == 0)
{
Console.WriteLine($"Dispositivo {i + 1}: {deviceInfo.DeviceName}");
}
}
}
else
{
var error = new System.Text.StringBuilder(256);
J2534.PassThruGetLastError(error);
Console.WriteLine($"Error: {error}");
}
PassThruGetNextDevice() - Obtención de información sobre los dispositivos encontradosPassThruOpen() - Establecimiento de la conexión con el adaptador