A C++ library for CAN bus packet handling, including support for higher level protocols CANopen and NMEA 2000.
- Unpack raw CAN frame byte sequences to
can::Packetstruct - Convert
can::Packetto raw CAN frame to be transmitted Bitsclass extendsstd::bitsetfor bit manipulation of CAN payload data- Convenient size-agnostic endianness conversion with
hton()andntoh()
- Unpack COB-ID to function code and node ID
- Unpack COB-ID to priority, source address, and PGN
- Convert CAN packet to PGN struct
- Easy to add PGN support with a simple yaml file (automatic code generation)
- C++20
- CMake 3.16 or later
Note: add flags to optionally build examples/tests, enable linting.
mkdir build
cd build
cmake .. -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON -DENABLE_LINTING=ON
makeAfter building with tests enabled:
# Run all tests via CTest (shows individual test cases)
make test
# Or run CTest directly with verbose output
ctest --verbose
# Run individual test executables
./test/test_can
# ... etc.Note: Google Test is automatically downloaded and built as part of the build process, ensuring consistent behavior across all platforms.
After building with examples enabled:
cd build
# CAN example
./example/can_example
# N2k example
./example/n2k_example
# CANopen example
./example/canopen_exampleThis project supports code linting and formatting using Uncrustify.
Install Uncrustify on your system:
# macOS
brew install uncrustify
# Ubuntu/Debian
sudo apt-get install uncrustifyOther systems: See the Uncrustify installation guide.
After building with linting enabled:
# Check code formatting (non-destructive)
make lint
# Apply formatting fixes automatically
make formatsudo make installInclude the library in your CMake project:
find_package(can-cpp REQUIRED)
target_link_libraries(your_target can-cpp::can-cpp)