Skip to content

taerugh/can-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

can-cpp

A C++ library for CAN bus packet handling, including support for higher level protocols CANopen and NMEA 2000.

Features

CAN

  • Unpack raw CAN frame byte sequences to can::Packet struct
  • Convert can::Packet to raw CAN frame to be transmitted
  • Bits class extends std::bitset for bit manipulation of CAN payload data
  • Convenient size-agnostic endianness conversion with hton() and ntoh()

CANopen

  • Unpack COB-ID to function code and node ID

NMEA 2000 (n2k)

  • 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)

Dependencies

  • C++20
  • CMake 3.16 or later

Building

Build the library

Note: add flags to optionally build examples/tests, enable linting.

mkdir build
cd build
cmake .. -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON -DENABLE_LINTING=ON
make

Running tests

After 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.

Running examples

After building with examples enabled:

cd build

# CAN example
./example/can_example

# N2k example
./example/n2k_example

# CANopen example
./example/canopen_example

Code Linting and Formatting

This project supports code linting and formatting using Uncrustify.

Installation

Install Uncrustify on your system:

# macOS
brew install uncrustify

# Ubuntu/Debian
sudo apt-get install uncrustify

Other systems: See the Uncrustify installation guide.

Usage

After building with linting enabled:

# Check code formatting (non-destructive)
make lint

# Apply formatting fixes automatically
make format

Installation

sudo make install

Usage

Include the library in your CMake project:

find_package(can-cpp REQUIRED)
target_link_libraries(your_target can-cpp::can-cpp)

About

A C++ library for CAN bus packet handling, including support for higher level protocols CANopen and NMEA 2000.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages