Skip to content

Commit 7279650

Browse files
committed
first commit
1 parent 1502640 commit 7279650

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+16345
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
.vs
3+
build
4+
*.txt
5+
!CMakeLists.txt

.vscode/c_cpp_properties.json

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"/usr/include",
7+
"/usr/local/include",
8+
"${workspaceFolder}",
9+
"${workspaceFolder}/include",
10+
"${workspaceFolder}/include/unix"
11+
],
12+
"defines": [],
13+
"intelliSenseMode": "clang-x64",
14+
"browse": {
15+
"path": [
16+
"/usr/include",
17+
"/usr/local/include",
18+
"${workspaceFolder}",
19+
"${workspaceFolder}/include",
20+
"${workspaceFolder}/include/unix"
21+
],
22+
"limitSymbolsToIncludedHeaders": true,
23+
"databaseFilename": ""
24+
},
25+
"macFrameworkPath": [
26+
"/System/Library/Frameworks",
27+
"/Library/Frameworks"
28+
],
29+
"cStandard": "c11",
30+
"cppStandard": "c++17"
31+
},
32+
{
33+
"name": "Linux",
34+
"includePath": [
35+
"${workspaceFolder}",
36+
"/usr/include/c++/7",
37+
"/usr/include/x86_64-linux-gnu/c++/7",
38+
"/usr/include/c++/7/backward",
39+
"/usr/lib/llvm-6.0/lib/clang/6.0.0/include",
40+
"/usr/local/include",
41+
"/usr/include/x86_64-linux-gnu",
42+
"/usr/include",
43+
"${workspaceFolder}/include",
44+
"${workspaceFolder}/include/unix"
45+
],
46+
"defines": [],
47+
"intelliSenseMode": "clang-x64",
48+
"browse": {
49+
"path": [
50+
"${workspaceFolder}",
51+
"/usr/include/c++/7",
52+
"/usr/include/x86_64-linux-gnu/c++/7",
53+
"/usr/include/c++/7/backward",
54+
"/usr/lib/llvm-6.0/lib/clang/6.0.0/include",
55+
"/usr/local/include",
56+
"/usr/include/x86_64-linux-gnu",
57+
"/usr/include",
58+
"${workspaceFolder}/include",
59+
"${workspaceFolder}/include/unix"
60+
],
61+
"limitSymbolsToIncludedHeaders": true,
62+
"databaseFilename": ""
63+
},
64+
"compilerPath": "/usr/bin/clang",
65+
"cStandard": "c11",
66+
"cppStandard": "c++11"
67+
},
68+
{
69+
"name": "Win32",
70+
"browse": {
71+
"path": [
72+
"${workspaceFolder}",
73+
"${workspaceFolder}/include",
74+
"${workspaceFolder}/include/win32"
75+
],
76+
"limitSymbolsToIncludedHeaders": true
77+
},
78+
"includePath": [
79+
"${workspaceFolder}",
80+
"${workspaceFolder}/include",
81+
"${workspaceFolder}/include/win32"
82+
],
83+
"defines": [
84+
"_DEBUG",
85+
"UNICODE",
86+
"_UNICODE"
87+
],
88+
"cStandard": "c11",
89+
"intelliSenseMode": "msvc-x64",
90+
"cppStandard": "c++14"
91+
}
92+
],
93+
"version": 4
94+
}

.vscode/launch.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Debug",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/build/${workspaceFolderBasename}",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"preLaunchTask": "build debug",
16+
"environment": [],
17+
"externalConsole": true,
18+
"osx": {
19+
"miDebuggerPath": "/usr/local/Cellar/llvm/6.0.1/bin/lldb-mi",
20+
"MIMode": "lldb"
21+
},
22+
"linux": {
23+
"MIMode": "gdb",
24+
"setupCommands": [
25+
{
26+
"description": "Enable pretty-printing for gdb",
27+
"text": "-enable-pretty-printing",
28+
"ignoreFailures": true
29+
}
30+
]
31+
},
32+
"windows": {
33+
"type": "cppvsdbg",
34+
"program": "${workspaceFolder}/build/${workspaceFolderBasename}.exe",
35+
}
36+
}
37+
]
38+
}

.vscode/settings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"files.associations": {
3+
"__locale": "cpp",
4+
"__string": "cpp",
5+
"string": "cpp",
6+
"string_view": "cpp",
7+
"thread": "cpp",
8+
"functional": "cpp"
9+
}
10+
}

.vscode/tasks.json

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "build debug",
8+
"type": "shell",
9+
"linux": {
10+
"command": "mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug ../ && make",
11+
},
12+
"osx": {
13+
"command": "mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug ../ && make",
14+
},
15+
"windows": {
16+
"command": "cd build && \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat\" amd64 && cmake -DCMAKE_BUILD_TYPE=Debug -G \"CodeBlocks - NMake Makefiles\" ../ && cmake --build ./ --target all --",
17+
},
18+
"args": [],
19+
"group": {
20+
"kind": "build",
21+
"isDefault": true
22+
},
23+
"presentation": {
24+
"reveal": "always"
25+
},
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "build release",
30+
"type": "shell",
31+
"linux": {
32+
"command": "mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release ../ && make",
33+
},
34+
"osx": {
35+
"command": "mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release ../ && make",
36+
},
37+
"windows": {
38+
"command": "cd build && \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat\" amd64 && cmake -DCMAKE_BUILD_TYPE=Release -G \"CodeBlocks - NMake Makefiles\" ../ && cmake --build ./ --target all --",
39+
},
40+
"args": [],
41+
"group": {
42+
"kind": "build",
43+
"isDefault": true
44+
},
45+
"presentation": {
46+
"reveal": "always"
47+
},
48+
"problemMatcher": "$msCompile"
49+
}
50+
]
51+
}

CANBase.h

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#pragma once
2+
#ifndef __CANBase_H
3+
#define __CANBase_H
4+
#include <functional>
5+
#include <string>
6+
#include "CANType.h"
7+
namespace ZCANBus {
8+
9+
class CANBase {
10+
public:
11+
CANBase(){};
12+
13+
virtual ~CANBase(){};
14+
15+
/**
16+
* @brief Open a specific channel
17+
* @param channel the specific number for the CAN device
18+
* @param baudRate the speed for the communication
19+
* @param type different CAN devices has differents meanings
20+
* @return a error code. Generally, 0 means OK. @see CanStatus
21+
*/
22+
virtual CANStatus OpenChannel(int channel, CANRate baudRate, int type) = 0;
23+
24+
/**
25+
* @brief Read CAN message continuously with async mode.
26+
* @param callback the function will be called while received new CAN
27+
* message the param msg is the received msg and status is the error code
28+
* which generally 0 means OK
29+
* @param interval the max interval in milliseconds between two message
30+
* received
31+
*/
32+
virtual void ReadLoop(
33+
std::function<void(const CANMessage* msg, CANStatus status)> callback,
34+
uint64_t interval) = 0;
35+
36+
/**@brief End read CAN message continuously with async mode.*/
37+
virtual void EndReadLoop() = 0;
38+
39+
/**
40+
* @brief Read CAN message once
41+
* @param msg Modified by received CAN message
42+
* @param timeout Read CAN message with timeout in milliseconds
43+
* @return a error code. Generally, 0 means OK. @see CanStatus
44+
*/
45+
virtual CANStatus ReadOnce(CANMessage& msg, uint64_t timeout = 0) = 0;
46+
47+
/**
48+
* @brief Write CAN message once
49+
* @param msg the CAN message to be wrote
50+
* @return a error code. Generally, 0 means OK. @see CanStatus
51+
*/
52+
virtual CANStatus Write(CANMessage* msg, int count) = 0;
53+
54+
/**
55+
* @brief Close the channel.
56+
* @return a error code. Generally, 0 means OK. @see CanStatus
57+
*/
58+
virtual CANStatus CloseChannel() = 0;
59+
60+
/**
61+
* @brief Flush the data in queue.
62+
* @return a error code. Generally, 0 means OK. @see CanStatus
63+
*/
64+
virtual CANStatus FlushQueue() = 0;
65+
66+
/**
67+
* @brief Get error message in detail.
68+
* @param status the error code. Modified by the new error code returned by
69+
* this operation
70+
* @return error message
71+
*/
72+
virtual std::string GetErrorText(CANStatus& status) = 0;
73+
};
74+
} // namespace ZCANBus
75+
#endif

CANHandler.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Created by ShizengChen on 2017/12/24.
3+
//
4+
5+
#include "CANHandler.h"
6+
#include "CANBase.h"
7+
#ifdef USE_PEAK
8+
#include "CANPeak.h"
9+
#endif
10+
#ifdef USE_KVASER
11+
#include "CANKvaser.h"
12+
#endif
13+
14+
namespace ZCANBus {
15+
CANStatus CANHandler::OpenChannel(int channel, CANRate baudRate, int type) {
16+
return baseCan->OpenChannel(channel, baudRate, type);
17+
}
18+
19+
void CANHandler::ReadLoop(
20+
std::function<void(const CANMessage *msg, CANStatus status)> callback,
21+
uint64_t interval) {
22+
baseCan->ReadLoop(callback, interval);
23+
}
24+
25+
void CANHandler::EndReadLoop() { baseCan->EndReadLoop(); }
26+
27+
CANStatus CANHandler::ReadOnce(CANMessage &msg, uint64_t timeout) {
28+
return baseCan->ReadOnce(msg);
29+
}
30+
31+
CANStatus CANHandler::Write(CANMessage *msg, int count) {
32+
return baseCan->Write(msg, count);
33+
}
34+
35+
CANStatus CANHandler::CloseChannel() { return baseCan->CloseChannel(); }
36+
37+
CANStatus CANHandler::FlushQueue() { return baseCan->FlushQueue(); }
38+
39+
std::string CANHandler::GetErrorText(CANStatus &status) {
40+
return baseCan->GetErrorText(status);
41+
}
42+
43+
CANHandler::CANHandler(CANType canType) {
44+
switch (canType) {
45+
#ifdef USE_PEAK
46+
case CANType::PEAK_CAN:
47+
baseCan = new CANPeak();
48+
break;
49+
#endif
50+
#ifdef USE_KVASER
51+
case CANType::KVASER_CAN:
52+
baseCan = new CANKvaser();
53+
break;
54+
#endif
55+
default:
56+
throw;
57+
}
58+
}
59+
60+
CANHandler::~CANHandler() { delete baseCan; }
61+
} // namespace ZCANBus

0 commit comments

Comments
 (0)