Giter Site home page Giter Site logo

simoncahill / libsockcanpp Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 11.0 57 KB

A C++ wrapper around Linux's socketcan featureset.

Home Page: https://documentation.simonc.eu/docs/libsockcanpp/

License: Apache License 2.0

CMake 15.40% C++ 84.60%
cpp linux socketcan can wrapper

libsockcanpp's Introduction

libsockcanpp

Welcome to the documentation for libsockcanpp!
libsockcanpp is a socketcan wrapper library for C++, aimed to be easy to use without any fuss.

Build

Useful Links

Getting Started

Building

libsockcanpp was designed with use in CMake projects, but it can also easily be integrated into existing Makefile projects, as long as cmake is present on the build system.

  1. clone the repository: git clone https://github.com/SimonCahill/libsockcanpp.git
  2. create build directory: mkdir build && cd build
  3. generate build files: cmake .. -DCMAKE_TOOLCHAIN_FILE=../toolchains/desired-toolchain.cmake
  4. building library: make -j

Incorporating into Cmake projects:

  1. clone the repository: git clone https://github.com/SimonCahill/libsockcanpp.git
  2. add the following to CMakeLists.txt
if (NOT TARGET sockcanpp)
    add_subdirectory(/path/to/libsockcanpprepo ${CMAKE_CURRENT_BUILD_DIR}/libsockcanpp)
endif()

# ... 

target_link_libraries(
    # ...
    sockcanpp
)
  1. generate and build
  2. ??? profit

Using the CAN driver

libsockcanpp provides a simple interface to socketcan, which is represented by the CanDriver class.
CanDriver handles the setup of the socket; it does not however setup the CAN interface!

Instantiating CanDriver

The example below describes how to instantiate a new instance of CanDriver.
You can create as many instances as required, the resources are free'd once the instance has gone out of scope or been deleted.

The following parameters are required for correct instantiation:

  1. CAN interface: [v]can[0-?]
  2. CAN protocol: see linux/can.h for options
  3. (optional) CAN sender ID (arbitration ID)

The following exceptions may be thrown if something went wrong during initialisation:

  • @see CanInitException
    • if socketcan failed to initlialise
    • if ioctl failed
    • if socket binding failed

CanDriver is fully thread-safe and can be used in multi-threaded applications.

#include <CanDriver.hpp>

using sockcanpp::CanDriver;

int main() {
    CanDriver canDriver("can0", CAN_RAW[, 0xbad]);

    return 0;
}

Using CAN IDs

libsockcanpp provides a first-class datatype, @see CanId, which acts as an integer which can be either 11 or 29 bits in size.
The @see CanId type is used through libsockcanpp to provide a semi fool-proof method of using CAN arbitration IDs without the pitfalls of using traditional 32-bit integers.

CanId supports the following operations:

  • bitwise AND/OR
  • casting to: [u]int16, [u]int32
  • basic comparison:
    • equal to
    • not equal to
    • greater than (or equal to)
    • less than (or equal to)
  • arithmetic:
    • addition
    • subtraction

Sending CAN frames

Sending CAN frames with sockcanpp is as easy as you could imagine.

  1. instantiate a @see CanDriver object
  2. create a @see CanMessage
  3. send message
#include <CanDriver.hpp>

using sockcanpp::CanDriver;
using sockcanpp::CanId;
using sockcanpp::CanMessage;

void sendCanFrameExample() {
    CanDriver canDriver("can0", CAN_RAW[, 0xd00d]);

    CanMessage messageToSend(0 /*send with default ID*/, "8 bytes!" /* the data */);

    auto sentByteCount = canDriver.sendMessage(messageToSend[, false /* don't force extended ID */]);

    printf("Sent %d bytes via CAN!\n", sentByteCount);
}

void sendMultipleFramesExample() {
    CanDriver canDriver("can1", CAN_RAW[, 0 /* no default ID */]);

    queue<CanMessage> messageQueue = {
        CanMessage(0x269, "somedata"),
        Canmessage(0x1e9, "moredata")
    };

    auto sentByteCount = canDriver.sendMessageQueue(messageQueue[, milliseconds(20) /* delay between frames */[, false /* don't force extended ID */]]);

    printf("Sent %d bytes via CAN!\n", sentByteCount);

}

Receiving messages via CAN

Receiving CAN messages is almost as simple as sending them! Firstly, check if there are any messages in the buffer, then pull them out; either one-by-one, or all at once!

#include <CanDriver.hpp>

using sockcanpp::CanDriver;
using sockcanpp::CanId;
using sockcanpp::CanMessage;

void receiveCanFramesExample() {
    CanDriver canDriver("can2", CAN_RAW[, 0 /* no default ID */]);

    if (canDriver.waitForMessages([milliseconds(3000) /* timeout */])) {
        // read a single message
        CanMessage receivedMessage = canDriver.readMessage();

        // read all available messages
        queue<CanMessage> receivedMessages = canDriver.readQueuedMessages();

        // handle CAN frames
    }
}

libsockcanpp's People

Contributors

ernibrown avatar ezarkei avatar johnarild-hyrex avatar jorisoffouga avatar simoncahill avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

libsockcanpp's Issues

Queue size in read

_queueSize = select(_socketFd + 1, &readFileDescriptors, 0, 0, &waitTime);

There's an error here. Select does not return the size of the queue, only the number of file descriptors that are ready for reading/writing. So if you want to read all the messages you need to repeat the readMessage until it returns 0 bytes read. Or maybe you can do a ioctl call to FIONREAD, but I have not tested this

https://man7.org/linux/man-pages/man2/select.2.html

CanMessage issues

I'm having two separate issues with CanMessages. First, when reading in a message and calling the getCanId function, the CanId is off but barely. For example reading in a CanMessage with id = 0x18022080, the returned CanId from getCanId is 0x98022080. Similarly when sending a CanMessage, the data being sent is off as well. Example: The data to be sent should be 60 00 12 04 00 00 00 in hex, but what is being send is 70 17 B0 04 00 FF 00. Any ideas on what could be going on?

Is the message seperated or not

It may be wrong question, but is there a way to detect can message seperated into multiple can messages or single frame?
I read CF FF, but i couldnt see any implementation in libsockcanpp.

Thank you.

CanId

I'm using the CanId class in my project to manage CAN message identifiers. However, I've noticed that when I pass an extended frame ID, such as 0x70000000, to the constructor, the class incorrectly identifies it as a standard frame ID.

After some investigation, I believe the issue is that the class is not properly masking the ID to only include the 11 bits that represent the CAN identifier. I tried updating the constructor to mask the ID using the bitwise AND operator like this:

CanId(const uint32_t identifier): _identifier(identifier & 0x7FF)

However, this did not solve the issue. I also noticed that the CanId class already has bitwise operators defined, such as '&' and '|', but I'm not sure how to use them to correctly identify extended frame IDs.

Could someone please help me identify the root cause of this issue and suggest a solution?

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.