39 if(((
unsigned int)iErrorState & 0xFFFF) & CAN_ERR_QRCVEMPTY)
41 warning(
"receive queue is empty");
44 if(iErrorState & CAN_ERR_OVERRUN)
46 warning(
"receive buffer overrun");
49 if(iErrorState & CAN_ERR_XMTFULL)
51 warning(
"transmit buffer full");
54 if(iErrorState & CAN_ERR_BUSOFF)
59 if(iErrorState & CAN_ERR_ILLPARAMTYPE )
61 warning(
"CAN_ERR_ILLPARAMTYPE");
83 if(((
unsigned int)iErrorState & 0xFFFF) & CAN_ERR_QXMTFULL)
88 if(iErrorState & CAN_ERR_BUSLIGHT)
93 if(((
unsigned int)iErrorState & 0xFFFF) & CAN_ERR_BUSHEAVY)
98 if(((
unsigned int)iErrorState & 0xFFFF) & CAN_ERR_RESOURCE)
100 warning(
"can't create resource");
122 debug(0,
"entering CPCanDevice::setBaudRate()...\n");
123 warning(
"PCan Device must be reset to set the new baud rate!\n");
154 if(iRetVal != CAN_ERR_OK)
174 iRetVal = CAN_MsgFilter(
m_handle, uiMessageId ,uiMessageId,MSGTYPE_STANDARD);
175 if(iRetVal != CAN_ERR_OK)
177 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
191 debug(0,
"entering CPCanDevice::clearReadQueue()...\n");
193 TPCMsg.Msg.MSGTYPE = 0;
200 debug(0,
"Trying to read messages ...");
202 debug(0,
" 0x%04x\n",iRetVal);
204 }
while( iRetVal != CAN_ERR_QRCVEMPTY ) ;
219 warning(
"reinit:device not initialized");
275 if(iRetVal != CAN_ERR_OK)
277 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
284 if(iRetVal != CAN_ERR_OK)
286 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
294 if(iRetVal != CAN_ERR_OK)
296 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
304 if(iRetVal != CAN_ERR_OK)
306 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
314 if(iRetVal != CAN_ERR_OK)
316 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
324 if(iRetVal != CAN_ERR_OK)
326 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
354 TPCMsg.Msg.MSGTYPE = 0;
363 if (iRetVal == CAN_ERR_OK)
370 debug(2,
"Read error (%s), attempt %d of %d",strerror(nGetLastError()),no,
m_iNoOfRetries+1);
373 if (iRetVal == CAN_ERR_OK)
384 warning(
"CAN read failed Errorcode: 0x%04x", iRetVal);
397 TPCMsg.MSGTYPE = MSGTYPE_STANDARD;
404 TPCMsg.MSGTYPE = MSGTYPE_RTR;
406 iRetVal = CAN_Write(
m_handle, &TPCMsg);
407 if(iRetVal != CAN_ERR_OK)
409 warning(
"can send failed Errorcode:0x%04x", iRetVal);
416 warning(
"Last Error reported: %s",strerror(nGetLastError()));
438 error(-1,
"Sorry constructor is not implemented");
456 error(-1,
"Sorry operator= is not implemented");
497 return init(CAN_BAUD_250K);
501 int iRetVal = CAN_ERR_OK;
502 printf(
"Initializing pcan device ...\n");
509 printf(
"Error: Cannot open CAN on USB (%s): %s\n",
m_DeviceName, strerror(errno));
514 iRetVal = CAN_Init(
m_handle, baudRate, CAN_INIT_TYPE_ST);
516 if(iRetVal != CAN_ERR_OK)
518 printf(
"PcanDevice: error in init" );
522 printf(
"PcanDevice, init ok\n" );
542 printf(
"CPCanDevice::init: DebugLevel: %d\n",deb);
543 printf(
"writing debug output to file debug.txt!\n");
545 debug(0,
"entering CPCanDevice::init(const char* acInitString) ...\n");
548 warning(
"device already initialized");
555 strncpy(acString,acInitString,128);
556 pcToken = strtok( acString,
":" );
561 if( strcmp( pcToken,
"PCAN" ) != 0 )
565 pcToken = strtok( NULL,
"," );
615 pcToken = strtok( NULL,
"," );
629 printf(
"Error: Cannot open CAN on USB (%s): %s\n",
m_DeviceName, strerror(errno));
634 printf(
"PCanDevice successfully opened on %s\n",
m_DeviceName);
639 warning(
"open PCAN device failed no library found");
646 if(iRetVal != CAN_ERR_OK)
648 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
655 if(iRetVal != CAN_ERR_OK)
657 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
665 if(iRetVal != CAN_ERR_OK)
667 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
675 if(iRetVal != CAN_ERR_OK)
677 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
685 if(iRetVal != CAN_ERR_OK)
687 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
695 if(iRetVal != CAN_ERR_OK)
697 warning(
"Can_MsgFilter failed Errorcode: %d", iRetVal);
715 debug(0,
"PCanDevice:init successfull!\n");
733 iRetVal = LINUX_CAN_Statistics(
m_handle,&Diag);
734 debug(0,
"PCanDevice: exit():");
735 debug(0,
"--------------STATISTICS-------------------");
736 debug(0,
"Total number of reads: %d",Diag.dwReadCounter);
737 debug(0,
"Total number of writes: %d",Diag.dwWriteCounter);
738 debug(0,
"Total number of interrupts: %d",Diag.dwIRQcounter);
739 debug(0,
"Total number of errors: %d",Diag.dwErrorCounter);
740 debug(0,
"Error flag: 0x%04x",Diag.wErrorFlag);
744 warning(
"exit:device not initialized");
749 iRetVal = CAN_ERR_OK;
751 if(iRetVal != CAN_ERR_OK)
753 warning(
"can close failed Errorcode: %d", iRetVal);
766 bool bRecieved =
false;
769 TPCMsg.Msg.MSGTYPE = 0;
781 if(iRetVal != CAN_ERR_OK)
783 warning(
"can read failed Errorcode: 0x%04x", iRetVal);
790 debug(1,
"received CAN-ID %x, expected %x", TPCMsg.Msg.ID,
MSGID_ALL);
795 debug(1,
"wrong command ID");
int setMessageId(unsigned long uiMessageId)
#define ERRID_DEV_BADINITSTRING
#define MSGID_SCHUNK_RECV
unsigned char m_aucMessageData[8]
int initMessage(const char *pcClassName, int iDebuglevel=0, bool bDebug=true, bool bDebugFile=false)
int reinit(unsigned char ucBaudRateId)
CRITICAL_SECTION m_csDevice
unsigned short m_uiQueueSize
int readDevice(CProtocolMessage &rclProtocolMessage)
#define ERRID_DEV_ISINITIALIZED
void error(const int iErrorCode, const char *pcErrorMessage,...) const
#define ERRID_DEV_WRITEERROR
virtual ~CPCanDevice()
destructor
int getDebugLevel() const
#define ERRID_DEV_NOTINITIALIZED
unsigned long m_uiTimeOut
unsigned char m_ucMessageLength
int printMessage(CProtocolMessage &rclProtocolMessage, bool read)
unsigned long m_uiMessageId
void warning(const char *pcWarningMessage,...) const
#define ERRID_DEV_INITERROR
void setQueueSize(unsigned short uiQueueSize)
#define ERRID_DEV_EXITERROR
void debug(const int iDebugLevel, const char *pcDebugMessage,...) const
int waitForStartMotionAll()
#define ERRID_DEV_NOLIBRARY
int getDeviceError(int iErrorState)
CPCanDevice & operator=(const CPCanDevice &rclPCanDevice)
#define ERRID_DEV_READERROR
CPCanDevice()
default constructor
int writeDevice(CProtocolMessage &rclProtocolMessage)
void setTimeOut(unsigned long uiTimeOut)
unsigned long m_uiBaudRate