Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cob_generic_can/SocketCan.h>
00019 #include <stdlib.h>
00020 #include <cerrno>
00021 #include <cstring>
00022 #include <stdlib.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <fcntl.h>
00026 #include <unistd.h>
00027
00028 SocketCan::SocketCan(const char* device, int baudrate)
00029 {
00030 m_bInitialized = false;
00031
00032 p_cDevice = device;
00033 m_handle.reset(new can::ThreadedSocketCANInterface());
00034 }
00035
00036 SocketCan::SocketCan(const char* device)
00037 {
00038 m_bInitialized = false;
00039
00040 p_cDevice = device;
00041 m_handle.reset(new can::ThreadedSocketCANInterface());
00042 }
00043
00044
00045 SocketCan::~SocketCan()
00046 {
00047 if (m_bInitialized)
00048 {
00049 m_handle->shutdown();
00050 }
00051 }
00052
00053
00054 bool SocketCan::init_ret()
00055 {
00056 bool ret = true;
00057 if (!m_handle->init(p_cDevice, false))
00058 {
00059 print_error(m_handle->getState());
00060 ret = false;
00061 }
00062 else
00063 {
00064 m_reader.listen((boost::shared_ptr<can::CommInterface>) m_handle);
00065 m_bInitialized = true;
00066 bool bRet = true;
00067 ret = true;
00068 }
00069 return ret;
00070 }
00071
00072
00073 void SocketCan::init()
00074 {
00075 if (!init_ret())
00076 {
00077 sleep(3);
00078 exit(0);
00079 }
00080 }
00081
00082
00083
00084 bool SocketCan::transmitMsg(CanMsg CMsg, bool bBlocking)
00085 {
00086 can::Header header(CMsg.getID(), false, false, false);
00087 can::Frame message(header, CMsg.getLength());
00088 for (int i = 0; i < CMsg.getLength(); i++)
00089 {
00090 message.data[i] = CMsg.getAt(i);
00091 }
00092 return m_handle->send(message);
00093 }
00094
00095
00096 bool SocketCan::receiveMsg(CanMsg* pCMsg)
00097 {
00098 if (!m_bInitialized)
00099 {
00100 return false;
00101 }
00102
00103 bool bRet = false;
00104 can::Frame frame;
00105
00106 if (m_reader.read(&frame, boost::chrono::seconds(1)))
00107 {
00108 pCMsg->setID(frame.id);
00109 pCMsg->setLength(frame.dlc);
00110 pCMsg->set(frame.data[0], frame.data[1], frame.data[2], frame.data[3],
00111 frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
00112 bRet = true;
00113 }
00114 return bRet;
00115 }
00116
00117
00118 bool SocketCan::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
00119 {
00120 if (!m_bInitialized)
00121 {
00122 return false;
00123 }
00124
00125 can::Frame frame;
00126 bool bRet = false;
00127 int i = 0;
00128
00129 do
00130 {
00131 if (m_reader.read(&frame, boost::chrono::milliseconds(10)))
00132 {
00133 pCMsg->setID(frame.id);
00134 pCMsg->setLength(frame.dlc);
00135 pCMsg->set(frame.data[0], frame.data[1], frame.data[2], frame.data[3],
00136 frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
00137 bRet = true;
00138 break;
00139 }
00140 i++;
00141 }
00142 while ((i < iNrOfRetry && bRet != true));
00143 return bRet;
00144 }
00145
00146
00147 bool SocketCan::receiveMsgTimeout(CanMsg* pCMsg, int nMicroSecTimeout)
00148 {
00149 if (!m_bInitialized)
00150 {
00151 return false;
00152 }
00153
00154 bool bRet = false;
00155 can::Frame frame;
00156
00157 if (m_reader.read(&frame, boost::chrono::microseconds(nMicroSecTimeout)))
00158 {
00159 pCMsg->setID(frame.id);
00160 pCMsg->setLength(frame.dlc);
00161 pCMsg->set(frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
00162 bRet = true;
00163 }
00164 return bRet;
00165 }
00166
00167 void SocketCan::print_error(const can::State& state)
00168 {
00169 std::string err;
00170 std::cout << "ERROR: state=" << std::endl;
00171 m_handle->translateError(state.internal_error, err);
00172 std::cout << "ERROR: state=" << state.driver_state << " internal_error=" << state.internal_error << "('" << err << "') asio: " << state.error_code << std::endl;
00173 }