int32_t initNTCAN(uint32_t baud, uint32_t flags, int32_t ID[], int32_t net, int32_t rxSize, int32_t rxTime, int32_t txSize, int32_t txTime) { NTCAN_HANDLE handle; // NTCAN_RESULT result; // Used for checking for error int32_t i; // Index for multiple objects int32_t timeout=0; // Current number of timeouts /* Open can */ result = canOpen(net,flags,txSize,rxSize, // Opens device txTime,rxTime,&handle); if(errorCheck(CAN_OPEN,result) != 0) // Error check { return 0xFFFF; // returns 1 if error } // printf("canOpen() success\n"); /* Setting baudrate */ result = canSetBaudrate(handle,baud); // sets baudrate if(errorCheck(CAN_SET_BAUDRATE,result) != 0) // Error check { return 0xFFFF; // returns 1 if error } result = canGetBaudrate(handle,&baud); // Reads buadrate // printf("canSetBaudrate() success. Baudrate is %d\n",baud); /* Adding an ID */ for(i=1; i<ID[0]; i++) { do { result = canIdAdd(handle,ID[i]); // Adds ID to handle timeout++; // reapeat if Timeout if(timeout>MAX_TIMEOUT) { printf("Max timeout out reached, Aborting addID\n"); return 0xFFFF; } // return if repeated error } while( errorCheck(CAN_ID_ADD,result) == 2); if(errorCheck(CAN_ID_ADD,result) != 0) // error check { return 0xFFFF; } // printf("canIdAdd() successful\n"); } printf("Initializing sucessfull\n"); /* Flushing FIFO buffer */ result = canIoctl(handle,NTCAN_IOCTL_FLUSH_RX_FIFO,NULL); if(errorCheck(CAN_IO_CTL,result) != 0) // flushing FIFO { return 0xFFFF; // error check } // printf("System flushed, device ready to use\n"); return handle; // returns handle for NTCAN device }
int CESDDevice::setMessageId(unsigned long uiMessageId) { int iRetVal = 0; m_iErrorState = 0; iRetVal = canIdAdd(m_hDevice, uiMessageId); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } return m_iErrorState; }
//----------------------------------------------- CanESD::CanESD(int iIniBaudRate, int iNrNet) { //initial values int net=1; /* logical net number (here: 0) */ uint32_t mode=0; /* mode used for canOpen() */ int32_t txqueuesize=100; /* size of transmit queue */ int32_t rxqueuesize=100; /* size of receive queue */ int32_t txtimeout=0; /* timeout for transmit operations in ms */ int32_t rxtimeout=0; /* timeout for receive operations in ms */ NTCAN_RESULT retvalue; /* return values of NTCAN API calls */ //uint32_t baud=2; /* configured CAN baudrate (here: 500 kBit/s.) */ CMSG cmsg[8]; /* can message buffer */ int rtr=0; /* rtr bit */ int32_t len; /* # of CAN messages */ retvalue = canOpen(iNrNet, mode, txqueuesize, rxqueuesize, txtimeout, rxtimeout, &m_Handle); if (retvalue != NTCAN_SUCCESS) { LOGINFO("canOpen() failed: " << getErrorMessage(retvalue)); } Sleep(300); retvalue = canSetBaudrate(m_Handle, getBaudRate(iIniBaudRate)); if (retvalue != 0) { LOGINFO("canSetBaudrate() failed: " << getErrorMessage(retvalue)); canClose(m_Handle); } else { LOGINFO("Baudrate set to " << getBaudRate(iIniBaudRate)); } Sleep(300); //set Amtek Arm IDs int i; for(i=1; i < 2048; i++) { retvalue = canIdAdd(m_Handle, i); if (retvalue != NTCAN_SUCCESS) { LOGERROR("canIdAdd() failed: " << getErrorMessage(retvalue)); } } Sleep(300); m_LastID = -1; }
/** * Add a group of CAN identifier to the handle, so it can be received. * The identifiers are generated by inverting the id and adding each value between 0 and 7 * This is used for generating the answer commands by the RCS5000. * @param handle The handle to add the identifiers to. * @param id The command id sent to the RCS5000. * @return NTCAN_SUCESS if ok, or an error code. */ int CanESD::canIdAddGroup(HANDLE handle, int id) { int result = NTCAN_SUCCESS; int i = 0; int iRes = 0; int cmd_id = invert(id); //m_Mutex.lock(); for( i=0; i<8; ++i) { iRes = canIdAdd(m_Handle, cmd_id+i); //std::cout << "Adding CAN ID " << cmd_id+i << std::endl; if( iRes != NTCAN_SUCCESS ) { std::cout << "Adding CAN ID " << cmd_id+i << " failed with errorcode: " << iRes << " " << GetErrorStr(iRes) << std::endl; result = iRes; } } //m_Mutex.unlock(); return result; }
//----------------------------------------------- void CanESD::initIntern() { int ret=0; /* Does this make any sense as the handle is opened somewhere later?! //m_Handle = 9; //m_Mutex.lock(); //ret=canClose(m_Handle); if(ret == NTCAN_SUCCESS) { std::cout << "Can close ok ret = " << ret std::endl; } CAN_IF_STATUS *status; ret=canStatus(m_Handle,status); if(ret == NTCAN_SUCCESS) { std::cout << "Get status from can card ok ret = " << ret << std::endl; } */ ret = 0; int iCanNet = 1; //m_IniFile.GetKeyInt( "CanCtrl", "NetESD", &iCanNet, true); int iBaudrateVal = NTCAN_BAUD_250; //m_IniFile.GetKeyInt( "CanCtrl", "BaudrateVal", &iBaudrateVal, true); std::cout << "Initializing CAN network with id =" << iCanNet << ", baudrate=" << iBaudrateVal << std::endl; int iRet; if( m_bObjectMode ) //iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 0, 0, &m_Handle); iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 1000, 0, &m_Handle); else //iRet = canOpen(iCanNet, 0, 10000, 10000, 0, 0, &m_Handle); iRet = canOpen(iCanNet, 0, 10000, 10000, 1000, 0, &m_Handle); sleep(0.3); if(iRet == NTCAN_SUCCESS) std::cout << "CanESD::CanESD(), init ok" << std::endl; else std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl; /* ret=canClose(m_Handle); if(ret == NTCAN_SUCCESS) { std::cout << "Can close ok ret = " << ret << std::endl; if( m_bObjectMode ) iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 0, 0, &m_Handle); else iRet = canOpen(iCanNet, 0, 10000, 10000, 0, 0, &m_Handle); Sleep(300); if(iRet == NTCAN_SUCCESS) std::cout << "CanESD::CanESD(), init ok" << ret << std::endl; else std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl; } */ iRet = canSetBaudrate(m_Handle, iBaudrateVal); if(iRet == NTCAN_SUCCESS) std::cout << "CanESD::CanESD(), canSetBaudrate ok" << std::endl; else std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl; sleep(0.3); long lArg; iRet = canIoctl(m_Handle, NTCAN_IOCTL_FLUSH_RX_FIFO, NULL); //if( iRet == NTCAN_SUCCESS ) // std::cout << (int)(lArg) << " messages in CAN receive queue" << std::endl; // MMB/24.02.2006: Add all 11-bit identifiers as there is no loss in performance. for( int i=0; i<=0x7FF; ++i ) { iRet = canIdAdd( m_Handle, i ); if(iRet != NTCAN_SUCCESS) std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl; } /* iRet = canIdAddGroup(m_Handle, 0x98); // for COB Arm iRet = canIdAdd(m_Handle, 0x100); iRet = canIdAdd(m_Handle, 0x101); iRet = canIdAdd(m_Handle, 0x200); iRet = canIdAdd(m_Handle, 0x201); iRet = canIdAdd(m_Handle, 0x300); iRet = canIdAdd(m_Handle, 0x301); iRet = canIdAdd(m_Handle, 0x400); iRet = canIdAdd(m_Handle, 0x401); iRet = canIdAdd(m_Handle, 0x500); iRet = canIdAdd(m_Handle, 0x501); iRet = canIdAdd(m_Handle, 0x600); iRet = canIdAdd(m_Handle, 0x601); // for CAN-Open Harmonica iRet = canIdAdd(m_Handle, 0x283); iRet = canIdAdd(m_Handle, 0x282); iRet = canIdAdd(m_Handle, 0x583); iRet = canIdAdd(m_Handle, 0x582); iRet = canIdAdd(m_Handle, 0x603); iRet = canIdAdd(m_Handle, 0x602); // for CAN-Open Harmonica and Cello Mimro iRet = canIdAdd(m_Handle, 0x18a); iRet = canIdAdd(m_Handle, 0x28a); iRet = canIdAdd(m_Handle, 0x194); iRet = canIdAdd(m_Handle, 0x294); iRet = canIdAdd(m_Handle, 0x19e); iRet = canIdAdd(m_Handle, 0x29e); // for RCS5000 arm iRet = canIdAdd(m_Handle, 0x111); iRet = canIdAdd(m_Handle, 0x112); iRet = canIdAdd(m_Handle, 0x113); iRet = canIdAdd(m_Handle, 0x114); iRet = canIdAdd(m_Handle, 0x115); iRet = canIdAdd(m_Handle, 0x116); iRet = canIdAdd(m_Handle, 0x788); iRet = canIdAdd(m_Handle, 0x789); iRet = canIdAdd(m_Handle, 0x78A); iRet = canIdAdd(m_Handle, 0x78B); iRet = canIdAdd(m_Handle, 0x78C); iRet = canIdAdd(m_Handle, 0x78D); iRet = canIdAdd(m_Handle, 0x78E); iRet = canIdAdd(m_Handle, 0x78F); iRet = canIdAddGroup(m_Handle, 0x00); iRet = canIdAddGroup(m_Handle, 0x08); iRet = canIdAddGroup(m_Handle, 0x10); iRet = canIdAddGroup(m_Handle, 0x18); iRet = canIdAddGroup(m_Handle, 0x20); iRet = canIdAddGroup(m_Handle, 0x28); iRet = canIdAddGroup(m_Handle, 0x30); iRet = canIdAddGroup(m_Handle, 0x38); iRet = canIdAddGroup(m_Handle, 0x40); iRet = canIdAddGroup(m_Handle, 0x48); iRet = canIdAddGroup(m_Handle, 0x50); iRet = canIdAddGroup(m_Handle, 0x58); iRet = canIdAddGroup(m_Handle, 0x60); iRet = canIdAddGroup(m_Handle, 0x68); iRet = canIdAddGroup(m_Handle, 0x98); // for Mimro drive ctrl iRet = canIdAdd(m_Handle, 0x123); */ /* for(int i=0; i<2047; i++) { if (iRet != NTCAN_SUCCESS) { TRACE("canIdAdd: %x Error \n", i); } } */ sleep(0.3); m_LastID = -1; //m_Mutex.unlock(); }
int CESDDevice::init(const char* acInitString) { InitializeCriticalSection(&m_csDevice); int i, iRetVal = 0; int txTimeOut = 0; char* pcToken; char acString[128]; if(m_bInitFlag) { warning("device already initialized"); m_iErrorState = ERRID_DEV_ISINITIALIZED; return m_iErrorState; } m_iDeviceId = -1; m_iErrorState = 0; strncpy(m_acInitString,acInitString,128); strncpy(acString,acInitString,128); pcToken = strtok( acString, ":" ); if( !pcToken ) { m_iErrorState = ERRID_DEV_BADINITSTRING; return m_iErrorState; } if( strcmp( pcToken, "ESD" ) != 0 ) { m_iErrorState = ERRID_DEV_BADINITSTRING; return m_iErrorState; } pcToken = strtok( NULL, "," ); if( !pcToken ) { m_iErrorState = ERRID_DEV_BADINITSTRING; return m_iErrorState; } m_iDeviceId = atoi(pcToken); pcToken = strtok( NULL, "," ); if( !pcToken ) { m_iErrorState = ERRID_DEV_BADINITSTRING; return m_iErrorState; } m_iBaudRate = atoi(pcToken); #if defined(__LINUX__) m_uiTimeOut = 6; #endif #if defined (_WIN32) switch( m_iBaudRate ) { case 125: case 250: m_uiTimeOut = 4; break; case 500: m_uiTimeOut = 3; break; case 1000: m_uiTimeOut = 2; break; default: m_uiTimeOut = 10; break; } #endif try { iRetVal = canOpen( m_iDeviceId, // Net 0, // Mode m_uiQueueSize, // TX Queue m_uiQueueSize, // RX Queue m_uiTimeOut, // Tx Timeout m_uiTimeOut, // Rx Timeout &m_hDevice); if(iRetVal != NTCAN_SUCCESS) { warning("can open failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } iRetVal = canOpen( m_iDeviceId, // Net 0, // Mode 1, // TX Queue 1, // RX Queue 600, // Tx Timeout 100, // Rx Timeout &m_hSyncDevice); if(iRetVal != NTCAN_SUCCESS) { warning("can open failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } } catch(...) { warning("init ESD device failed no library found"); m_iErrorState = ERRID_DEV_NOLIBRARY; return m_iErrorState; } for(i = 0; i <= m_iModuleCountMax; i++) { iRetVal = canIdAdd(m_hDevice, (MSGID_ACK + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } iRetVal = canIdAdd(m_hDevice, (MSGID_STATE + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } } for(i = 0; i <= MAX_MP55; i++ ) { iRetVal = canIdAdd(m_hDevice, (MSGID_MP55_RECV + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } iRetVal = canIdAdd(m_hDevice, (0x180 + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } iRetVal = canIdAdd(m_hDevice, (0x600 + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } } for(i = 0; i < MAX_SCHUNK; i++ ) { iRetVal = canIdAdd(m_hDevice, (MSGID_SCHUNK_RECV + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } } iRetVal = canIdAdd(m_hSyncDevice, MSGID_ALL); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } m_iErrorState = setBaudRate(); if(m_iErrorState != 0) return m_iErrorState; m_iErrorState = clearReadQueue(); if(m_iErrorState != 0) return m_iErrorState; if(m_iErrorState == 0) m_bInitFlag = true; updateModuleIdMap(); return m_iErrorState; }
int CESDDevice::reinit(unsigned char ucBaudRateId) { int i, iRetVal = 0; m_iErrorState = 0; if(!m_bInitFlag) { warning("device not initialized"); m_iErrorState = ERRID_DEV_NOTINITIALIZED; return m_iErrorState; } switch(ucBaudRateId) { case BAUDRATEID_MOD_CAN_125K: m_iBaudRate = 125; break; case BAUDRATEID_MOD_CAN_250K: m_iBaudRate = 250; break; case BAUDRATEID_MOD_CAN_500K: m_iBaudRate = 500; break; case BAUDRATEID_MOD_CAN_1000K: m_iBaudRate = 1000; break; } iRetVal = canClose(m_hDevice); if(iRetVal != NTCAN_SUCCESS) { warning("can close failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_EXITERROR; } iRetVal = canClose(m_hSyncDevice); if(iRetVal != NTCAN_SUCCESS) { warning("can close failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_EXITERROR; } m_bInitFlag = false; iRetVal = canOpen( m_iDeviceId, // Net 0, // Mode m_uiQueueSize, // TX Queue m_uiQueueSize, // RX Queue 20*m_uiTimeOut, // Tx Timeout m_uiTimeOut, // Rx Timeout &m_hDevice); if(iRetVal != NTCAN_SUCCESS) { warning("can open failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } /*iRetVal = canOpen( m_iDeviceId, // Net 0, // Mode 1, // TX Queue 1, // RX Queue 600, // Tx Timeout 100, // Rx Timeout &m_hSyncDevice); if(iRetVal != NTCAN_SUCCESS) { warning("can open failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; }*/ m_iErrorState = setBaudRate(); if(m_iErrorState != 0) return m_iErrorState; for(i = 0; i <= m_iModuleCountMax; i++) { iRetVal = canIdAdd(m_hDevice, (MSGID_ACK + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } iRetVal = canIdAdd(m_hDevice, (MSGID_STATE + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } } for(i = 0; i < MAX_MP55; i++ ) { iRetVal = canIdAdd(m_hDevice, (MSGID_MP55_RECV + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } iRetVal = canIdAdd(m_hDevice, (0x180 + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } } for(i = 0; i < MAX_SCHUNK; i++ ) { iRetVal = canIdAdd(m_hDevice, (MSGID_SCHUNK_RECV + i)); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } } iRetVal = canIdAdd(m_hSyncDevice, MSGID_ALL); if(iRetVal != NTCAN_SUCCESS) { warning("can add ID failed Errorcode: %d", iRetVal); getDeviceError(iRetVal); m_iErrorState = ERRID_DEV_INITERROR; return m_iErrorState; } m_iErrorState = clearReadQueue(); if(m_iErrorState != 0) return m_iErrorState; if(m_iErrorState == 0) m_bInitFlag = true; updateModuleIdMap(); return m_iErrorState; }