int32_t readNTCAN(NTCAN_HANDLE handle, CMSG *msg, int32_t len) { NTCAN_RESULT result; int32_t i,j; int32_t timeout=0; /* Reading Object of NTCAN device */ do { result = canRead(handle,msg, &len, NULL); timeout++; /* If timeout error is recieved repeatly then read is aborted */ if(timeout > MAX_TIMEOUT) { result = canIoctl(handle, NTCAN_IOCTL_ABORT_RX, NULL); if(errorCheck(CAN_IO_CTL,result != 0)) { return 1; } // error check printf("Repeated Timeout, read aborted\n"); return 2; } } while(errorCheck(CAN_READ,result) == 2); if(errorCheck(CAN_READ,result) != 0) { return 1; } // error check /* Printing read object of NTCAN device to screen */ printf("readNTCAN() successfull\n") ; printf("Length of message recieved: %d\n", (len & 0x0F) ); for(j=0;j<len;j++) { printf("ID of NTCAN device: %x\n", msg->id); for(i=0;i<(msg->len);i++) { printf("Byte %d of recieved message: %d\n", i, msg->data[i]); } } return 0; }
int32_t closeNTCAN(NTCAN_HANDLE handle, int32_t ID[]) { NTCAN_RESULT result; int32_t timeout = 0; int32_t i=0; /* Flushing system */ result = canIoctl(handle, NTCAN_IOCTL_FLUSH_RX_FIFO, NULL); if(errorCheck(CAN_IO_CTL,result) != 0) // error check { return 1; } printf("System Flushed\n"); /* Deleting ID */ for(i=1; i<ID[0]; i++) { // Deletes all ids and retrys if timeout error do { result = canIdDelete(handle, ID[i]); timeout++; if(timeout>MAX_TIMEOUT) { // checks for max timeout printf("Max timeout reached, aborting\n"); return 2; } } while( errorCheck(CAN_ID_DELETE,result) == 2); if(errorCheck(CAN_ID_DELETE,result) != 0) // error check { return 1; } printf("Deleted NTCAN ID\n"); } /* Closing NTCAN device */ result = canClose(handle); // closes device if(errorCheck(CAN_CLOSE,result) != 0) // error check { return 1; } printf("Deleted NTCAN device. Device successfully closed.\n"); return 0; }
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 }
//----------------------------------------------- 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(); }