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

Last change on this file since 127 was 116, checked in by ydroniou, 9 years ago

Fix KVaser for Linux \o/

File size: 5.2 KB
Line 
1/*********************************************************************
2// created: 2008/2/11 - 11:59
3// filename: Win32CanInterface.cpp
4//
5// author: Gerald Dherbomez
6// Copyright Heudiasyc UMR UTC/CNRS 7253
7//
8// version: $Id: $
9//
10// purpose: Management of the Can Interface
11//
12*********************************************************************/
13
14#include "Win32CanInterface.h"
15
16// #include "Pacpus/PacpusTools/ShMem.h" // runtime crash because PosixShMem
17
18namespace pacpus {
19
20using namespace std;
21
22/************************************************************************/
23/// Constructor
24Win32CanInterface::Win32CanInterface()
25{
26 continue_ = true;
27 counter_ = 0;
28 receivedFramesArraySize_ = 0;
29 canDriver_ = NULL;
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_ != NULL) {
77 if(canDriver_->cleanUpPort() != 0)
78 {
79 cout << "CAN disconnection fails" << endl;
80 delete canDriver_;
81 return false;
82 }
83 else
84 {
85 delete canDriver_;
86 return true;
87 }
88 }
89 else
90 return true;
91}
92
93/************************************************************************/
94/// The main loop of the class
95void Win32CanInterface::run()
96{
97 continue_ = true;
98 counter_ = 0;
99
100 if (!receivedFramesArraySize_)
101 qFatal("receivedFramesArraySize_ not initialized, division by zero may occur if you continue. Context:%s:L%d",__FILE__, __LINE__);
102
103 cout << "Win32CanInterface starts" << endl;
104
105 switch (source_)
106 {
107 case VectorCard:
108 case PeakCard:
109 case igepCard:
110 case XLVectorCard:
111 case KvaserCard:
112 defaultLoop();
113 break;
114 default:
115 break;
116 }
117 counter_ = 0;
118 cout << "Win32CanInterface thread stopped...\n" << endl;
119}
120
121
122/************************************************************************/
123/// The loop used for waiting CAN data from Vector card
124// Default for vector, vectorXL, kvaser, Peak, Igep
125
126void Win32CanInterface::defaultLoop()
127{
128 while(continue_) {
129 // Wait incoming data from the CAN bus
130 if (canDriver_->receiveFrame(frame_) == 0) {
131 receivedFrames_[counter_].time = road_time();
132 receivedFrames_[counter_].timerange = 0;
133 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
134 semaphore_->release();
135 counter_++;
136 counter_ = counter_ % receivedFramesArraySize_;
137 }
138 }
139}
140
141/************************************************************************/
142/* The loop used for waiting CAN data from a shared memory
143/************************************************************************/
144// void Win32CanInterface::shMemLoop() // runtime crash because PosixShMem
145// {
146// shMem_ = new ShMem("CARMEN_CAN_2200", sizeof(TimestampedCanFrame));
147// while (continue_)
148// {
149// // Wait incoming data from the shared memory
150// if ( shMem_->wait(100) )
151// {
152// TimestampedCanFrame* ptr = (TimestampedCanFrame*)(shMem_->read());
153// memcpy(&frame_, &(ptr->frame), sizeof(CanFrame));
154// receivedFrames_[counter_].time = ptr->time;
155// receivedFrames_[counter_].timerange = ptr->timerange;
156// memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
157// semaphore_->release();
158// counter_++;
159// counter_ = counter_ % receivedFramesArraySize_;
160// }
161// } // END while (continue_)
162
163// delete shMem_;
164// }
165
166/************************************************************************/
167/// Stops the thread
168void Win32CanInterface::stop()
169{
170 continue_ = false;
171}
172
173/************************************************************************/
174/// Defines the place where the incoming frames will be copied
175void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize)
176{
177 receivedFrames_ = framesArray;
178 receivedFramesArraySize_ = framesArraySize;
179}
180
181void Win32CanInterface::setSource(DataSource source)
182{
183 source_ = source;
184}
185
186} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.