Kvaser ISO-TP SDK
Loading...
Searching...
No Matches
/examples/cpp_obd_reader/obd_reader.cpp
/*
* 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 <iostream>
#include <string>
#include <iomanip>
// Change the include path as needed
#ifdef _WIN32
#include "../../include/kvisotplib_wrapper.h"
#else
#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
// Example callback function definitions
// 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)
{
std::cout << "First Frame indication callback triggered, length: " << length << ", timestamp: " << timestamp << std::endl;
}
// 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)
{
std::cout << "Indication callback triggered, result: " << result << ", timestamp: " << timestamp << std::endl;
std::cout << "Payload: ";
for (unsigned int i = 0; i < payload_length; ++i)
{
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(payload[i]) << " ";
}
std::cout << std::dec << std::endl;
}
// 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)
{
std::cout << "Confirm callback triggered, result: " << result << ", timestamp: " << timestamp << std::endl;
}
void print_help()
{
std::cout << "Usage: obd_reader [options]" << std::endl
<< "Options:" << std::endl
<< " --channel <channel>, -c <channel> Set CAN channel (default: 0)" << std::endl
<< " --bit-rate <bit_rate>, -b <bit_rate> Set CAN bit rate (default: 500000)" << std::endl
<< " --data-phase-bit-rate <data_phase_bit_rate>, -d <data_phase_bit_rate> Set data phase bit rate (default: 0)" << std::endl
<< " --local-id <local_id>, -l <local_id> Set local address ID (default: 0x7E8)" << std::endl
<< " --remote-id <remote_id>, -r <remote_id> Set remote address ID (default: 0x7E0)" << std::endl
<< " --use-padding, -P Use padding in messages" << std::endl
<< " --use-bit-rate-switch, -s Use bit rate switching" << std::endl
<< " --mode <mode>, -m <mode> Set OBD-II mode (default: 0x09)" << std::endl
<< " --pid <pid>, -p <pid> Set PID (default: 0x02)" << std::endl
<< " --help, -h Show this help message" << std::endl;
}
// Main function demonstrating the usage of the Kvaser Iso-TP library wrapper
// This function initializes the library, creates an ISO-TP channel, sets addresses, registers callbacks,
// opens the channel, sends a message, and cleans up resources.
// It serves as an example of how to use the IsoTpWrapper class to interact with the Kvaser Iso-TP library.
int main(int argc, char *argv[])
{
try
{
unsigned int channel = 0; // Default channel
unsigned int bit_rate = 500000; // Default bit rate
unsigned int data_phase_bit_rate = 0; // Default data phase bit rate
AddressInfo local_address = {(unsigned int)0x7E8, false, 0, false, false}; // Default local address
AddressInfo remote_address = {(unsigned int)0x7E0, false, 0, false, false}; // Default remote address
bool use_padding = false; // Default padding setting
bool use_bit_rate_switch = false; // Default bit rate switch setting
unsigned char mode = 0x09; // Default OBD-II mode (service) 0x09 request vehicle information
unsigned char pid = 0x02; // Default PID (Parameter ID) 0x02 (VIN)
// Parse command line arguments for optional parameters
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if ((arg == "--channel" || arg == "-c") && i + 1 < argc)
{
channel = std::stoul(argv[++i]);
}
else if ((arg == "--bit-rate" || arg == "-b") && i + 1 < argc)
{
bit_rate = std::stoul(argv[++i]);
}
else if ((arg == "--data-phase-bit-rate" || arg == "-d") && i + 1 < argc)
{
data_phase_bit_rate = std::stoul(argv[++i]);
}
else if ((arg == "--local-id" || arg == "-l") && i + 1 < argc)
{
local_address.id = std::stoul(argv[++i], nullptr, 0);
}
else if ((arg == "--remote-id" || arg == "-r") && i + 1 < argc)
{
remote_address.id = std::stoul(argv[++i], nullptr, 0);
}
else if (arg == "--use-padding" || arg == "-P")
{
use_padding = true;
local_address.use_padding = true; // Set padding for local address
remote_address.use_padding = true; // Set padding for remote address
}
else if (arg == "--use-bit-rate-switch" || arg == "-s")
{
use_bit_rate_switch = true;
}
else if ((arg == "--mode" || arg == "-m") && i + 1 < argc)
{
mode = static_cast<unsigned char>(std::stoi(argv[++i], nullptr, 0));
}
else if ((arg == "--pid" || arg == "-p") && i + 1 < argc)
{
pid = static_cast<unsigned char>(std::stoi(argv[++i], nullptr, 0));
}
else if (arg == "--help" || arg == "-h")
{
print_help();
return 0;
}
}
// Example usage of the Kvaser Iso-TP library wrapper
// Load the Kvaser Iso-TP library
IsoTpWrapper isotplib;
if (!isotplib.load_library(LIBRARY_PATH))
{
std::cerr << "Failed to load Kvaser Iso-TP library!" << std::endl;
return 1;
}
// Create an ISO-TP channel
CanMode can_mode = data_phase_bit_rate == 0 ? CanMode::Can : CanMode::CanFd;
CreateParams create_params = {
channel,
can_mode,
true, // support_virtual
bit_rate,
data_phase_bit_rate,
local_address,
remote_address};
int isotp_handle = isotplib.create(&create_params);
if (isotp_handle < 0)
{
std::cerr << "Failed to create ISO-TP channel!" << std::endl;
return 1;
}
// Register callbacks
isotplib.register_callbacks(isotp_handle, ff_indication_callback, indication_callback, confirm_callback);
// Open the ISO-TP channel
if (isotplib.open(isotp_handle) < 0)
{
std::cerr << "Failed to open ISO-TP channel!" << std::endl;
isotplib.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 = {
MType::Diagnostics, // Message type
remote_address.id, // CAN ID
TAType::PhysicalCanStandard, // TAType
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 (!isotplib.write(isotp_handle, &write_params))
{
std::cerr << "Failed to send ISO-TP message!" << std::endl;
}
else
{
std::cout << "ISO-TP message sent successfully!" << std::endl;
}
// Wait for callbacks to be triggered
// For production, std::cin.get() with a proper event loop or signal handling.
std::cout << "Press Enter to exit..." << std::endl;
std::cin.get();
// Close the ISO-TP channel
if (isotplib.close(isotp_handle) < 0)
{
std::cerr << "Failed to close ISO-TP channel!" << std::endl;
}
else
{
std::cout << "ISO-TP channel closed successfully!" << std::endl;
}
// Destroy the ISO-TP channel
isotplib.destroy(isotp_handle);
// Unload the Kvaser Iso-TP library
isotplib.unload_library();
return 0;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl
<< std::endl;
print_help();
return 1;
}
}
Wrapper class for loading and using the Kvaser Iso-TP library.
Definition kvisotplib_wrapper.h:34
int close(int isotp_handle)
Closes this channel and takes its CAN channel off-bus.
Definition kvisotplib_wrapper.h:688
int open(int isotp_handle)
Opens the specified ISO-TP channel and takes its CAN channel bus-on.
Definition kvisotplib_wrapper.h:651
bool register_callbacks(int isotp_handle, FFIndicationCallback ff_indication_callback, IndicationCallback indication_callback, ConfirmCallback confirm_callback)
Registers callback functions to be used by the ISO-TP channel to inform the upper layers on events.
Definition kvisotplib_wrapper.h:192
void unload_library()
Unload the Kvaser ISO-TP library.
Definition kvisotplib_wrapper.h:77
int create(const struct CreateParams *create_params)
Creates and opens a new ISO-TP channel.
Definition kvisotplib_wrapper.h:125
void destroy(int isotp_handle)
Closes and removes the ISO-TP channel with the specified handle.
Definition kvisotplib_wrapper.h:156
bool load_library(const char *lib_path)
Load the Kvaser ISO-TP library.
Definition kvisotplib_wrapper.h:68
bool write(int isotp_handle, const struct WriteParams *write_params)
Sends a message.
Definition kvisotplib_wrapper.h:742
NResult
The result of the reception or transmission of a message.
Definition kvisotplib.h:65
TAType
The type of target addressing used for the message.
Definition kvisotplib.h:92
CanMode
Possible CAN modes.
Definition kvisotplib.h:40
MType
The type of message to be sent.
Definition kvisotplib.h:53
Kvaser ISO-TP C++ Wrapper.
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 that holds the parameters for an ISO-TP write request.
Definition kvisotplib.h:160