Kvaser ISO-TP SDK
Loading...
Searching...
No Matches
/examples/c_obd_reader/obd_reader.c
/*
* Copyright 2025 by KVASER AB, SWEDEN
*
* WWW: https://www.kvaser.com
*
* This software is furnished under a license and may be used and copied
* only in accordance with the terms of such license.
*
*/
#ifdef _WIN32
#include <windows.h>
#else
#define WINAPI
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// Change the include path as needed
#ifdef _WIN32
#include "../../include/kvisotplib.h"
#else
#include "kvisotplib.h"
#endif
// Constants, change if using a different library path
#ifdef _WIN32
const char *LIBRARY_PATH = "C:\\Program Files (x86)\\Kvaser\\IsoTp\\bin_x64\\kvisotplib.dll";
#else
const char *LIBRARY_PATH = "libkvisotplib.so";
#endif
// Callback for First Frame indication
void WINAPI ff_indication_callback(enum MType m_type,
unsigned int can_id,
enum TAType ta_type,
unsigned char address_extension,
unsigned int length,
unsigned long long timestamp)
{
printf("First Frame indication callback triggered, length: %u, timestamp: %llu\n", length, timestamp);
}
// Callback for complete message indication
void WINAPI indication_callback(enum MType m_type,
unsigned int can_id,
enum TAType ta_type,
unsigned char address_extension,
unsigned int length,
unsigned long long timestamp,
const unsigned char *payload,
unsigned int payload_length,
enum NResult result)
{
printf("Indication callback triggered, result: %d, timestamp: %llu\n", result, timestamp);
printf("Payload: ");
for (unsigned int i = 0; i < payload_length; ++i)
{
printf("%02X ", payload[i]);
}
printf("\n");
}
// Callback for confirmation of message transmission
void WINAPI confirm_callback(enum MType m_type,
unsigned int can_id,
enum TAType ta_type,
unsigned char address_extension,
unsigned long long timestamp,
enum NResult result,
unsigned int request_handle)
{
printf("Confirm callback triggered, result: %d, timestamp: %llu\n", result, timestamp);
}
// Prints out the help message for the command line options
void print_help()
{
printf("Usage: obd_reader [options]\n"
"Options:\n"
" --channel <channel>, -c <channel> Set CAN channel (default: 0)\n"
" --bit-rate <bit_rate>, -b <bit_rate> Set CAN bit rate (default: 500000)\n"
" --data-phase-bit-rate <data_phase_bit_rate>, -d <data_phase_bit_rate> Set data phase bit rate (default: 0)\n"
" --local-id <local_id>, -l <local_id> Set local address ID (default: 0x7E8)\n"
" --remote-id <remote_id>, -r <remote_id> Set remote address ID (default: 0x7E0)\n"
" --use-padding, -P Use padding in messages\n"
" --use-bit-rate-switch, -s Use bit rate switching\n"
" --mode <mode>, -m <mode> Set OBD-II mode (default: 0x09)\n"
" --pid <pid>, -p <pid> Set PID (default: 0x02)\n"
" --help, -h Show this help message\n");
}
// Main function demonstrating the usage of the Kvaser Iso-TP library
// This function initializes the library, creates an ISO-TP channel, sets addresses, registers callbacks,
// opens the channel, sends a message, and cleans up resources.
int main(int argc, char *argv[])
{
unsigned int channel = 0;
unsigned int bit_rate = 500000;
unsigned int data_phase_bit_rate = 0;
AddressInfo local_address = {(unsigned int)0x7E8, false, 0, false, false};
AddressInfo remote_address = {(unsigned int)0x7E0, false, 0, false, false};
bool use_padding = false;
bool use_bit_rate_switch = false;
unsigned char mode = 0x09;
unsigned char pid = 0x02;
// Parse command line arguments
for (int i = 1; i < argc; ++i)
{
if ((strcmp(argv[i], "--channel") == 0 || strcmp(argv[i], "-c") == 0) && i + 1 < argc)
{
channel = (unsigned int)strtoul(argv[++i], NULL, 0);
}
else if ((strcmp(argv[i], "--bit-rate") == 0 || strcmp(argv[i], "-b") == 0) && i + 1 < argc)
{
bit_rate = (unsigned int)strtoul(argv[++i], NULL, 0);
}
else if ((strcmp(argv[i], "--data-phase-bit-rate") == 0 || strcmp(argv[i], "-d") == 0) && i + 1 < argc)
{
data_phase_bit_rate = (unsigned int)strtoul(argv[++i], NULL, 0);
}
else if ((strcmp(argv[i], "--local-id") == 0 || strcmp(argv[i], "-l") == 0) && i + 1 < argc)
{
local_address.id = (unsigned int)strtoul(argv[++i], NULL, 0);
}
else if ((strcmp(argv[i], "--remote-id") == 0 || strcmp(argv[i], "-r") == 0) && i + 1 < argc)
{
remote_address.id = (unsigned int)strtoul(argv[++i], NULL, 0);
}
else if ((strcmp(argv[i], "--use-padding") == 0 || strcmp(argv[i], "-P") == 0))
{
use_padding = true;
local_address.use_padding = true;
remote_address.use_padding = true;
}
else if ((strcmp(argv[i], "--use-bit-rate-switch") == 0 || strcmp(argv[i], "-s") == 0))
{
use_bit_rate_switch = true;
}
else if ((strcmp(argv[i], "--mode") == 0 || strcmp(argv[i], "-m") == 0) && i + 1 < argc)
{
mode = (unsigned char)strtoul(argv[++i], NULL, 0);
}
else if ((strcmp(argv[i], "--pid") == 0 || strcmp(argv[i], "-p") == 0) && i + 1 < argc)
{
pid = (unsigned char)strtoul(argv[++i], NULL, 0);
}
else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
{
print_help();
return 0;
}
}
// Load the Kvaser Iso-TP library
IsoTpApi api = load_library(LIBRARY_PATH);
if (!api.is_valid)
{
fprintf(stderr, "Failed to load Kvaser Iso-TP library!\n");
return 1;
}
// Create an ISO-TP channel
// Use Can for classic CAN (data_phase_bit_rate == 0), CanFd for CAN FD (data_phase_bit_rate != 0)
CanMode can_mode = data_phase_bit_rate == 0 ? Can : CanFd;
CreateParams create_params = {
channel,
can_mode,
true, // support_virtual
bit_rate,
data_phase_bit_rate,
local_address,
remote_address};
int isotp_handle = api.isotp_create(&create_params);
if (isotp_handle < 0)
{
fprintf(stderr, "Failed to create ISO-TP channel!\n");
return 1;
}
// Register callbacks
if (!api.isotp_register_callbacks(isotp_handle, ff_indication_callback, indication_callback, confirm_callback))
{
fprintf(stderr, "Failed to register callbacks!\n");
api.isotp_destroy(isotp_handle);
return 1;
}
// Open the ISO-TP channel
if (api.isotp_open(isotp_handle) < 0)
{
fprintf(stderr, "Failed to open ISO-TP channel!\n");
api.isotp_destroy(isotp_handle);
return 1;
}
// The data array holds the OBD-II request: [mode, pid, param1, param2, ...]
// Add more parameters to the array as needed for your request.
unsigned char data[] = {mode, pid}; // Add more parameters as needed
unsigned int data_length = sizeof(data); // Automatically set the correct length
// Send an ISO-TP message
WriteParams write_params = {
Diagnostics, // Message type
remote_address.id, // CAN ID
0, // Address extension
data, // Data payload
data_length, // Data length
use_padding, // Use padding (set to true to enable padding in the message)
use_bit_rate_switch, // Use bit rate switch
1 // Request handle (used to identify the request in confirm_callback)
};
if (!api.isotp_write(isotp_handle, &write_params))
{
fprintf(stderr, "Failed to send ISO-TP message!\n");
}
else
{
printf("ISO-TP message sent successfully!\n");
}
// Wait for callbacks to be triggered
// For production, replace getchar() with a proper event loop or signal handling.
printf("Press Enter to exit...\n");
getchar();
// Close the ISO-TP channel
if (api.isotp_close(isotp_handle) < 0)
{
fprintf(stderr, "Failed to close ISO-TP channel!\n");
}
else
{
printf("ISO-TP channel closed successfully!\n");
}
// Destroy the ISO-TP channel
api.isotp_destroy(isotp_handle);
// Unload the Kvaser Iso-TP library
return 0;
}
Kvaser ISO-TP API.
NResult
The result of the reception or transmission of a message.
Definition kvisotplib.h:65
IsoTpApi load_library(const char *lib_path)
Load the Kvaser ISO-TP library.
Definition kvisotplib.h:737
TAType
The type of target addressing used for the message.
Definition kvisotplib.h:92
@ PhysicalCanStandard
Physical addressing over classical CAN with standard (11-bit) identifier.
Definition kvisotplib.h:94
CanMode
Possible CAN modes.
Definition kvisotplib.h:40
@ Can
Classic CAN mode.
Definition kvisotplib.h:42
@ CanFd
CAN FD mode.
Definition kvisotplib.h:44
void unload_library(IsoTpApi *pApi)
Unload the Kvaser ISO-TP library and clear the API structure.
Definition kvisotplib.h:712
MType
The type of message to be sent.
Definition kvisotplib.h:53
@ Diagnostics
A message that is sent using normal addressing.
Definition kvisotplib.h:55
ISO-TP address information.
Definition kvisotplib.h:115
bool use_padding
True if message padding should be used.
Definition kvisotplib.h:125
unsigned int id
CAN identifier.
Definition kvisotplib.h:117
A struct that holds the parameters for creating an ISO-TP channel.
Definition kvisotplib.h:132
A struct with all function pointers for the ISO-TP API.
Definition kvisotplib.h:575
isotp_open_t isotp_open
Open the ISO-TP channel.
Definition kvisotplib.h:647
isotp_destroy_t isotp_destroy
Destroy an existing ISO-TP channel.
Definition kvisotplib.h:595
isotp_write_t isotp_write
Write a message to the ISO-TP channel.
Definition kvisotplib.h:655
bool is_valid
A flag indicating if the library was successfully loaded and if the API is available.
Definition kvisotplib.h:583
isotp_create_t isotp_create
Create a new ISO-TP channel.
Definition kvisotplib.h:591
isotp_close_t isotp_close
Close the ISO-TP channel.
Definition kvisotplib.h:651
isotp_register_callbacks_t isotp_register_callbacks
Register callback functions.
Definition kvisotplib.h:599
A struct that holds the parameters for an ISO-TP write request.
Definition kvisotplib.h:160