source: pacpussensors/trunk/CanGateway/Win32CanInterface.cpp@ 110

Last change on this file since 110 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.8 KB
RevLine 
[1]1/*********************************************************************
2// created: 2008/2/11 - 11:59
3// filename: Win32CanInterface.cpp
4//
5// author: Gerald Dherbomez
[89]6// Copyright Heudiasyc UMR UTC/CNRS 7253
[1]7//
8// version: $Id: $
9//
[89]10// purpose: Management of the Can Interface
[1]11//
12*********************************************************************/
13
14#include "Win32CanInterface.h"
15
[94]16// #include "Pacpus/PacpusTools/ShMem.h" // runtime crash because PosixShMem
[1]17
18namespace pacpus {
19
20using namespace std;
21
22/************************************************************************/
23/// Constructor
24Win32CanInterface::Win32CanInterface()
25{
26 continue_ = true;
27 counter_ = 0;
28 receivedFramesArraySize_ = 0;
[41]29 canDriver_ = NULL;
[1]30}
31
32/************************************************************************/
33/// Destructor
34Win32CanInterface::~Win32CanInterface()
35{
36}
37
38/************************************************************************/
39/// Opens the CAN interface of the number given in argument
40bool Win32CanInterface::openInterface(const int number, const unsigned int speed)
41{
42 canDriver_ = new CanDriver(number, speed);
43
44 //connection to CAN bus
45 if(canDriver_->initPort() != 0)
46 {
47 cout << "CAN connection fails" << endl;
48 return false;
49 }
50 else
51 return true;
52}
53/************************************************************************/
54/// Open the CAN interface of the number given in argument
55bool Win32CanInterface::openInterface(char * port, char * accessMode)
56{
57 canDriver_ = new CanDriver(port, accessMode);
58 //connection to CAN bus
59 if(canDriver_->initPort() != 0)
60 {
61 cout << "CAN connection fails" << endl;
62 return false;
63 }
64 else{
65 return true;
66 }
67}
68
69/************************************************************************/
70/// Close the CAN interface
71/// todo: close only the port identified by number
72/// make a function that close the driver or close the driver only if
73/// there is no more pending connections
74bool Win32CanInterface::closeInterface(const int /*number*/)
75{
76 if(canDriver_->cleanUpPort() != 0)
77 {
78 cout << "CAN disconnection fails" << endl;
79 delete canDriver_;
80 return false;
81 }
82 else
83 {
84 delete canDriver_;
85 return true;
86 }
87}
88
89/************************************************************************/
90/// The main loop of the class
91void Win32CanInterface::run()
92{
93 continue_ = true;
94 counter_ = 0;
95
96 if (!receivedFramesArraySize_)
97 qFatal("receivedFramesArraySize_ not initialized, division by zero may occur if you continue. Context:%s:L%d",__FILE__, __LINE__);
98
99 cout << "Win32CanInterface starts" << endl;
100
101 switch (source_)
102 {
103 case VectorCard:
104 vectorLoop();
105 break;
[94]106 // case SharedMemory: // runtime crash because PosixShMem
107 // shMemLoop();
108 // break;
[1]109 case PeakCard:
110 peakLoop();
111 break;
[4]112 case igepCard:
113 igepLoop();
114 break;
[1]115 case XLVectorCard:
116 vectorXlLoop();
[89]117 case KvaserCard:
118 kvaserLoop();
[1]119 break;
120 default:
121 break;
122 }
123 counter_ = 0;
124 cout << "Win32CanInterface thread stopped...\n" << endl;
125}
126
127
128/************************************************************************/
129/// The loop used for waiting CAN data from Vector card
130void Win32CanInterface::vectorLoop()
131{
132 while (continue_)
133 {
134 // Wait incoming data from the CAN bus
135 if (canDriver_->receiveFrame(frame_) == 0) {
136 receivedFrames_[counter_].time = road_time();
137 receivedFrames_[counter_].timerange = 0;
138 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
139 semaphore_->release();
140 counter_++;
141 counter_ = counter_ % receivedFramesArraySize_;
142 }
143 }
144}
145
146/************************************************************************/
147/// The loop used for waiting CAN data from XL Vector card
148void Win32CanInterface::vectorXlLoop()
149{
150 while(continue_) {
151 // Wait incoming data from the CAN bus
152 if (canDriver_->receiveFrame(frame_) == 0) {
153 receivedFrames_[counter_].time = road_time();
154 receivedFrames_[counter_].timerange = 0;
155 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
156 semaphore_->release();
157 counter_++;
158 counter_ = counter_ % receivedFramesArraySize_;
159 }
160 }
161}
162
163/************************************************************************/
[89]164/// The loop used for waiting CAN data from Kvaser card
165void Win32CanInterface::kvaserLoop()
166{
167 while(continue_) {
168 // Wait incoming data from the CAN bus
169 if (canDriver_->receiveFrame(frame_) == 0) {
170 receivedFrames_[counter_].time = road_time();
171 receivedFrames_[counter_].timerange = 0;
172 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
173 semaphore_->release();
174 counter_++;
175 counter_ = counter_ % receivedFramesArraySize_;
176 }
177 }
178}
179
180/************************************************************************/
[1]181/* The loop used for waiting CAN data from a shared memory
182/************************************************************************/
[94]183// void Win32CanInterface::shMemLoop() // runtime crash because PosixShMem
184// {
185// shMem_ = new ShMem("CARMEN_CAN_2200", sizeof(TimestampedCanFrame));
186// while (continue_)
187// {
188// // Wait incoming data from the shared memory
189// if ( shMem_->wait(100) )
190// {
191// TimestampedCanFrame* ptr = (TimestampedCanFrame*)(shMem_->read());
192// memcpy(&frame_, &(ptr->frame), sizeof(CanFrame));
193// receivedFrames_[counter_].time = ptr->time;
194// receivedFrames_[counter_].timerange = ptr->timerange;
195// memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
196// semaphore_->release();
197// counter_++;
198// counter_ = counter_ % receivedFramesArraySize_;
199// }
200// } // END while (continue_)
[1]201
[94]202// delete shMem_;
203// }
[1]204
205/************************************************************************/
206/// The loop used for waiting CAN data from Peak card
207void Win32CanInterface::peakLoop()
208{
209 std::cout << "In peak loop" << std::endl;
210 while(continue_)
211 {
212 // Wait incoming data from the CAN bus
213 if ( canDriver_->receiveFrame(frame_) == 0 ) {
214 receivedFrames_[counter_].time = road_time();
215 receivedFrames_[counter_].timerange = 0;
216 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
217 semaphore_->release();
218 counter_++;
219 counter_ = counter_ % receivedFramesArraySize_;
220 }
221 }
222}
223
224/************************************************************************/
[4]225/// The loop used for waiting CAN data from igep card
226void Win32CanInterface::igepLoop()
227{
228 std::cout << "In igep loop" << std::endl;
229
230 while(continue_)
231 {
232 // Wait incoming data from the CAN bus
233 if ( canDriver_->receiveFrame(frame_) == 0 ) {
234 receivedFrames_[counter_].time = road_time();
235 receivedFrames_[counter_].timerange = 0;
236 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
237 semaphore_->release();
238 counter_++;
239 counter_ = counter_ % receivedFramesArraySize_;
240 }
241 }
242}
243/************************************************************************/
[1]244/// Stops the thread
245void Win32CanInterface::stop()
246{
247 continue_ = false;
248}
249
250/************************************************************************/
251/// Defines the place where the incoming frames will be copied
252void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize)
253{
254 receivedFrames_ = framesArray;
255 receivedFramesArraySize_ = framesArraySize;
256}
257
258void Win32CanInterface::setSource(DataSource source)
259{
260 source_ = source;
261}
262
263} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.