- Timestamp:
- Dec 16, 2015, 4:57:50 PM (9 years ago)
- Location:
- trunk/CanGateway
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CanGateway/CanGateway.cpp
r91 r116 59 59 60 60 // set the exhange parameters for incoming CAN frames 61 canIf_.setExchangeBuffer(incomingCanFrames_, INCOMINGCANFRAMES_SIZE); 62 canIf_.setSignalSempahore(&semaphore_); 63 if (source_ == "shMem") 64 canIf_.setSource(Win32CanInterface::SharedMemory); 61 canIfRead_.setExchangeBuffer(incomingCanFrames_, INCOMINGCANFRAMES_SIZE); 62 canIfRead_.setSignalSempahore(&semaphore_); 63 64 if (source_ == "shMem") { 65 sameCanIf = true; 66 canIfRead_.setSource(Win32CanInterface::SharedMemory); 67 } 65 68 else if (source_ == "vector") 66 69 { 67 canIf_.setSource(Win32CanInterface::VectorCard); 70 sameCanIf = true; 71 canIfRead_.setSource(Win32CanInterface::VectorCard); 68 72 // open the interface 69 if (!canIf _.openInterface(channel_, speed_))73 if (!canIfRead_.openInterface(channel_, speed_)) 70 74 qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_); 71 75 } 72 76 else if (source_ == "vectorXL") 73 77 { 74 canIf_.setSource(Win32CanInterface::XLVectorCard); 78 sameCanIf = true; 79 canIfRead_.setSource(Win32CanInterface::XLVectorCard); 75 80 // open the interface 76 if (!canIf _.openInterface(channel_, speed_))81 if (!canIfRead_.openInterface(channel_, speed_)) 77 82 qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_); 78 83 } 79 84 else if (source_ == "peak") 80 85 { 81 canIf_.setSource(Win32CanInterface::PeakCard); 82 // open interface 83 if (canIf_.openInterface(port_, accessMode_)==0) 86 sameCanIf = true; 87 canIfRead_.setSource(Win32CanInterface::PeakCard); 88 // open interface 89 if (canIfRead_.openInterface(port_, accessMode_)==0) 84 90 qFatal("Failed to open the CAN interface port %s in %s mode",port_, accessMode_); 85 91 } 86 92 else if (source_ == "igep") 87 93 { 88 canIf_.setSource(Win32CanInterface::igepCard); 89 // open interface 90 if (canIf_.openInterface(port_, accessMode_)==0) 94 sameCanIf = true; 95 canIfRead_.setSource(Win32CanInterface::igepCard); 96 // open interface 97 if (canIfRead_.openInterface(port_, accessMode_)==0) 91 98 qFatal("Failed to open the CAN interface port %s in %s mode",port_, accessMode_); 92 99 } 93 100 else if (source_ == "kvaser") 94 101 { 95 canIf_.setSource(Win32CanInterface::KvaserCard); 96 // open interface 97 if (canIf_.openInterface(channel_,speed_)==0) 102 sameCanIf = false; 103 canIfRead_.setSource(Win32CanInterface::KvaserCard); 104 // open interface 105 if (canIfRead_.openInterface(channel_,speed_)==0) 106 qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_); 107 108 canIfWrite_.setSource(Win32CanInterface::KvaserCard); 109 // open interface 110 if (canIfWrite_.openInterface(channel_,speed_)==0) 98 111 qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_); 99 112 } … … 105 118 106 119 // start the 2 threads: reception thread and decoding thread 107 canIf_.start(); 120 canIfRead_.start(); 121 108 122 start(); 109 123 } … … 117 131 { 118 132 counter_ = 0; 119 canIf_.stop(); 120 121 if ((source_ == "vector")||(source_=="peak")||(source_=="vectorXL")||(source_=="igep")||(source_=="kvaser")) 122 canIf_.closeInterface(channel_); 133 134 canIfRead_.stop(); 135 136 if ((source_ == "vector")||(source_=="peak")||(source_=="vectorXL")||(source_=="igep")||(source_=="kvaser")) { 137 canIfRead_.closeInterface(channel_); 138 if(!sameCanIf) 139 canIfWrite_.closeInterface(channel_); 140 } 123 141 124 canIf _.wait();142 canIfRead_.wait(); 125 143 126 144 // we stop the decoding thread … … 167 185 bool CanGateway::sendFrame(const CanFrame frame) 168 186 { 169 if (canIf_.canDriver_ == NULL) 170 return false; 187 if(sameCanIf) 188 { 189 if (canIfRead_.canDriver_ == NULL) 190 return false; 191 else 192 { 193 if (canIfWrite_.canDriver_->sendFrame(frame)) 194 return true; 195 else 196 return false; 197 } 198 } 171 199 else 172 200 { 173 if (canIf _.canDriver_->sendFrame(frame))174 return true;201 if (canIfWrite_.canDriver_ == NULL) 202 return false; 175 203 else 176 return false; 204 { 205 if (canIfWrite_.canDriver_->sendFrame(frame)) 206 return true; 207 else 208 return false; 209 } 177 210 } 178 211 } -
trunk/CanGateway/CanGateway.h
r91 r116 62 62 void toc(char * text) { printf("duration = %d %s\n", (int)(road_time() - tic_), text ); } 63 63 64 Win32CanInterface canIf_; 64 Win32CanInterface canIfRead_; 65 Win32CanInterface canIfWrite_; 66 bool sameCanIf; 65 67 QSemaphore semaphore_; 66 68 TimestampedCanFrame incomingCanFrames_[INCOMINGCANFRAMES_SIZE]; -
trunk/CanGateway/Win32CanInterface.cpp
r94 r116 5 5 // author: Gerald Dherbomez 6 6 // Copyright Heudiasyc UMR UTC/CNRS 7253 7 // 7 // 8 8 // version: $Id: $ 9 9 // … … 37 37 38 38 /************************************************************************/ 39 /// Opens the CAN interface of the number given in argument 39 /// Opens the CAN interface of the number given in argument 40 40 bool Win32CanInterface::openInterface(const int number, const unsigned int speed) 41 41 { 42 42 canDriver_ = new CanDriver(number, speed); 43 44 //connection to CAN bus 43 44 //connection to CAN bus 45 45 if(canDriver_->initPort() != 0) 46 46 { … … 69 69 /************************************************************************/ 70 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 71 /// todo: close only the port identified by number 72 /// make a function that close the driver or close the driver only if 73 73 /// there is no more pending connections 74 74 bool Win32CanInterface::closeInterface(const int /*number*/) 75 75 { 76 if(canDriver_->cleanUpPort() != 0) 77 { 78 cout << "CAN disconnection fails" << endl; 79 delete canDriver_; 80 return false; 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 } 81 88 } 82 89 else 83 {84 delete canDriver_;85 90 return true; 86 }87 91 } 88 92 … … 95 99 96 100 if (!receivedFramesArraySize_) 97 qFatal("receivedFramesArraySize_ not initialized, division by zero may occur if you continue. Context:%s:L%d",__FILE__, __LINE__); 101 qFatal("receivedFramesArraySize_ not initialized, division by zero may occur if you continue. Context:%s:L%d",__FILE__, __LINE__); 98 102 99 103 cout << "Win32CanInterface starts" << endl; … … 102 106 { 103 107 case VectorCard: 104 vectorLoop(); 108 case PeakCard: 109 case igepCard: 110 case XLVectorCard: 111 case KvaserCard: 112 defaultLoop(); 105 113 break; 106 // case SharedMemory: // runtime crash because PosixShMem 107 // shMemLoop(); 108 // break; 109 case PeakCard: 110 peakLoop(); 111 break; 112 case igepCard: 113 igepLoop(); 114 break; 115 case XLVectorCard: 116 vectorXlLoop(); 117 case KvaserCard: 118 kvaserLoop(); 119 break; 120 default: 114 default: 121 115 break; 122 116 } … … 128 122 /************************************************************************/ 129 123 /// The loop used for waiting CAN data from Vector card 130 void 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 } 124 // Default for vector, vectorXL, kvaser, Peak, Igep 145 125 146 /************************************************************************/ 147 /// The loop used for waiting CAN data from XL Vector card 148 void Win32CanInterface::vectorXlLoop() 126 void Win32CanInterface::defaultLoop() 149 127 { 150 128 while(continue_) { 151 // Wait incoming data from the CAN bus 129 // Wait incoming data from the CAN bus 152 130 if (canDriver_->receiveFrame(frame_) == 0) { 153 131 receivedFrames_[counter_].time = road_time(); … … 155 133 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame)); 156 134 semaphore_->release(); 157 counter_++; 158 counter_ = counter_ % receivedFramesArraySize_; 159 } 160 } 161 } 162 163 /************************************************************************/ 164 /// The loop used for waiting CAN data from Kvaser card 165 void 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_++; 135 counter_++; 175 136 counter_ = counter_ % receivedFramesArraySize_; 176 137 } … … 195 156 // memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) ); 196 157 // semaphore_->release(); 197 // counter_++; 158 // counter_++; 198 159 // counter_ = counter_ % receivedFramesArraySize_; 199 160 // } 200 161 // } // END while (continue_) 201 162 202 163 // delete shMem_; 203 164 // } 204 165 205 /************************************************************************/206 /// The loop used for waiting CAN data from Peak card207 void Win32CanInterface::peakLoop()208 {209 std::cout << "In peak loop" << std::endl;210 while(continue_)211 {212 // Wait incoming data from the CAN bus213 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 /************************************************************************/225 /// The loop used for waiting CAN data from igep card226 void Win32CanInterface::igepLoop()227 {228 std::cout << "In igep loop" << std::endl;229 230 while(continue_)231 {232 // Wait incoming data from the CAN bus233 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 166 /************************************************************************/ 244 167 /// Stops the thread … … 251 174 /// Defines the place where the incoming frames will be copied 252 175 void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize) 253 { 176 { 254 177 receivedFrames_ = framesArray; 255 178 receivedFramesArraySize_ = framesArraySize; -
trunk/CanGateway/Win32CanInterface.h
r94 r116 59 59 protected: 60 60 void run(); 61 void vectorLoop(); 61 62 62 //void shMemLoop(); // runtime crash because PosixShMem 63 void peakLoop(); 64 void igepLoop(); 65 void vectorXlLoop(); 66 void kvaserLoop(); 63 void defaultLoop(); 67 64 68 65 -
trunk/CanGateway/driver/KVaserCanDriver.cpp
r94 r116 4 4 5 5 #include "KVaserCanDriver.h" 6 /* 6 7 #ifdef WIN32 7 8 #include "kvaser/windows/canlib.h" … … 9 10 #include "kvaser/linux/canlib.h" 10 11 #endif 12 */ 13 14 15 #include <canlib.h> 16 11 17 #include <cassert> 12 18 #include <cstdio> … … 44 50 // Check a status code and issue an error message if the code isn't canOK. 45 51 // 46 void ErrorExit( char*id, canStatus stat)52 void ErrorExit(string id, canStatus stat) 47 53 { 48 54 char buf[50]; … … 123 129 124 130 125 void KVaserCanDriver::displayHardware() 131 void KVaserCanDriver::displayHardware() 126 132 { 127 133 LOG_INFO("----------------------------------------------------------"); … … 139 145 canHandle_ = canOpenChannel(hwChannel, 0); 140 146 if (canHandle_ < 0) { 141 ErrorExit("canOpenChannel", (canStatus) canHandle_);147 ErrorExit("canOpenChannel", (canStatus) canHandle_); 142 148 } 143 else 149 else 144 150 { 145 151 int ret = 0; … … 153 159 break; 154 160 case 500000: 155 ret = canSetBusParams(canHandle_, canBITRATE_500K, 0, 0, 0, 0, 0);161 ret = canSetBusParams(canHandle_, canBITRATE_500K, 4, 3, 1, 1, 0); 156 162 break; 157 163 default: … … 166 172 if (ret < 0) { 167 173 ErrorExit("canBusOn", (canStatus)ret); 168 174 169 175 } 170 176 } … … 184 190 canInitializeLibrary(); 185 191 #endif 186 192 187 193 // open the physical CAN interface 188 194 initialize(kvaserHardwareType_, canChannel_, canBitRate_); … … 199 205 short KVaserCanDriver::cleanUpPort (void) 200 206 { 201 207 202 208 //traceXLCommand("xlCloseDriver", xlCloseDriver()); 203 209 … … 215 221 { 216 222 canStatus stat = canWrite(canHandle_, frame.id,frame.data, frame.dlc, 0); 217 if (stat == canOK ) 223 if (stat == canOK ) 218 224 { 219 225 return 0; 220 226 } else { 221 227 LOG_WARN("Kvaser driver - sendFrame method - Failed to send the CAN frame"); 222 return 1; 228 return 1; 223 229 } 224 230 } … … 236 242 //DWORD timestamp; 237 243 unsigned long timestamp; 238 244 239 245 canStatus stat = canReadWait(canHandle_, &id, msg, &dlc, &flags, ×tamp, WAIT_RECEIVING_FRAME_TIMEOUT); 240 if (stat == canOK ) 246 if (stat == canOK ) 241 247 { 242 if ((flags & canMSG_ERROR_FRAME) == 0) 248 if ((flags & canMSG_ERROR_FRAME) == 0) 243 249 { 244 250 // Get the message data … … 254 260 memcpy(frame.data, msg, frame.dlc); 255 261 return 0; 256 } 257 } 262 } 263 } 258 264 else if (stat == canERR_NOMSG ) { 259 265 // timeout occurs 260 266 LOG_WARN("Kvaser card - receiveFrame() method - TIMEOUT"); 261 267 return 1; 262 } 268 } 263 269 else { 264 270 // An error frame.
Note:
See TracChangeset
for help on using the changeset viewer.