Lectura de los mensajes recibidos
Última modificación:
La función lee de la cola del canal los mensajes recibidos. El adaptador puede recibir un máximo de 100 mensajes por cola para un canal y dispone de 64 KB de memoria libre para todas las colas. Cuando se llena la cola o toda la memoria libre, la recepción de mensajes se suspende.
long PassThruReadMsgs(unsigned long ChannelID, PASSTHRU_MSG* pMsg, unsigned long* pNumMsgs, unsigned long Timeout)
START_OF_MESSAGE con
la marca de tiempo del inicio de la recepción. A continuación le sigue el mensaje principal con la marca de tiempo del final de la recepción.
PassThruConnect.ERR_TIMEOUT).| Código | Descripción | Posibles causas y soluciones |
|---|---|---|
| STATUS_NOERROR | La función se ejecutó correctamente | - |
| ERR_CONCURRENT_API_CALL v5.0 | Llamada paralela a la API |
|
| ERR_DEVICE_NOT_OPEN v5.0 | El dispositivo no está abierto |
|
| ERR_DEVICE_NOT_CONNECTED | No hay conexión con el adaptador |
|
| ERR_INVALID_DEVICE_ID | Identificador de dispositivo no válido |
|
| ERR_INVALID_CHANNEL_ID | Identificador de canal no válido |
|
| ERR_NOT_SUPPORTED v5.0 | La función no es compatible |
|
| ERR_NULL_PARAMETER | No se indicó el puntero al búfer |
|
| ERR_TIMEOUT | Se agotó el tiempo de espera |
|
| ERR_BUFFER_EMPTY | La cola de recepción está vacía |
|
| ERR_BUFFER_OVERFLOW | La cola de recepción se ha desbordado |
|
| ERR_BUFFER_TOO_SMALL v5.0 | El búfer es demasiado pequeño |
|
| ERR_NO_FLOW_CONTROL | No se ha establecido el filtro Flow Control |
|
| ERR_FAILED | Error interno |
|
#include "j2534_dll.hpp"
unsigned long ChannelID; // ID obtenido de PassThruConnect
PASSTHRU_MSG Msgs[10]; // Búfer para los mensajes
unsigned long NumMsgs = 10; // Solicitamos hasta 10 mensajes
unsigned long Timeout = 1000; // Tiempo de espera 1000 ms
long ret = PassThruReadMsgs(ChannelID, &Msgs[0], &NumMsgs, Timeout);
if (ret == STATUS_NOERROR || ret == ERR_TIMEOUT)
{
// Procesamiento de los mensajes recibidos (NumMsgs unidades)
for (unsigned long i = 0; i < NumMsgs; i++) {
if (Msgs[i].RxStatus & START_OF_MESSAGE) {
// Indicador de inicio del mensaje
continue;
}
// Procesamiento de los datos Msgs[i].Data, Msgs[i].DataSize
}
}
else
{
char error[256];
PassThruGetLastError(error);
printf("Error: %s\n", error);
}
// channelID obtenido previamente de ptConnect
val numMsgsToRead = 10
val timeout = 1000 // ms
val result = j2534.ptReadMsgs(channelID, numMsgsToRead, timeout)
if (result.status == STATUS_NOERROR || result.status == ERR_TIMEOUT) {
// Se leyeron correctamente result.msgs.size mensajes
for (msg in result.msgs) {
if (msg.rxStatus and START_OF_MESSAGE != 0) {
continue // Omitimos el indicador de inicio
}
Log.i("J2534", "Recibido: ${msg.data.toHexString()}")
}
} else {
Log.e("J2534", "Error de lectura: ${result.status}")
}
from ctypes import *
import platform
# Carga de la biblioteca
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")
# Estructura PASSTHRU_MSG
class PASSTHRU_MSG(Structure):
_fields_ = [
("ProtocolID", c_ulong),
("RxStatus", c_ulong),
("TxFlags", c_ulong),
("Timestamp", c_ulong),
("DataSize", c_ulong),
("ExtraDataIndex", c_ulong),
("Data", c_ubyte * 4128)
]
# channel_id obtenido previamente de PassThruConnect
msgs = (PASSTHRU_MSG * 10)()
num_msgs = c_ulong(10)
timeout = c_ulong(1000)
ret = j2534.PassThruReadMsgs(channel_id, byref(msgs[0]), byref(num_msgs), timeout)
if ret == 0 or ret == 0x09: # STATUS_NOERROR or ERR_TIMEOUT
print(f"Recibidos {num_msgs.value} mensajes")
for i in range(num_msgs.value):
if msgs[i].RxStatus & 0x02: # START_OF_MESSAGE
continue
data = bytes(msgs[i].Data[:msgs[i].DataSize])
print(f"Datos: {data.hex()}")
else:
error = create_string_buffer(256)
j2534.PassThruGetLastError(error)
print(f"Error: {error.value.decode()}")
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct PASSTHRU_MSG
{
public uint ProtocolID;
public uint RxStatus;
public uint TxFlags;
public uint Timestamp;
public uint DataSize;
public uint ExtraDataIndex;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4128)]
public byte[] Data;
}
class J2534
{
[DllImport("j2534sd_v04_04_x64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int PassThruReadMsgs(
uint ChannelID,
[In, Out] PASSTHRU_MSG[] pMsg,
ref uint pNumMsgs,
uint Timeout);
[DllImport("j2534sd_v04_04_x64.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int PassThruGetLastError(
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pErrorDescription);
}
// Uso:
// channelId obtenido previamente de PassThruConnect
PASSTHRU_MSG[] msgs = new PASSTHRU_MSG[10];
for (int i = 0; i < msgs.Length; i++)
msgs[i].Data = new byte[4128];
uint numMsgs = 10;
uint timeout = 1000;
int ret = J2534.PassThruReadMsgs(channelId, msgs, ref numMsgs, timeout);
if (ret == 0 || ret == 0x09) // STATUS_NOERROR or ERR_TIMEOUT
{
Console.WriteLine($"Recibidos {numMsgs} mensajes");
for (uint i = 0; i < numMsgs; i++)
{
if ((msgs[i].RxStatus & 0x02) != 0) // START_OF_MESSAGE
continue;
byte[] data = new byte[msgs[i].DataSize];
Array.Copy(msgs[i].Data, data, msgs[i].DataSize);
Console.WriteLine($"Datos: {BitConverter.ToString(data)}");
}
}
else
{
var error = new System.Text.StringBuilder(256);
J2534.PassThruGetLastError(error);
Console.WriteLine($"Error: {error}");
}