https://github.com/haradama/ofxsocketcan
openFrameworks addon for SocketCAN
https://github.com/haradama/ofxsocketcan
automotive openframeworks openframeworks-addon socketcan vehicle
Last synced: 8 months ago
JSON representation
openFrameworks addon for SocketCAN
- Host: GitHub
- URL: https://github.com/haradama/ofxsocketcan
- Owner: haradama
- License: mit
- Created: 2023-04-10T14:07:48.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-29T12:40:02.000Z (almost 3 years ago)
- Last Synced: 2025-03-05T20:52:21.241Z (11 months ago)
- Topics: automotive, openframeworks, openframeworks-addon, socketcan, vehicle
- Language: C++
- Homepage:
- Size: 55.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ofxSocketCAN
A simple wrapper of SocketCAN for [openFrameworks](https://openframeworks.cc).
## Usage
| Method | Description |
| ------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `bool setup(const std::string& interface, bool use_can_fd = false)` | Initializes the SocketCAN connection with the specified interface and optional CAN FD support. Returns `true` on success, `false` otherwise. |
| `bool send(const can_frame& frame)` | Sends a standard CAN frame. Returns `true` on success, `false` otherwise. Use this method when CAN FD is not enabled. |
| `bool send(const canfd_frame& frame)` | Sends a CAN FD frame. Returns `true` on success, `false` otherwise. Use this method when CAN FD is enabled. |
| `bool receive(can_frame& frame)` | Receives a standard CAN frame. Returns `true` on success, `false` otherwise. Use this method when CAN FD is not enabled. |
| `bool receive(canfd_frame& frame)` | Receives a CAN FD frame. Returns `true` on success, `false` otherwise. Use this method when CAN FD is enabled. |
| `bool available()` | Rutuns `true` if the device is available, `false` otherwise. |
## Example
```cpp
#pragma once
#include "ofMain.h"
#include "ofxSocketCAN.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxSocketCAN device;
std::string interfaceName;
can_frame receiveFrame;
};
```
```cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetWindowTitle("ofxSocketCAN Example");
interfaceName = "vcan0";
device.setup(interfaceName);
if (device.available()) {
ofLogNotice("ofApp::setup") << "Connected to " << interfaceName;
can_frame sendFrame;
sendFrame.can_id = 0x123;
sendFrame.can_dlc = 3;
sendFrame.data[0] = 0x11;
sendFrame.data[1] = 0x22;
sendFrame.data[2] = 0x33;
if (device.send(sendFrame)) {
ofLogNotice("ofApp::setup") << "Sent CAN frame: ID 0x" << std::hex << sendFrame.can_id;
} else {
ofLogError("ofApp::setup") << "Failed to send CAN frame.";
}
} else {
ofLogError("ofApp::setup") << "Failed to connect to " << interfaceName;
}
}
//--------------------------------------------------------------
void ofApp::update(){
if (device.available()) {
if (device.receive(receiveFrame)) {
ofLogNotice("ofApp::update") << "Received CAN frame: ID 0x" << std::hex << receiveFrame.can_id;
}
}
}
```