CanOpenReceiveThread.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of the SCHUNK Canopen Driver suite.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
00012 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00023 //----------------------------------------------------------------------
00024 #include "CanOpenReceiveThread.h"
00025 
00026 
00027 #include "Logging.h"
00028 
00029 namespace icl_hardware {
00030 namespace canopen_schunk {
00031 
00032 CanOpenReceiveThread::CanOpenReceiveThread( const icl_core::TimeSpan& period,
00033                                 const boost::shared_ptr<icl_hardware::can::tCanDevice>& can_device,
00034                                 ReceivedPacketCallback const& received_callback)
00035   : m_period_time_ms(period.toMSec()),
00036     m_can_device (can_device),
00037     m_received_callback(received_callback)
00038 {
00039   m_thread = boost::thread(&CanOpenReceiveThread::workerFunction, this);
00040 }
00041 
00042 CanOpenReceiveThread::~CanOpenReceiveThread()
00043 {
00044   stop();
00045   m_thread.join();
00046 }
00047 
00048 
00049 void CanOpenReceiveThread::workerFunction()
00050 {
00051   int32_t receive_result;
00052   while (true)
00053   {
00054     if (m_can_device)// != NULL)
00055     {
00056       if (m_can_device->IsInitialized())
00057       {
00058         receive_result = receiveData();
00059         if (receive_result == -ENODATA)
00060         {
00061           // currently do nothing
00062         }
00063         else if (receive_result != 0)
00064         {
00065           LOGGING_ERROR_C (CanOpen, CanOpenReceiveThread, "Reading CAN message failed, received error code " << receive_result << ". Doing nothing." << endl);
00066         }
00067       }
00068       else
00069       {
00070         LOGGING_WARNING_C(CanOpen, CanOpenReceiveThread, "Cannot read data from can device. It is not initialized!" << endl);
00071       }
00072     }
00073 
00074     // Wait for the thread period so that the timing is in sync.
00075     try
00076     {
00077         boost::this_thread::sleep(boost::posix_time::milliseconds(m_period_time_ms));
00078     }
00079     catch(boost::thread_interrupted&)
00080     {
00081         return;
00082     }
00083   }
00084 }
00085 
00086 void CanOpenReceiveThread::stop()
00087 {
00088   m_thread.interrupt();
00089 }
00090 
00091 int32_t CanOpenReceiveThread::receiveData()
00092 {
00093   int32_t receive_result = m_can_device->Receive(m_can_msg);
00094   if ( receive_result > 0)
00095   {
00096     m_received_callback(m_can_msg);
00097   }
00098   else
00099   {
00100     return receive_result;
00101   }
00102   return 0;
00103 }
00104 
00105 
00106 
00107 }} // end of NS


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Sun May 22 2016 03:30:56