Quantex GmbH
DE RU EN EL
Your region: Europe

PassThruGetNextDevice v5.0

Retrieve device information

Last modified:

Description

The function sequentially returns information about the devices found by the most recent call to PassThruScanForDevices(). Each call returns information about the next device in the list. The order of devices may differ on each new scan.

long PassThruGetNextDevice(SDEVICE* psDevice)
Note: A call to PassThruOpen() is not required before using this function. It is sufficient to first call PassThruScanForDevices().

Parameters

SDEVICE structure

typedef struct {
    char          DeviceName[80];       // Device name (ASCII, null-terminated)
    unsigned long DeviceAvailable;      // Device availability state
    unsigned long DeviceDLLFWStatus;    // DLL and firmware compatibility
    unsigned long DeviceConnectMedia;   // Connection type (wired/wireless)
    unsigned long DeviceConnectSpeed;   // Connection speed (bit/s)
    unsigned long DeviceSignalQuality;  // Signal quality (0-100%, 0xFFFFFFFF if unknown)
    unsigned long DeviceSignalStrength; // Signal strength (0-100%, 0xFFFFFFFF if unknown)
} SDEVICE;

Structure field descriptions

Field Description
DeviceName ASCII string with the device name (up to 80 characters including the NULL terminator). The name is intended to be shown to the user and must uniquely identify the device. Examples: "ScanDoc FD #N4999", "ScanDoc PRO (WiFi)"
DeviceAvailable Device availability state. See the values table
DeviceDLLFWStatus DLL/firmware compatibility status of the device. See the values table
DeviceConnectMedia Connection type to the device. See the values table
DeviceConnectSpeed Connection speed to the device in bits per second
DeviceSignalQuality Signal quality from 0 to 100%. 0xFFFFFFFF if not determined
DeviceSignalStrength Signal strength from 0 to 100%. 0xFFFFFFFF if not determined

DeviceAvailable values

Constant Value Description
DEVICE_STATE_UNKNOWN 0 Device state could not be determined
DEVICE_AVAILABLE 1 Device is free and ready to connect
DEVICE_IN_USE 2 Device is already in use (opened by another application)

DeviceDLLFWStatus values

Constant Value Description
DEVICE_DLL_FW_COMPATIBILTY_UNKNOWN 0 DLL/firmware compatibility could not be determined
DEVICE_DLL_FW_COMPATIBLE 1 DLL and firmware are compatible
DEVICE_DLL_OR_FW_NOT_COMPATIBLE 2 DLL or firmware is outdated or incompatible
DEVICE_DLL_NOT_COMPATIBLE 3 DLL is outdated or incompatible with the device
DEVICE_FW_NOT_COMPATIBLE 4 Device firmware is outdated or incompatible with the DLL

DeviceConnectMedia values

Constant Value Description
DEVICE_CONN_UNKNOWN 0 Connection type could not be determined
DEVICE_CONN_WIRELESS 1 Wireless connection (WiFi, BLE)
DEVICE_CONN_WIRED 2 Wired connection (USB, Ethernet)

Function call order

PassThruScanForDevices(&count)  → Get the number of devices
    ↓
for (i = 0; i < count; i++) {
    PassThruGetNextDevice(&device) → Get device information
}
    ↓
PassThruOpen(deviceName)        → Open the selected device
Important: The application is not required to call PassThruGetNextDevice() for every device. The iteration can be stopped at any time. However, subsequent calls will continue to return the remaining devices until the end of the list, until the DLL is unloaded, or until a new call to PassThruScanForDevices().

Returned error codes

Code Description Possible causes and solutions
STATUS_NOERROR Function completed successfully The psDevice structure has been populated with device information
ERR_NULL_PARAMETER psDevice pointer not provided Pass a valid pointer to an SDEVICE structure
ERR_EXCEEDED_LIMIT All devices have already been enumerated
  • Information about all discovered devices has already been returned
  • Solution: call PassThruScanForDevices() to perform a new scan
ERR_BUFFER_EMPTY The device list is empty
  • PassThruScanForDevices() did not find any devices
  • PassThruScanForDevices() has not been called
  • Solution: call PassThruScanForDevices() and check pDeviceCount
ERR_CONCURRENT_API_CALL A J2534 API function is already running
  • Another J2534 function has not yet completed
  • Solution: wait for the previous call to complete
ERR_NOT_SUPPORTED Function not supported
  • The DLL does not support dynamic device enumeration
  • Solution: call PassThruOpen() directly
ERR_FAILED Internal error
  • Use PassThruGetLastError() to obtain details

Examples

C/C++ example

#include "j2534_dll.hpp"

unsigned long deviceCount = 0;

// Scan for devices
long ret = PassThruScanForDevices(&deviceCount);
if (ret != STATUS_NOERROR || deviceCount == 0)
{
    printf("No devices found\n");
    return;
}

printf("Devices found: %lu\n", deviceCount);

// Iterate through all discovered devices
SDEVICE device;
for (unsigned long i = 0; i < deviceCount; i++)
{
    ret = PassThruGetNextDevice(&device);
    if (ret != STATUS_NOERROR)
    {
        break;
    }

    printf("\nDevice %lu:\n", i + 1);
    printf("  Name: %s\n", device.DeviceName);
    printf("  Available: %s\n",
        device.DeviceAvailable == DEVICE_AVAILABLE ? "Yes" :
        device.DeviceAvailable == DEVICE_IN_USE ? "In use" : "Unknown");
    printf("  Compatibility: %s\n",
        device.DeviceDLLFWStatus == DEVICE_DLL_FW_COMPATIBLE ? "OK" : "Update required");
    printf("  Connection: %s\n",
        device.DeviceConnectMedia == DEVICE_CONN_WIRELESS ? "Wireless" :
        device.DeviceConnectMedia == DEVICE_CONN_WIRED ? "Wired" : "Unknown");

    if (device.DeviceSignalStrength != 0xFFFFFFFF)
    {
        printf("  Signal strength: %lu%%\n", device.DeviceSignalStrength);
    }
}

// Connect to the first available device
// (a real application should let the user choose)
unsigned long deviceID;
ret = PassThruOpen(device.DeviceName, &deviceID);
if (ret == STATUS_NOERROR)
{
    printf("\nConnected to: %s\n", device.DeviceName);
    // ... work with the device ...
    PassThruClose(deviceID);
}

Kotlin example (Android)

val j2534 = J2534JNI(context)

// Scan for devices
val scanResult = j2534.ptScanForDevices()
if (scanResult.status != STATUS_NOERROR || scanResult.deviceCount == 0) {
    Log.w("J2534", "No devices found")
    return
}

// Collect information about all devices
val devices = mutableListOf<DeviceInfo>()

for (i in 0 until scanResult.deviceCount) {
    val result = j2534.ptGetNextDevice()
    if (result.status == STATUS_NOERROR) {
        devices.add(result.device)
        Log.i("J2534", """
            Device ${i + 1}:
              Name: ${result.device.name}
              Available: ${result.device.available}
              Signal: ${result.device.signalStrength}%
        """.trimIndent())
    }
}

// Show the device selection dialog
showDeviceSelectionDialog(devices) { selectedDevice ->
    val openResult = j2534.ptOpen(selectedDevice.name)
    if (openResult.status == STATUS_NOERROR) {
        // Work with the device...
    }
}

Python example (ctypes)

from ctypes import *
import platform

# Load the library
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")

# SDEVICE structure definition
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)
    ]

# Constants
DEVICE_AVAILABLE = 1
DEVICE_IN_USE = 2
DEVICE_DLL_FW_COMPATIBLE = 1
DEVICE_CONN_WIRELESS = 1
DEVICE_CONN_WIRED = 2

# Scan for devices
device_count = c_ulong()
ret = j2534.PassThruScanForDevices(byref(device_count))

if ret != 0 or device_count.value == 0:
    print("No devices found")
    exit()

print(f"Devices found: {device_count.value}\n")

# Get information about each device
devices = []
for i in range(device_count.value):
    device = SDEVICE()
    ret = j2534.PassThruGetNextDevice(byref(device))

    if ret == 0:
        devices.append(device)
        name = device.DeviceName.decode('utf-8')
        available = "Yes" if device.DeviceAvailable == DEVICE_AVAILABLE else \
                    "In use" if device.DeviceAvailable == DEVICE_IN_USE else "?"
        media = "WiFi/BLE" if device.DeviceConnectMedia == DEVICE_CONN_WIRELESS else \
                "USB/LAN" if device.DeviceConnectMedia == DEVICE_CONN_WIRED else "?"

        print(f"Device {i + 1}:")
        print(f"  Name: {name}")
        print(f"  Available: {available}")
        print(f"  Connection: {media}")

        if device.DeviceSignalStrength != 0xFFFFFFFF:
            print(f"  Signal: {device.DeviceSignalStrength}%")
        print()

# Connect to the first available device
if devices:
    device_id = c_ulong()
    ret = j2534.PassThruOpen(devices[0].DeviceName, byref(device_id))
    if ret == 0:
        print(f"Connected to: {devices[0].DeviceName.decode()}")
        # ... work with the device ...
        j2534.PassThruClose(device_id)

C# example (P/Invoke)

using System;
using System.Collections.Generic;
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;
}

public enum DeviceAvailable : uint
{
    Unknown = 0,
    Available = 1,
    InUse = 2
}

public enum DeviceConnectMedia : uint
{
    Unknown = 0,
    Wireless = 1,
    Wired = 2
}

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

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

    [DllImport("j2534sd_v05_00_x64.dll")]
    public static extern int PassThruOpen(string pName, out uint pDeviceID);

    [DllImport("j2534sd_v05_00_x64.dll")]
    public static extern int PassThruClose(uint DeviceID);
}

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

if (ret != 0 || deviceCount == 0)
{
    Console.WriteLine("No devices found");
    return;
}

Console.WriteLine($"Devices found: {deviceCount}\n");

var devices = new List<SDEVICE>();

for (uint i = 0; i < deviceCount; i++)
{
    SDEVICE device;
    ret = J2534.PassThruGetNextDevice(out device);

    if (ret == 0)
    {
        devices.Add(device);
        Console.WriteLine($"Device {i + 1}:");
        Console.WriteLine($"  Name: {device.DeviceName}");
        Console.WriteLine($"  Available: {(DeviceAvailable)device.DeviceAvailable}");
        Console.WriteLine($"  Connection: {(DeviceConnectMedia)device.DeviceConnectMedia}");

        if (device.DeviceSignalStrength != 0xFFFFFFFF)
            Console.WriteLine($"  Signal: {device.DeviceSignalStrength}%");

        Console.WriteLine();
    }
}

// Connect to the first device
if (devices.Count > 0)
{
    uint deviceId;
    ret = J2534.PassThruOpen(devices[0].DeviceName, out deviceId);
    if (ret == 0)
    {
        Console.WriteLine($"Connected to: {devices[0].DeviceName}");
        // ... work with the device ...
        J2534.PassThruClose(deviceId);
    }
}

Related functions