// read from CAN forever - until manual break int read_loop(bool display_on) { // read in endless loop until Ctrl-C while (1) { TPCANRdMsg m; __u32 status; if (LINUX_CAN_Read(h, &m)) { perror("receivetest: LINUX_CAN_Read()"); return errno; } if (display_on) print_message_ex(&m); // check if a CAN status is pending if (m.Msg.MSGTYPE & MSGTYPE_STATUS) { status = CAN_Status(h); if ((int)status < 0) { errno = nGetLastError(); perror("receivetest: CAN_Status()"); return errno; } printf("receivetest: pending CAN status 0x%04x read.\n", (__u16)status); } } return 0; }
//------------------------------------------- bool CanPeakSys::transmitMsg(CanMsg CMsg, bool bBlocking) { TPCANMsg TPCMsg; bool bRet = true; if (m_bInitialized == false) return false; // copy CMsg to TPCmsg TPCMsg.LEN = CMsg.m_iLen; TPCMsg.ID = CMsg.m_iID; TPCMsg.MSGTYPE = CMsg.m_iType; for(int i=0; i<8; i++) TPCMsg.DATA[i] = CMsg.getAt(i); // write msg int iRet; iRet = CAN_Write(m_handle, &TPCMsg); iRet = CAN_Status(m_handle); if(iRet < 0) { std::cout << "CanPeakSys::transmitMsg, errorcode= " << nGetLastError() << std::endl; bRet = false; } return bRet; }
//------------------------------------------- bool CANPeakSys2PCI::receiveMsg(CanMsg* pCMsg) { TPCANRdMsg TPCMsg; TPCMsg.Msg.LEN = 8; TPCMsg.Msg.MSGTYPE = 0; TPCMsg.Msg.ID = 0; int iRet = CAN_ERR_OK; bool bRet = false; if (m_bInitialized == false) return false; iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0); if (iRet == CAN_ERR_OK) { pCMsg->m_iID = TPCMsg.Msg.ID; pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3], TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]); bRet = true; } else if (CAN_Status(m_handle) != CAN_ERR_QRCVEMPTY) { // Error (Don't output error of empty queue) LOGERROR( "receiveMsg() : errorcode= " << nGetLastError() ); pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0); } return bRet; }
// read from CAN forever - until manual break int read_loop() { // read in endless loop until Ctrl-C while (1) { TPCANMsg m; __u32 status; if ((errno = CAN_Read(h, &m))) { perror("receivetest: CAN_Read()"); return errno; } else { print_message(&m); // check if a CAN status is pending if (m.MSGTYPE & MSGTYPE_STATUS) { status = CAN_Status(h); if ((int)status < 0) { errno = nGetLastError(); perror("receivetest: CAN_Status()"); return errno; } else printf("receivetest: pending CAN status 0x%04x read.\n", (__u16)status); } } } return 0; }
//------------------------------------------- bool CANPeakSys2PCI::transmitMsg(CanMsg& CMsg) { TPCANMsg TPCMsg; bool bRet = true; if (m_bInitialized == false) return false; // copy CMsg to TPCmsg TPCMsg.LEN = CMsg.m_iLen; TPCMsg.ID = CMsg.m_iID; TPCMsg.MSGTYPE = CMsg.m_iType; for(int i=0; i<8; i++) TPCMsg.DATA[i] = CMsg.getAt(i); // write msg int iRet; iRet = CAN_Write(m_handle, &TPCMsg); iRet = CAN_Status(m_handle); if(iRet < 0) { LOGERROR( "transmitMsg() : errorcode= " << nGetLastError() ); bRet = false; } return bRet; }
//------------------------------------------- bool CANPeakSysDongle::transmitMsg(CanMsg& CMsg) { bool bRet; int i, iRet; TPCANMsg TPCMsg; if(!m_bInitialized) { return false; } // copy CMsg to TPCmsg TPCMsg.LEN = CMsg.m_iLen; TPCMsg.ID = CMsg.m_iID; TPCMsg.MSGTYPE = CMsg.m_iType; for(i = 0; i < 8; i++) { TPCMsg.DATA[i] = CMsg.getAt(i); } // write msg bRet = true; // std::cout<<"sending command: "<<std::endl; CAN_Write(m_handle, &TPCMsg); iRet = CAN_Status(m_handle); if(iRet < 0) { std::cout<<"transmitMsg() : errorcode= " << nGetLastError()<<std::endl; bRet = false; } return bRet; }
//------------------------------------------- bool ros_for_can::receiveMsgTimeout(CanMsg* pCMsg, int nSecTimeout) { int iRet = CAN_ERR_OK; TPCANRdMsg TPCMsg; TPCMsg.Msg.LEN = 8; TPCMsg.Msg.MSGTYPE = 0; TPCMsg.Msg.ID = 0; if (m_bInitialized == false) return false; bool bRet = true; iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, nSecTimeout); // eval return value if(iRet != CAN_ERR_OK) { std::cout << "ros_for_can::receiveMsgTimeout, errorcode= " << nGetLastError() << std::endl; pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0); bRet = false; } else { pCMsg->setID(TPCMsg.Msg.ID); pCMsg->setLength(TPCMsg.Msg.LEN); pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3], TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]); } return bRet; }
//------------------------------------------- bool CANPeakSysDongle::receiveMsg(CanMsg* pCMsg) { TPCANRdMsg TPCMsg; TPCMsg.Msg.LEN = 8; TPCMsg.Msg.MSGTYPE = 0; TPCMsg.Msg.ID = 0; int iRet = CAN_ERR_OK; bool bRet = false; if(!m_bInitialized) { return false; } iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0); if (iRet == CAN_ERR_OK) { pCMsg->m_iID = TPCMsg.Msg.ID; pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3], TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]); bRet = true; } else if(TPCMsg.Msg.MSGTYPE & MSGTYPE_STATUS) { iRet = CAN_Status(m_handle); if (iRet < 0) { errno = nGetLastError(); perror("receivetest: CAN_Status()"); return false; } else { printf("receivetest: pending CAN status 0x%04x read.\n", (__u16)iRet); } pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0); if(iRet != CAN_ERR_QRCVEMPTY) { // Error (Don't output error of empty queue) std::cout<< "receiveMsg() : errorcode= " << nGetLastError()<<std::endl; pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0); } } return bRet; }
//------------------------------------------- bool CANPeakSys2PCI::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry) { int i, iRet; TPCANRdMsg TPCMsg; TPCMsg.Msg.LEN = 8; TPCMsg.Msg.MSGTYPE = 0; TPCMsg.Msg.ID = 0; if (m_bInitialized == false) return false; // wait until msg in buffer bool bRet = true; iRet = CAN_ERR_OK; i=0; do { iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0); if(iRet == CAN_ERR_OK) { break; } i++; Sleep(100); } while(i < iNrOfRetry); // eval return value if(iRet != CAN_ERR_OK) { LOGERROR("receiveMsgRetry() : errorcode= " << nGetLastError() ); pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0); bRet = false; } else { pCMsg->m_iID = TPCMsg.Msg.ID; pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3], TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]); } return bRet; }
//------------------------------------------- bool ros_for_can::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry) { int i, iRet; TPCANRdMsg TPCMsg; TPCMsg.Msg.LEN = 8; TPCMsg.Msg.MSGTYPE = 0; TPCMsg.Msg.ID = 0; if (m_bInitialized == false) return false; // wait until msg in buffer bool bRet = true; iRet = CAN_ERR_OK; i=0; do { iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0); if(iRet == CAN_ERR_OK) break; i++; usleep(10000); } while(i < iNrOfRetry); // eval return value if(iRet != CAN_ERR_OK) { std::cout << "ros_for_can::receiveMsgRetry, errorcode= " << nGetLastError() << std::endl; pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0); bRet = false; } else { pCMsg->setID(TPCMsg.Msg.ID); pCMsg->setLength(TPCMsg.Msg.LEN); pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3], TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]); } return bRet; }
/**************************************************************************** * Name: arm_caninit(void) ****************************************************************************/ int arm_caninit(){ /* Initialization of the CAN hardware is performed by logic external to * this test. */ const char *szDeviceNode = DEFAULT_NODE; errno = 0; /* Open the CAN device for reading */ // open the CAN port // please use what is appropriate // HW_DONGLE_SJA // HW_DONGLE_SJA_EPP // HW_ISA_SJA // HW_PCI h = LINUX_CAN_Open(szDeviceNode, O_RDWR); if (!h) { errno = nGetLastError(); perror("bitratetest: CAN_Open()"); return 0; } /*set the bitrate*/ errno = CAN_Init(h,CAN_BAUD_1M,CAN_INIT_TYPE_ST); if(errno) { perror("set can speed error"); CAN_Close(h); return 0; } /* Now loop the appropriate number of times, performing one loopback test * on each pass. */ printf("can init is ok!\n"); //return 1 is ok; return 1; }
// here all is done int main(int argc, char *argv[]) { char *ptr; int i; const char *szDeviceNode = DEFAULT_NODE; errno = 0; current_release = CURRENT_RELEASE; disclaimer("bitratetest"); // decode command line arguments for (i = 1; i < argc; i++) { char c; ptr = argv[i]; while (*ptr == '-') ptr++; c = *ptr; ptr++; if (*ptr == '=') ptr++; switch(tolower(c)) { case 'f': szDeviceNode = ptr; break; case '?': case 'h': hlpMsg(); my_private_exit(0); break; default: errno = EINVAL; perror("bitratetest: unknown command line argument"); my_private_exit(errno); break; } } // open the CAN port // please use what is appropriate // HW_DONGLE_SJA // HW_DONGLE_SJA_EPP // HW_ISA_SJA // HW_PCI h = LINUX_CAN_Open(szDeviceNode, O_RDWR); if (h) { char txt[VERSIONSTRING_LEN]; // get version info errno = CAN_VersionInfo(h, txt); if (!errno) printf("bitratetest: driver version = %s\n", txt); else { perror("bitratetest: CAN_VersionInfo()"); my_private_exit(errno); } printf("\n"); // calculate BTR0BTR1 from bitrates for (i = 1000000; i > 2500; i /= 2) { if (i == 62500) i = 100000; if (i == 6250) i = 10000; printf("bitratetest: %d bits/sec \t->BTR0BTR1=0x%04x\n", i + 1, LINUX_CAN_BTR0BTR1(h, i + 1)); printf(" %d bits/sec \t->BTR0BTR1=0x%04x\n", i , LINUX_CAN_BTR0BTR1(h, i)); printf(" %d bits/sec \t->BTR0BTR1=0x%04x\n", i - 1, LINUX_CAN_BTR0BTR1(h, i - 1)); }; printf("\n"); } else { errno = nGetLastError(); perror("bitratetest: CAN_Open()"); } my_private_exit(errno); return errno; }
int PcanPci::getError() { return nGetLastError(); }