source: pacpussensors/trunk/CanGateway/driver/KVaserCanDriver.cpp@ 89

Last change on this file since 89 was 89, checked in by DHERBOMEZ Gérald, 9 years ago

Add KVASER CAN driver support for CanGateway component. Tested only for Windows.

File size: 6.8 KB
Line 
1/// @date created 2015/06/23
2/// @author Gerald Dherbomez
3/// @version $Id$
4
5#include "KVaserCanDriver.h"
6#include "kvaser/canlib.h"
7
8#include <cassert>
9#include <cstdio>
10#include <iomanip>
11#include <iostream>
12#include <string>
13
14#include "Pacpus/kernel/Log.h"
15#include "../CanFrame.h"
16
17using namespace pacpus;
18using namespace std;
19
20DECLARE_STATIC_LOGGER("pacpussensors.CanGateway.KVaserCanDriver");
21
22#define WAIT_RECEIVING_FRAME_TIMEOUT 1000
23
24/*
25#define RX_QUEUE_SIZE 4096
26#define QUEUE_LEVEL 1
27
28bool checkXlStatus(const XLstatus status) {
29 if ( status != XL_SUCCESS ) {
30 XLstringType error = xlGetErrorString(status);
31 LOG_ERROR(error);
32 return false;
33 } else {
34 return true;
35 }
36
37}
38*/
39//
40// Check a status code and issue an error message if the code isn't canOK.
41//
42void ErrorExit(char* id, canStatus stat)
43{
44 char buf[50];
45 if (stat != canOK) {
46 buf[0] = '\0';
47 canGetErrorText(stat, buf, sizeof(buf));
48 LOG_ERROR("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
49 //exit(1);
50 }
51}
52
53/**
54* Constructor which enables to initialize the different attributes of the class with default values.
55* @see ~KVaserCanDriver (void)
56*/
57
58KVaserCanDriver::KVaserCanDriver(int channel, unsigned int bitRate)
59{
60 LOG_INFO("Notice : KVASER CAN Driver used\n");
61 canChannel_ = channel;
62 canBitRate_ = bitRate;
63 kvaserHardwareType_ = 0;
64}
65
66
67/**
68* Constructor which enables to initialize the different attributes of the class with default values.
69* @see ~KVaserCanDriver (void)
70* @see KVaserCanDriver (unsigned int gHwTypeT, Vaccess gChannelMaskT, unsigned int gCanIdT, unsigned int gBitRateT,int gHwChannelT)
71*/
72
73KVaserCanDriver::KVaserCanDriver (void)
74{
75 LOG_INFO("Notice : KVASER CAN Driver used\n");
76 canChannel_ = 0;
77 canBitRate_ = 500000;
78 kvaserHardwareType_ = 0;
79}
80
81/**
82* Constructor which enables to initialize the different attributes of the class with values given in parameters.
83* @see ~KVaserCanDriver (void)
84* @see KVaserCanDriver (void)
85*/
86KVaserCanDriver::KVaserCanDriver(const int hwType, const int hwChannel, const unsigned long bitrate)
87{
88 canChannel_ = hwChannel;
89 canBitRate_ = bitrate;
90 kvaserHardwareType_ = hwType;
91}
92
93
94/**
95* Constructor which enables to select the channel and to initialize the different attributes of the class with default values..
96* @see ~KVaserCanDriver (void)
97* @see KVaserCanDriver (void)
98*/
99
100KVaserCanDriver::KVaserCanDriver(int channel)
101{
102 LOG_INFO("Notice : KVASER CAN Driver used\n");
103 canChannel_ = channel;
104 canBitRate_ = 500000;
105 kvaserHardwareType_ = 0;
106}
107
108
109
110/**
111* Destructor which clean up the different attributs of the class.
112* @see KVaserCanDriver (void)
113*/
114KVaserCanDriver::~KVaserCanDriver (void)
115{
116
117}
118
119
120
121void KVaserCanDriver::displayHardware()
122{
123 LOG_INFO("----------------------------------------------------------");
124 LOG_INFO("KVASER DRIVER HARDWARE INFORMATION -- TODO");
125 LOG_INFO("KVASER DRIVER VERSION: 5_11_931 ");
126 LOG_INFO("----------------------------------------------------------");
127}
128
129
130void KVaserCanDriver::initialize(const int hwType, const int hwChannel, const unsigned long bitrate)
131{
132 LOG_INFO("Notice : KVASER CAN Driver used\n");
133 displayHardware();
134
135 canHandle_ = canOpenChannel(hwChannel, 0);
136 if (canHandle_ < 0) {
137 ErrorExit("canOpenChannel", (canStatus)canHandle_);
138 }
139 else
140 {
141 int ret = 0;
142 switch (bitrate)
143 {
144 case 125000:
145 ret = canSetBusParams(canHandle_, canBITRATE_125K, 0, 0, 0, 0, 0);
146 break;
147 case 250000:
148 ret = canSetBusParams(canHandle_, canBITRATE_250K, 0, 0, 0, 0, 0);
149 break;
150 case 500000:
151 ret = canSetBusParams(canHandle_, canBITRATE_500K, 0, 0, 0, 0, 0);
152 break;
153 default:
154 ret = canERR_NOTFOUND;
155 break;
156 }
157 if (ret < 0) {
158 ErrorExit("canSetBusParams", (canStatus)ret);
159 }
160
161 ret = canBusOn(canHandle_);
162 if (ret < 0) {
163 ErrorExit("canBusOn", (canStatus)ret);
164
165 }
166 }
167}
168
169
170/**
171* Member used to initialise the configuration of the CAN Card.
172* @see cleanUpPort (void)
173* @return a Vstatus variable which contain the error code of the function. On success, it return VSUCCESS. On failure, it return Vstatus error code which is defined in VCanD.h
174*/
175short KVaserCanDriver::initPort (void)
176{
177 //
178 // Initialize CANLIB.
179 //
180 canInitializeLibrary();
181
182 // open the physical CAN interface
183 initialize(kvaserHardwareType_, canChannel_, canBitRate_);
184
185 return 0;
186}
187
188
189/**
190* Member used to clean up the configuration of the CAN Card.
191* @see initPort (void)
192* @return a Vstatus variable which contain the error code of the function. On success, it return VSUCCESS. On failure, it return Vstatus error code which is defined in VCanD.h
193*/
194short KVaserCanDriver::cleanUpPort (void)
195{
196
197 //traceXLCommand("xlCloseDriver", xlCloseDriver());
198
199 canBusOff(canHandle_);
200 canClose(canHandle_);
201
202 return 0;
203}
204
205
206/**
207* Member which permit to send a frame on the CAN bus and test if the frame is well acknowledged.
208*/
209short KVaserCanDriver::sendFrame (struct CanFrame frame)
210{
211 canStatus stat = canWrite(canHandle_, frame.id,frame.data, frame.dlc, 0);
212 if (stat == canOK )
213 {
214 return 0;
215 } else {
216 LOG_WARN("Kvaser driver - sendFrame method - Failed to send the CAN frame");
217 return 1;
218 }
219}
220
221
222/**
223* Member which permit to receive of a frame on the CAN bus.
224*/
225//Vstatus KVaserCanDriver::receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char * data, int * identifiant)
226short KVaserCanDriver::receiveFrame (struct CanFrame &frame)
227{
228 long id;
229 unsigned int dlc, flags;
230 unsigned char msg[8];
231 DWORD time;
232
233 canStatus stat = canReadWait(canHandle_, &id, msg, &dlc, &flags, &time, WAIT_RECEIVING_FRAME_TIMEOUT);
234 if (stat == canOK )
235 {
236 if ((flags & canMSG_ERROR_FRAME) == 0)
237 {
238 // Get the message data
239 /*fprintf(f, "%8lu%c%c%c%c %02lu ",
240 id,
241 flags & canMSG_EXT ? 'x' : ' ',
242 flags & canMSG_RTR ? 'R' : ' ',
243 flags & canMSGERR_OVERRUN ? 'o' : ' ',
244 flags & canMSG_NERR ? 'N' : ' ', // TJA 1053/1054 transceivers only
245 dlc);*/
246 frame.id = id;
247 frame.dlc = dlc;
248 memcpy(frame.data, msg, frame.dlc);
249 return 0;
250 } else if (stat == canERR_NOMSG ) {
251 // timeout occurs
252 LOG_WARN("Kvaser card - receiveFrame() method - TIMEOUT");
253 return 1;
254 } else {
255 // An error frame.
256 LOG_WARN("Kvaser card, error frame");
257 return 1;
258 }
259 }
260
261 return 0;
262}
263
264
265/**
266* Member which wait the reception of a frame on the CAN bus.
267* @see sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data)
268* @see receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char ** data)
269*/
270void KVaserCanDriver::waitReceivingFrame(void)
271{
272 LOG_WARN("KVASER driver - waitReceivingFrame(void) method - Not yet implemented");
273}
Note: See TracBrowser for help on using the repository browser.