Changeset 116 in pacpussensors for trunk/CanGateway/Win32CanInterface.cpp


Ignore:
Timestamp:
Dec 16, 2015, 4:57:50 PM (9 years ago)
Author:
ydroniou
Message:

Fix KVaser for Linux \o/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CanGateway/Win32CanInterface.cpp

    r94 r116  
    55//  author:     Gerald Dherbomez
    66//              Copyright Heudiasyc UMR UTC/CNRS 7253
    7 // 
     7//
    88//  version:    $Id: $
    99//
     
    3737
    3838/************************************************************************/
    39 /// Opens the CAN interface of the number given in argument 
     39/// Opens the CAN interface of the number given in argument
    4040bool Win32CanInterface::openInterface(const int number, const unsigned int speed)
    4141{
    4242  canDriver_ = new CanDriver(number, speed);
    43            
    44   //connection to CAN   bus 
     43
     44  //connection to CAN   bus
    4545  if(canDriver_->initPort() != 0)
    4646  {
     
    6969/************************************************************************/
    7070/// 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
    7373///       there is no more pending connections
    7474bool Win32CanInterface::closeInterface(const int /*number*/)
    7575{
    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    }
    8188  }
    8289  else
    83   {
    84     delete canDriver_;
    8590    return true;
    86   }
    8791}
    8892
     
    9599
    96100  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__);
    98102
    99103  cout << "Win32CanInterface starts" << endl;
     
    102106  {
    103107  case VectorCard:
    104     vectorLoop();
     108  case PeakCard:
     109  case igepCard:
     110  case XLVectorCard:
     111  case KvaserCard:
     112    defaultLoop();
    105113    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:
    121115    break;
    122116  }
     
    128122/************************************************************************/
    129123/// 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
    145125
    146 /************************************************************************/
    147 /// The loop used for waiting CAN data from XL Vector card
    148 void Win32CanInterface::vectorXlLoop()
     126void Win32CanInterface::defaultLoop()
    149127{
    150128  while(continue_) {
    151     // Wait incoming data from the CAN bus 
     129    // Wait incoming data from the CAN bus
    152130    if (canDriver_->receiveFrame(frame_) == 0) {
    153131      receivedFrames_[counter_].time = road_time();
     
    155133      memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
    156134      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_++;
    175136      counter_ = counter_ % receivedFramesArraySize_;
    176137    }
     
    195156//       memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
    196157//       semaphore_->release();
    197 //       counter_++; 
     158//       counter_++;
    198159//       counter_ = counter_ % receivedFramesArraySize_;
    199160//     }
    200161//   } // END while (continue_)
    201  
     162
    202163//   delete shMem_;
    203164// }
    204165
    205 /************************************************************************/
    206 /// The loop used for waiting CAN data from Peak card
    207 void 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 /************************************************************************/
    225 /// The loop used for waiting CAN data from igep card
    226 void 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 }
    243166/************************************************************************/
    244167/// Stops the thread
     
    251174/// Defines the place where the incoming frames will be copied
    252175void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize)
    253 { 
     176{
    254177  receivedFrames_ = framesArray;
    255178  receivedFramesArraySize_ = framesArraySize;
Note: See TracChangeset for help on using the changeset viewer.