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

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

kvaser CAN driver testing on Linux. Works fine.
TODO: test back the code on Windows.

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