Odczyt odebranych komunikatów
Ostatnia zmiana:
Funkcja odczytuje z kolejki kanału odebrane komunikaty. Adapter może odebrać maksymalnie 100 komunikatów na jedną kolejkę dla jednego kanału i dysponuje 64 KB wolnej pamięci dla wszystkich kolejek. Po zapełnieniu kolejki lub całej wolnej pamięci odbiór komunikatów zostaje wstrzymany.
long PassThruReadMsgs(unsigned long ChannelID, PASSTHRU_MSG* pMsg, unsigned long* pNumMsgs, unsigned long Timeout)
START_OF_MESSAGE ze
znacznikiem czasu początku odbioru. Po nim następuje główny komunikat ze znacznikiem czasu końca odbioru.
PassThruConnect.ERR_TIMEOUT nie jest generowany).| Kod | Opis | Możliwe przyczyny i rozwiązania |
|---|---|---|
| STATUS_NOERROR | Funkcja wykonana pomyślnie | - |
| ERR_CONCURRENT_API_CALL v5.0 | Równoległe wywołanie API |
|
| ERR_DEVICE_NOT_OPEN v5.0 | Urządzenie nie jest otwarte |
|
| ERR_DEVICE_NOT_CONNECTED | Brak połączenia z adapterem |
|
| ERR_INVALID_DEVICE_ID | Nieprawidłowy identyfikator urządzenia |
|
| ERR_INVALID_CHANNEL_ID | Nieprawidłowy identyfikator kanału |
|
| ERR_NOT_SUPPORTED v5.0 | Funkcja nie jest obsługiwana |
|
| ERR_NULL_PARAMETER | Nie podano wskaźnika na bufor |
|
| ERR_TIMEOUT | Upłynął limit czasu oczekiwania |
|
| ERR_BUFFER_EMPTY | Kolejka odbiorcza jest pusta |
|
| ERR_BUFFER_OVERFLOW | Kolejka odbiorcza została przepełniona |
|
| ERR_BUFFER_TOO_SMALL v5.0 | Bufor jest zbyt mały |
|
| ERR_NO_FLOW_CONTROL | Nie ustawiono filtra Flow Control |
|
| ERR_FAILED | Błąd wewnętrzny |
|
#include "j2534_dll.hpp"
unsigned long ChannelID; // ID uzyskany z PassThruConnect
PASSTHRU_MSG Msgs[10]; // Bufor na komunikaty
unsigned long NumMsgs = 10; // Żądamy do 10 komunikatów
unsigned long Timeout = 1000; // Limit czasu 1000 ms
long ret = PassThruReadMsgs(ChannelID, &Msgs[0], &NumMsgs, Timeout);
if (ret == STATUS_NOERROR || ret == ERR_TIMEOUT)
{
// Obsługa odebranych komunikatów (NumMsgs sztuk)
for (unsigned long i = 0; i < NumMsgs; i++) {
if (Msgs[i].RxStatus & START_OF_MESSAGE) {
// Wskaźnik początku komunikatu
continue;
}
// Obsługa danych Msgs[i].Data, Msgs[i].DataSize
}
}
else
{
char error[256];
PassThruGetLastError(error);
printf("Error: %s\n", error);
}
// channelID uzyskany wcześniej z ptConnect
val numMsgsToRead = 10
val timeout = 1000 // ms
val result = j2534.ptReadMsgs(channelID, numMsgsToRead, timeout)
if (result.status == STATUS_NOERROR || result.status == ERR_TIMEOUT) {
// Pomyślnie odczytano result.msgs.size komunikatów
for (msg in result.msgs) {
if (msg.rxStatus and START_OF_MESSAGE != 0) {
continue // Pomijamy wskaźnik początku
}
Log.i("J2534", "Odebrano: ${msg.data.toHexString()}")
}
} else {
Log.e("J2534", "Błąd odczytu: ${result.status}")
}
from ctypes import *
import platform
# Ładowanie biblioteki
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")
# Struktura 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 uzyskany wcześniej z 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"Odebrano {num_msgs.value} komunikatów")
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"Dane: {data.hex()}")
else:
error = create_string_buffer(256)
j2534.PassThruGetLastError(error)
print(f"Błąd: {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);
}
// Użycie:
// channelId uzyskany wcześniej z 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($"Odebrano {numMsgs} komunikatów");
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($"Dane: {BitConverter.ToString(data)}");
}
}
else
{
var error = new System.Text.StringBuilder(256);
J2534.PassThruGetLastError(error);
Console.WriteLine($"Błąd: {error}");
}