Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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)
00055 {
00056 if (m_can_device->IsInitialized())
00057 {
00058 receive_result = receiveData();
00059 if (receive_result == -ENODATA)
00060 {
00061
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
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 }}