CanESD.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 // general includes
00019 
00020 // Headers provided by other cob-packages
00021 #include <cob_generic_can/CanESD.h>
00022 
00023 // Headers provided by other cob-packages which should be avoided/removed
00024 
00025 
00026 //-----------------------------------------------
00027 CanESD::CanESD(const char* cIniFile, bool bObjectMode)
00028 {
00029         m_bObjectMode = bObjectMode;
00030         m_bIsTXError = false;
00031         m_IniFile.SetFileName(cIniFile, "CanESD.cpp");
00032         initIntern();
00033 }
00034 
00035 //-----------------------------------------------
00040 CanESD::~CanESD()
00041 {
00042         std::cout << "Closing CAN handle" << std::endl;
00043         canClose(m_Handle);
00044 }
00045 
00046 //-----------------------------------------------
00047 bool CanESD::init_ret()
00048 {
00049         // Not implemented yet
00050         return false;
00051 }
00052 
00053 //-----------------------------------------------
00054 void CanESD::initIntern()
00055 {
00056         int ret=0;
00057         ret = 0;
00058         int iCanNet = 1;
00059         m_IniFile.GetKeyInt( "CanCtrl", "NetESD", &iCanNet, true);
00060 
00061         int iBaudrateVal = 2;
00062         m_IniFile.GetKeyInt( "CanCtrl", "BaudrateVal", &iBaudrateVal, true);
00063 
00064         std::cout << "Initializing CAN network with id =" << iCanNet << ", baudrate=" << iBaudrateVal << std::endl;
00065 
00066         int iRet;
00067         if( m_bObjectMode )
00068                 iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 1000, 0, &m_Handle);
00069         else
00070                 iRet = canOpen(iCanNet, 0, 10000, 10000, 1000, 0, &m_Handle);
00071         Sleep(300);
00072 
00073         if(iRet == NTCAN_SUCCESS)
00074                 std::cout << "CanESD::CanESD(), init ok" << std::endl;
00075         else
00076                 std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
00077 
00078         iRet = canSetBaudrate(m_Handle, iBaudrateVal);
00079         if(iRet == NTCAN_SUCCESS)
00080                 std::cout << "CanESD::CanESD(), canSetBaudrate ok" << std::endl;
00081         else
00082                 std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
00083         Sleep(300);
00084 
00085         //long lArg;
00086         iRet = canIoctl(m_Handle, NTCAN_IOCTL_FLUSH_RX_FIFO, NULL);
00087 
00088         // MMB/24.02.2006: Add all 11-bit identifiers as there is no loss in performance.
00089         for( int i=0; i<=0x7FF; ++i ) {
00090                 iRet = canIdAdd( m_Handle, i );
00091                 if(iRet != NTCAN_SUCCESS)
00092                         std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
00093         }
00094 
00095 
00096         Sleep(300);
00097 
00098         m_LastID = -1;
00099 }
00100 
00101 //-----------------------------------------------
00108 bool CanESD::transmitMsg(CanMsg CMsg, bool bBlocking)
00109 {
00110         CMSG NTCANMsg;
00111         NTCANMsg.id = CMsg.m_iID;
00112         NTCANMsg.len = CMsg.m_iLen;
00113 
00114         for(int i=0; i<8; i++)
00115                 NTCANMsg.data[i] = CMsg.getAt(i);
00116 
00117         int ret;
00118         int32_t len;
00119         bool bRet = true;
00120 
00121         len = 1;
00122 
00123         if (bBlocking)
00124                 ret = canWrite(m_Handle, &NTCANMsg, &len, NULL);
00125         else
00126                 ret = canSend(m_Handle, &NTCANMsg, &len);
00127 
00128         if( ret != NTCAN_SUCCESS)
00129         {
00130                 std::cout << "error in CANESD::transmitMsg: " << GetErrorStr(ret) << std::endl;
00131                 bRet = false;
00132         }
00133 
00134         m_LastID = (int)NTCANMsg.data[0];
00135 
00136         m_bIsTXError = !bRet;
00137         return bRet;
00138 }
00139 
00140 //-----------------------------------------------
00141 bool CanESD::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
00142 {
00143         //int id = pCMsg->m_iID;
00144         CMSG NTCANMsg;
00145         NTCANMsg.len = 8;
00146 
00147         int32_t len;
00148         int i, ret;
00149         bool bRet = true;
00150 
00151         i=0;
00152 
00153         len = 1;
00154 
00155         do
00156         {
00157                 len = 1;
00158                 ret = canTake(m_Handle, &NTCANMsg, &len);
00159                 i++;
00160                 Sleep(10);
00161         }
00162 
00163         while((len == 0) && (i < iNrOfRetry));
00164 
00165         if(i == iNrOfRetry)
00166         {
00167                 if( ret != NTCAN_SUCCESS )
00168                         std::cout << "error in CANESD::receiveMsgRetry: " << GetErrorStr(ret) << std::endl;
00169 
00170                 bRet = false;
00171         }
00172         else
00173         {
00174                 pCMsg->m_iID = NTCANMsg.id;
00175                 pCMsg->m_iLen = NTCANMsg.len;
00176                 pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
00177                         NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
00178         }
00179 
00180         return bRet;
00181 }
00182 
00183 //-----------------------------------------------
00184 bool CanESD::receiveMsg(CanMsg* pCMsg)
00185 {
00186         CMSG NTCANMsg;
00187         NTCANMsg.len = 8;
00188 
00189         int ret;
00190         int32_t len;
00191         bool bRet = true;
00192 
00193         len = 1;
00194 
00195         // Debug valgrind
00196         NTCANMsg.data[0] = 0;
00197         NTCANMsg.data[1] = 0;
00198         NTCANMsg.data[2] = 0;
00199         NTCANMsg.data[3] = 0;
00200         NTCANMsg.data[4] = 0;
00201         NTCANMsg.data[5] = 0;
00202         NTCANMsg.data[6] = 0;
00203         NTCANMsg.data[7] = 0;
00204         NTCANMsg.msg_lost = 0;
00205         NTCANMsg.id = 0;
00206         NTCANMsg.len = 0;
00207 
00208         pCMsg->set(0,0,0,0,0,0,0,0);
00209 
00210 
00211         if( !isObjectMode() ) {
00212                 pCMsg->m_iID = 0;
00213         } else {
00214                 NTCANMsg.id = pCMsg->m_iID;
00215         }
00216 
00217         ret = canTake(m_Handle, &NTCANMsg, &len);
00218 
00219         if( !isObjectMode() ) {
00220                 if( (len == 1) && (ret == NTCAN_SUCCESS) )
00221                 {
00222                         // message received
00223                         pCMsg->m_iID = NTCANMsg.id;
00224                         pCMsg->m_iLen = NTCANMsg.len;
00225                         pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
00226                                 NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
00227                         bRet = true;
00228                 }
00229                 else
00230                 {
00231                         // no message
00232                         if( ret != NTCAN_SUCCESS)
00233                         {
00234                                 // error
00235                                 std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(ret) << std::endl;
00236                         }
00237 
00238                         pCMsg->m_iID = NTCANMsg.id;
00239                         pCMsg->set(0,0,0,0,0,0,0,0);
00240 
00241                         bRet = false;
00242                 }
00243         } else {
00244                 if( len == 16 ) {
00245                         // No message was received yet.
00246                         pCMsg->m_iID = NTCANMsg.id;
00247                         pCMsg->set(0,0,0,0,0,0,0,0);
00248                         bRet = false;
00249                 } else {
00250                         pCMsg->m_iID = NTCANMsg.id;
00251                         pCMsg->m_iLen = NTCANMsg.len;
00252                         pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
00253                                    NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
00254                         bRet = true;
00255                 }
00256         }
00257 
00258         if( NTCANMsg.msg_lost != 0 )
00259                 std::cout << (int)(NTCANMsg.msg_lost) << " messages lost!" << std::endl;
00260 
00261         return bRet;
00262 }
00263 
00264 
00265 //-----------------------------------------------
00266 bool CanESD::receiveMsgTimeout(CanMsg* pCMsg, int nMicroSecTimeout)
00267 {
00268     // Not implemented yet
00269     return false;
00270 }
00271 
00280 int CanESD::canIdAddGroup(NTCAN_HANDLE handle, int id)
00281 {
00282         int result = NTCAN_SUCCESS;
00283         int i = 0;
00284         int iRes = 0;
00285         int cmd_id = invert(id);
00286 
00287         for( i=0; i<8; ++i) {
00288                 iRes = canIdAdd(m_Handle, cmd_id+i);
00289 
00290                 if( iRes != NTCAN_SUCCESS ) {
00291                         std::cout << "Adding CAN ID " << cmd_id+i << " failed with errorcode: " << iRes << " " << GetErrorStr(iRes) << std::endl;
00292                         result = iRes;
00293                 }
00294         }
00295 
00296         return result;
00297 }
00298 
00299 //-----------------------------------------------
00300 std::string CanESD::GetErrorStr(int ntstatus) const
00301 {
00302         switch (ntstatus)
00303         {
00304         case NTCAN_SUCCESS:                     return "NTCAN_SUCCESS";
00305         case NTCAN_RX_TIMEOUT:                  return "NTCAN_RX_TIMEOUT";
00306         case NTCAN_TX_TIMEOUT:                  return "NTCAN_TX_TIMEOUT";
00307         case NTCAN_TX_ERROR:                    return "NTCAN_TX_ERROR";
00308         case NTCAN_CONTR_OFF_BUS:               return "NTCAN_CONTR_OFF_BUS";
00309         case NTCAN_CONTR_BUSY:                  return "NTCAN_CONTR_BUSY";
00310         case NTCAN_CONTR_WARN:                  return "NTCAN_CONTR_WARN";
00311         case NTCAN_NO_ID_ENABLED:               return "NTCAN_NO_ID_ENABLED";
00312         case NTCAN_ID_ALREADY_ENABLED:          return "NTCAN_ID_ALREADY_ENABLED";
00313         case NTCAN_ID_NOT_ENABLED:              return "NTCAN_ID_NOT_ENABLED";
00314 
00315         case NTCAN_INVALID_FIRMWARE:            return "NTCAN_INVALID_FIRMWARE";
00316         case NTCAN_MESSAGE_LOST:                return "NTCAN_MESSAGE_LOST";
00317         case NTCAN_INVALID_HARDWARE:            return "NTCAN_INVALID_HARDWARE";
00318 
00319         case NTCAN_PENDING_WRITE:               return "NTCAN_PENDING_WRITE";
00320         case NTCAN_PENDING_READ:                return "NTCAN_PENDING_READ";
00321         case NTCAN_INVALID_DRIVER:              return "NTCAN_INVALID_DRIVER";
00322 
00323         case NTCAN_INVALID_PARAMETER:           return "NTCAN_INVALID_PARAMETER";
00324         case NTCAN_INVALID_HANDLE:              return "NTCAN_INVALID_HANDLE";
00325         case NTCAN_NET_NOT_FOUND:               return "NTCAN_NET_NOT_FOUND";
00326         case NTCAN_INSUFFICIENT_RESOURCES:      return "NTCAN_INSUFFICIENT_RESOURCES";
00327 
00328         case NTCAN_OPERATION_ABORTED:           return "NTCAN_OPERATION_ABORTED";
00329         }
00330         char msg[100];
00331         sprintf(msg, "unknown error code %d", ntstatus);
00332         return msg;
00333 }
00334 
00344 int CanESD::readEvent()
00345 {
00346         EVMSG evmsg;
00347         int iRet = 0;
00348         int ret;
00349 
00350         ret = canReadEvent(m_Handle, &evmsg, NULL);
00351 
00352         if(ret == NTCAN_SUCCESS)
00353         {
00354                 if( (int)evmsg.evid == NTCAN_EV_CAN_ERROR ) {
00355                         switch( evmsg.evdata.s[0] ) {
00356                                 case 0x00:
00357                                         iRet = 0;
00358                                         break;
00359                                 case 0xC0:
00360                                         iRet = -6;
00361                                         std::cout << "BUS OFF" << std::endl;
00362                                         break;
00363                                 case 0x40:
00364                                         iRet = -7;
00365                                         std::cout << "ERROR PASSIVE" << std::endl;
00366                                         break;
00367                         }
00368                         if( evmsg.evdata.s[3] != 0 ) {
00369                                 iRet = -3;
00370                                 std::cout << "Lost " << (int)evmsg.evdata.s[3] << " messages" << std::endl;
00371                         } else if( evmsg.evdata.s[5] != 0 ) {
00372                                 iRet = -5;
00373                                 std::cout << "Lost " << (int)evmsg.evdata.s[5] << " messages from fifo" << std::endl;
00374                         }
00375                 }
00376         }
00377         return iRet;
00378 }
00379 


cob_generic_can
Author(s): Christian Connette
autogenerated on Sat Jun 8 2019 21:02:26