25 #include <sys/types.h> 67 std::cout <<
"Cannot open CAN on USB: " << strerror(errno) << std::endl;
81 std::string sCanDevice;
84 sCanDevice =
"/dev/pcan32";
85 }
else std::cout <<
"CAN-device path read from ini-File: " << sCanDevice << std::endl;
88 m_handle = LINUX_CAN_Open(sCanDevice.c_str(), O_RDWR);
93 std::cout <<
"Cannot open CAN on USB: " << strerror(errno) << std::endl;
114 TPCMsg.ID = CMsg.
getID();
115 TPCMsg.MSGTYPE = CMsg.
getType();
116 for(
int i=0; i<8; i++)
117 TPCMsg.DATA[i] = CMsg.
getAt(i);
123 iRet = LINUX_CAN_Write_Timeout(
m_handle, &TPCMsg, 25);
125 if(iRet != CAN_ERR_OK) {
127 std::cout <<
"CANPeakSysUSB::transmitMsg An error occured while sending..." << iRet << std::endl;
139 std::cout <<
"CANPeakSysUSB::transmitMsg, system error: " << iRet << std::endl;
141 }
else if((iRet & CAN_ERR_BUSOFF) != 0) {
142 std::cout <<
"CANPeakSysUSB::transmitMsg, BUSOFF detected" << std::endl;
144 std::cout <<
"Trying to re-init Hardware..." << std::endl;
147 }
else if((iRet & CAN_ERR_ANYBUSERR) != 0) {
148 std::cout <<
"CANPeakSysUSB::transmitMsg, ANYBUSERR" << std::endl;
150 }
else if( (iRet & (~CAN_ERR_QRCVEMPTY)) != 0) {
151 std::cout <<
"CANPeakSysUSB::transmitMsg, CAN_STATUS: " << iRet << std::endl;
164 TPCMsg.Msg.MSGTYPE = 0;
167 int iRet = CAN_ERR_OK;
173 iRet = LINUX_CAN_Read_Timeout(
m_handle, &TPCMsg, 0);
175 if (iRet == CAN_ERR_OK)
177 pCMsg->
setID(TPCMsg.Msg.ID);
179 pCMsg->
set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
180 TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
183 else if( (iRet & (~CAN_ERR_QRCVEMPTY)) != 0)
185 std::cout <<
"CANPeakSysUSB::receiveMsg, CAN_STATUS: " << iRet << std::endl;
186 pCMsg->
set(0, 0, 0, 0, 0, 0, 0, 0);
190 if( TPCMsg.Msg.MSGTYPE == MSGTYPE_STATUS ) {
191 std::cout <<
"CANPeakSysUSB::receiveMsg, status message catched:\nData is (CAN_ERROR_...) " << TPCMsg.Msg.DATA[3] << std::endl;
192 pCMsg->
set(0, 0, 0, 0, 0, 0, 0, 0);
205 TPCMsg.Msg.MSGTYPE = 0;
216 iRet = LINUX_CAN_Read_Timeout(
m_handle, &TPCMsg, 0);
218 if(iRet == CAN_ERR_OK)
224 while(i < iNrOfRetry);
227 if(iRet != CAN_ERR_OK)
229 std::cout <<
"CANPeakSysUSB::receiveMsgRetry, errorcode= " << nGetLastError() << std::endl;
230 pCMsg->
set(0, 0, 0, 0, 0, 0, 0, 0);
235 pCMsg->
setID(TPCMsg.Msg.ID);
237 pCMsg->
set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
238 TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
247 int iRet = CAN_ERR_OK;
251 TPCMsg.Msg.MSGTYPE = 0;
258 iRet = LINUX_CAN_Read_Timeout(
m_handle, &TPCMsg, nMicroSecTimeout);
261 if(iRet != CAN_ERR_OK)
263 std::cout <<
"CANPeakSysUSB::receiveMsgTimeout, errorcode= " << nGetLastError() << std::endl;
264 pCMsg->
set(0, 0, 0, 0, 0, 0, 0, 0);
269 pCMsg->
setID(TPCMsg.Msg.ID);
271 pCMsg->
set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
272 TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
279 int ret = CAN_ERR_OK;
285 ret = CAN_Init(
m_handle, CAN_BAUD_1M, CAN_INIT_TYPE_ST);
288 ret = CAN_Init(
m_handle, CAN_BAUD_500K, CAN_INIT_TYPE_ST);
291 ret = CAN_Init(
m_handle, CAN_BAUD_250K, CAN_INIT_TYPE_ST);
294 ret = CAN_Init(
m_handle, CAN_BAUD_125K, CAN_INIT_TYPE_ST);
297 ret = CAN_Init(
m_handle, CAN_BAUD_50K, CAN_INIT_TYPE_ST);
300 ret = CAN_Init(
m_handle, CAN_BAUD_20K, CAN_INIT_TYPE_ST);
303 ret = CAN_Init(
m_handle, CAN_BAUD_10K, CAN_INIT_TYPE_ST);
309 std::cout <<
"CANPeakSysUSB::CANPeakSysUSB(), error in init" << std::endl;
315 std::cout <<
"CANPeakSysUSB::CanpeakSys(), init ok" << std::endl;
326 LINUX_CAN_Statistics(
m_handle, &diag);
328 std::cout <<
"*************************\n" 329 <<
"*** Detailed status output of CANPeakSys\n" 330 <<
"*************************" 331 <<
"\nIRQ-Level: " << diag.wIrqLevel
332 <<
"\nNo reads: " << diag.dwReadCounter
333 <<
"\nNo writes: " << diag.dwWriteCounter
334 <<
"\nNo interrupts: " << diag.dwIRQcounter
335 <<
"\nNo errors: " << diag.dwErrorCounter
336 <<
"\nError flag: " << diag.wErrorFlag
337 <<
"\nLast error: " << diag.nLastError
bool receiveMsg(CanMsg *pCMsg)
void outputDetailedStatus()
CANPeakSysUSB(const char *device, int baudrate)
int GetKeyInt(const char *pSect, const char *pKey, int *pValue, bool bWarnIfNotfound=true)
bool receiveMsgRetry(CanMsg *pCMsg, int iNrOfRetry)
int SetFileName(std::string fileName, std::string strIniFileUsedBy="", bool bCreate=false)
bool receiveMsgTimeout(CanMsg *pCMsg, int nMicroSeconds)
void set(BYTE Data0=0, BYTE Data1=0, BYTE Data2=0, BYTE Data3=0, BYTE Data4=0, BYTE Data5=0, BYTE Data6=0, BYTE Data7=0)
int GetKeyString(const char *pSect, const char *pKey, std::string *pStrToRead, bool bWarnIfNotfound=true)
bool transmitMsg(CanMsg CMsg, bool bBlocking=true)