PCanDevice.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "PCanDevice.h"
19 #include <errno.h>
20 // ========================================================================== ;
21 // ;
22 // ---- private auxiliary functions ----------------------------------------- ;
23 // ;
24 // ========================================================================== ;
25 
26 // ========================================================================== ;
27 // ;
28 // ---- protected auxiliary functions --------------------------------------- ;
29 // ;
30 // ========================================================================== ;
31 
32 //possible incomplete
33 //
34 
35 int CPCanDevice::getDeviceError(int iErrorState)
36 {
38 
39  if(((unsigned int)iErrorState & 0xFFFF) & CAN_ERR_QRCVEMPTY)
40  {
41  warning("receive queue is empty");
42  error= ERRID_DEV_WRITEERROR;
43  }
44  if(iErrorState & CAN_ERR_OVERRUN)
45  {
46  warning("receive buffer overrun");
47  error= ERRID_DEV_READERROR;
48  }
49  if(iErrorState & CAN_ERR_XMTFULL)
50  {
51  warning("transmit buffer full");
52  error = ERRID_DEV_WRITEERROR;
53  }
54  if(iErrorState & CAN_ERR_BUSOFF)
55  {
56  warning("CAN_ERR_OFF_BUS");
57  error = ERRID_DEV_READERROR;
58  }
59  if(iErrorState & CAN_ERR_ILLPARAMTYPE )
60  {
61  warning("CAN_ERR_ILLPARAMTYPE");
62  error = ERRID_DEV_READERROR;
63  }
64  /*
65  * no corresponding errors in pcan.h found
66  *
67  else if(iErrorState == NTCAN_MESSAGE_LOST)
68  {
69  warning("NTCAN_MESSAGE_LOST");
70  return ERRID_DEV_READERROR;
71  }
72  else if(iErrorState == NTCAN_RX_TIMEOUT)
73  {
74  warning("NTCAN_RX_TIMEOUT");
75  return ERRID_DEV_READTIMEOUT;
76  }
77  else if(iErrorState == NTCAN_TX_TIMEOUT)
78  {
79  warning("NTCAN_TX_TIMEOUT");
80  return ERRID_DEV_WRITETIMEOUT;
81  }
82  */
83  if(((unsigned int)iErrorState & 0xFFFF) & CAN_ERR_QXMTFULL)
84  {
85  warning("transmit queue full");
86  error = ERRID_DEV_WRITEERROR;
87  }
88  if(iErrorState & CAN_ERR_BUSLIGHT)
89  {
90  warning("bus error");
91  error = ERRID_DEV_WRITEERROR;
92  }
93  if(((unsigned int)iErrorState & 0xFFFF) & CAN_ERR_BUSHEAVY)
94  {
95  warning("bus error");
96  error = ERRID_DEV_WRITEERROR;
97  }
98  if(((unsigned int)iErrorState & 0xFFFF) & CAN_ERR_RESOURCE)
99  {
100  warning("can't create resource");
101  error = ERRID_DEV_WRITEERROR;
102  }
103 
104  return error;
105 }
106 
107 //ok
108 int CPCanDevice::setBaudRate(unsigned char iBaudRate)
109 {
110  m_uiBaudRate = (unsigned long) iBaudRate;
111  return setBaudRate();
112 }
113 
114 //ok
116 {
117  /*
118  * baud rate must be set when initializing can device!
119  *
120  * */
121 
122  debug(0,"entering CPCanDevice::setBaudRate()...\n");
123  warning("PCan Device must be reset to set the new baud rate!\n");
124 
125  int iRetVal = 0;
126  m_iErrorState = 0;
127 
128  switch( m_iBaudRate )
129  {
130  case 125:
131  m_uiBaudRate=CAN_BAUD_125K; // 125k
132  break;
133  case 250:
134  m_uiBaudRate=CAN_BAUD_250K; // 250k
135  break;
136  case 500:
137  m_uiBaudRate=CAN_BAUD_500K; // 500k
138  break;
139  case 1000:
140  m_uiBaudRate=CAN_BAUD_1M; // 1000k
141  break;
142  default:
143  m_uiBaudRate=CAN_BAUD_250K; // 250k
144  break;
145  }
146 
147 
148  if (m_bInitFlag)
149  {
150  CAN_Close(m_handle);
151  }
152  iRetVal = init(m_uiBaudRate);
153  debug(0,"InitFlag set to %d\n",m_bInitFlag);
154  if(iRetVal != CAN_ERR_OK)
155  {
156  warning("can set baudrate 0x%x failed Errorcode: %d", m_uiBaudRate, iRetVal);
157  getDeviceError(iRetVal);
159  return m_iErrorState;
160  }
161  else
162  {
163  debug(0,"PCanDevice: setting baud rate to %d\n",m_iBaudRate);
164  }
165  return m_iErrorState;
166 }
167 
168 //ok
169 int CPCanDevice::setMessageId(unsigned long uiMessageId)
170 {
171  int iRetVal = 0;
172  m_iErrorState = 0;
173  //iRetVal = canIdAdd(m_hDevice, uiMessageId);
174  iRetVal = CAN_MsgFilter(m_handle, uiMessageId ,uiMessageId,MSGTYPE_STANDARD);
175  if(iRetVal != CAN_ERR_OK)
176  {
177  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
178  getDeviceError(iRetVal);
180  return m_iErrorState;
181  }
182  return m_iErrorState;
183 }
184 
185 //ok
187 {
188  int iRetVal = 0;
189  TPCANRdMsg TPCMsg;
190 
191  debug(0,"entering CPCanDevice::clearReadQueue()...\n");
192  TPCMsg.Msg.LEN = 8;
193  TPCMsg.Msg.MSGTYPE = 0;
194  TPCMsg.Msg.ID = 0;
195 
196  m_iErrorState = 0;
197  do
198  {
199  //iRetVal = canRead(m_hDevice, &clESDProtocolMessage, &iNumberOfMessages, NULL);
200  debug(0,"Trying to read messages ...");
201  iRetVal = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, m_uiTimeOut);
202  debug(0," 0x%04x\n",iRetVal);
203 
204  }while( iRetVal != CAN_ERR_QRCVEMPTY ) ;
205 
206  return m_iErrorState;
207 }
208 
209 
210 //ok
211 int CPCanDevice::reinit(unsigned char ucBaudRateId)
212 {
213 
214  int iRetVal = 0;
215 
216  m_iErrorState = 0;
217  if(!m_bInitFlag)
218  {
219  warning("reinit:device not initialized");
221  return m_iErrorState;
222  }
223  iRetVal = setBaudRate(ucBaudRateId);
224 
225  /* done in setBaudRate
226  switch(ucBaudRateId)
227  {
228  case BAUDRATEID_MOD_CAN_125K:
229  m_iBaudRate = 125;
230  break;
231  case BAUDRATEID_MOD_CAN_250K:
232  m_iBaudRate = 250;
233  break;
234  case BAUDRATEID_MOD_CAN_500K:
235  m_iBaudRate = 500;
236  break;
237  case BAUDRATEID_MOD_CAN_1000K:
238  m_iBaudRate = 1000;
239  break;
240  }
241  iRetVal = canClose(m_hDevice);
242  if(iRetVal != CAN_ERR_OK)
243  {
244  warning("can close failed Errorcode: %d", iRetVal);
245  getDeviceError(iRetVal);
246  m_iErrorState = ERRID_DEV_EXITERROR;
247  }
248  iRetVal = canClose(m_hSyncDevice);
249  if(iRetVal != CAN_ERR_OK)
250  {
251  warning("can close failed Errorcode: %d", iRetVal);
252  getDeviceError(iRetVal);
253  m_iErrorState = ERRID_DEV_EXITERROR;
254  }
255  m_bInitFlag = false;
256  iRetVal = canOpen(
257  m_iDeviceId, // Net
258  0, // Mode
259  m_uiQueueSize, // TX Queue
260  m_uiQueueSize, // RX Queue
261  20*m_uiTimeOut, // Tx Timeout
262  m_uiTimeOut, // Rx Timeout
263  &m_hDevice);
264  if(iRetVal != CAN_ERR_OK)
265  {
266  warning("can open failed Errorcode: %d", iRetVal);
267  getDeviceError(iRetVal);
268  m_iErrorState = ERRID_DEV_INITERROR;
269  return m_iErrorState;
270  }
271  */
272 
273  //iRetVal = canIdAdd(m_hDevice, (MSGID_ACK + i));
274  iRetVal = CAN_MsgFilter(m_handle, MSGID_ACK ,MSGID_ACK+m_iModuleCountMax,MSGTYPE_STANDARD);
275  if(iRetVal != CAN_ERR_OK)
276  {
277  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
278  getDeviceError(iRetVal);
280  return m_iErrorState;
281  }
282  //iRetVal = canIdAdd(m_hDevice, (MSGID_STATE + i));
283  iRetVal = CAN_MsgFilter(m_handle, MSGID_STATE ,MSGID_STATE+m_iModuleCountMax,MSGTYPE_STANDARD);
284  if(iRetVal != CAN_ERR_OK)
285  {
286  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
287  getDeviceError(iRetVal);
289  return m_iErrorState;
290  }
291 
292  //iRetVal = canIdAdd(m_hDevice, (MSGID_MP55_RECV + i));
293  iRetVal = CAN_MsgFilter(m_handle, MSGID_MP55_RECV ,MSGID_MP55_RECV+MAX_MP55,MSGTYPE_STANDARD);
294  if(iRetVal != CAN_ERR_OK)
295  {
296  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
297  getDeviceError(iRetVal);
299  return m_iErrorState;
300  }
301 
302  //iRetVal = canIdAdd(m_hDevice, (0x180 + i));
303  iRetVal = CAN_MsgFilter(m_handle, 0x180 ,0x180+MAX_MP55,MSGTYPE_STANDARD);
304  if(iRetVal != CAN_ERR_OK)
305  {
306  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
307  getDeviceError(iRetVal);
309  return m_iErrorState;
310  }
311 
312  //iRetVal = canIdAdd(m_hDevice, (MSGID_SCHUNK_RECV + i));
313  iRetVal = CAN_MsgFilter(m_handle, MSGID_SCHUNK_RECV ,MSGID_SCHUNK_RECV+MAX_SCHUNK,MSGTYPE_STANDARD);
314  if(iRetVal != CAN_ERR_OK)
315  {
316  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
317  getDeviceError(iRetVal);
319  return m_iErrorState;
320  }
321 
322  //iRetVal = canIdAdd(m_hSyncDevice, MSGID_ALL);
323  iRetVal = CAN_MsgFilter(m_handle, MSGID_ALL ,MSGID_ALL,MSGTYPE_STANDARD);
324  if(iRetVal != CAN_ERR_OK)
325  {
326  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
327  getDeviceError(iRetVal);
329  return m_iErrorState;
330  }
331 
333  if(m_iErrorState != 0)
334  return m_iErrorState;
335 
336  if(m_iErrorState == 0)
337  {
338  m_bInitFlag = true;
339  }
340 
341 
343  return m_iErrorState;
344 }
345 
346 
347 
349 {
350  int iRetVal = 0;
351 
352  TPCANRdMsg TPCMsg;
353  TPCMsg.Msg.LEN = 8;
354  TPCMsg.Msg.MSGTYPE = 0;
355  TPCMsg.Msg.ID = 0;
356 
357  m_iErrorState = 0;
358  int no = 0;
359  do
360  {
361  m_iErrorState = 0;
362  iRetVal = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, m_uiTimeOut);
363  if (iRetVal == CAN_ERR_OK)
364  break;
365  else
366  CAN_Status(m_handle);
367  no++;
368 
369  m_iErrorState = getDeviceError(iRetVal);
370  debug(2,"Read error (%s), attempt %d of %d",strerror(nGetLastError()),no,m_iNoOfRetries+1);
371  //sleep(100);
372  }while (no <= m_iNoOfRetries);
373  if (iRetVal == CAN_ERR_OK)
374  {
375  rclProtocolMessage.m_uiMessageId = TPCMsg.Msg.ID;
376  rclProtocolMessage.m_ucMessageLength = TPCMsg.Msg.LEN;
377  memcpy(rclProtocolMessage.m_aucMessageData, TPCMsg.Msg.DATA, rclProtocolMessage.m_ucMessageLength);
378  printMessage(rclProtocolMessage,READ);
379  }
380  else
381  {
382  //warning("Last Error reported: %s",strerror(nGetLastError()));
383  //m_iErrorState = getDeviceError(iRetVal);
384  warning("CAN read failed Errorcode: 0x%04x", iRetVal);
385 // return m_iErrorState;
386  }
387  return m_iErrorState;
388 }
389 
390 
391 //ok
393 {
394  int iRetVal = 0;
395  TPCANMsg TPCMsg;
396 
397  TPCMsg.MSGTYPE = MSGTYPE_STANDARD;
398  m_iErrorState = 0;
399  TPCMsg.ID = rclProtocolMessage.m_uiMessageId;
400  printMessage(rclProtocolMessage,WRITE);
401 
402  TPCMsg.LEN = rclProtocolMessage.m_ucMessageLength;
403  if(rclProtocolMessage.m_bRTRFlag)
404  TPCMsg.MSGTYPE = MSGTYPE_RTR;
405  memcpy(TPCMsg.DATA, rclProtocolMessage.m_aucMessageData, rclProtocolMessage.m_ucMessageLength);
406  iRetVal = CAN_Write(m_handle, &TPCMsg);
407  if(iRetVal != CAN_ERR_OK)
408  {
409  warning("can send failed Errorcode:0x%04x", iRetVal);
410  m_iErrorState = getDeviceError(iRetVal);
411  //return m_iErrorState;
412  }
413  iRetVal = CAN_Status(m_handle);
414  if (iRetVal < 0)
415  {
416  warning("Last Error reported: %s",strerror(nGetLastError()));
418  }
419 
420  return m_iErrorState;
421 }
422 
423 // ========================================================================== ;
424 // ;
425 // ---- constructors / destructor ------------------------------------------- ;
426 // ;
427 // ========================================================================== ;
428 
430 {
432  m_DeviceName = (char*) malloc(200 * sizeof(char));
433  memset(m_DeviceName,0,sizeof(m_DeviceName));
434 }
435 
437 {
438  error(-1, "Sorry constructor is not implemented");
439 }
440 
442 {
443  free(m_DeviceName);
444  if (m_bInitFlag)
445  this->exit();
446 }
447 
448 // ========================================================================== ;
449 // ;
450 // ---- operators ----------------------------------------------------------- ;
451 // ;
452 // ========================================================================== ;
453 
455 {
456  error(-1, "Sorry operator= is not implemented");
457  return *this;
458 }
459 
460 // ========================================================================== ;
461 // ;
462 // ---- query functions ----------------------------------------------------- ;
463 // ;
464 // ========================================================================== ;
465 
466 // ========================================================================== ;
467 // ;
468 // ---- modify functions ---------------------------------------------------- ;
469 // ;
470 // ========================================================================== ;
471 
472 void CPCanDevice::setQueueSize(unsigned short uiQueueSize)
473 {
474  m_uiQueueSize = uiQueueSize;
475 }
476 
477 void CPCanDevice::setTimeOut(unsigned long uiTimeOut)
478 {
479  m_uiTimeOut= uiTimeOut;
480 }
481 
482 // ========================================================================== ;
483 // ;
484 // ---- I/O functions ------------------------------------------------------- ;
485 // ;
486 // ========================================================================== ;
487 
488 // ========================================================================== ;
489 // ;
490 // ---- exec functions ------------------------------------------------------ ;
491 // ;
492 // ========================================================================== ;
493 
494 //ok
496 {
497  return init(CAN_BAUD_250K);
498 }
499 int CPCanDevice::init(unsigned long baudRate)
500 {
501  int iRetVal = CAN_ERR_OK;
502  printf("Initializing pcan device ...\n");
503  //m_handle = LINUX_CAN_Open("/dev/pcan32", O_RDWR);
504  m_handle = LINUX_CAN_Open(m_DeviceName,0);
505 
506  if (! m_handle)
507  {
508  // Fatal error
509  printf("Error: Cannot open CAN on USB (%s): %s\n", m_DeviceName, strerror(errno));
510  iRetVal = -1;
511  }
512  else
513  {
514  iRetVal = CAN_Init(m_handle, baudRate, CAN_INIT_TYPE_ST);
515  }
516  if(iRetVal != CAN_ERR_OK)
517  {
518  printf("PcanDevice: error in init" );
519  }
520  else
521  {
522  printf("PcanDevice, init ok\n" );
523  m_bInitFlag = true;
524  }
525  return iRetVal;
526 }
527 
528 /*
529  * Not needed for pcan
530  */
531 int CPCanDevice::init(const char* acInitString)
532 {
533  InitializeCriticalSection(&m_csDevice);
534  int iRetVal = 0;
535  m_uiTimeOut = 100;
536  m_iNoOfRetries = 10;
537  char* pcToken;
538  char acString[128];
539  int deb = getDebugLevel();
540  if (deb > 0)
541  {
542  printf("CPCanDevice::init: DebugLevel: %d\n",deb);
543  printf("writing debug output to file debug.txt!\n");
544  }
545  debug(0,"entering CPCanDevice::init(const char* acInitString) ...\n");
546  if(m_bInitFlag)
547  {
548  warning("device already initialized");
550  return m_iErrorState;
551  }
552  m_iDeviceId = -1;
553  m_iErrorState = 0;
554  strncpy(m_acInitString,acInitString,128);
555  strncpy(acString,acInitString,128);
556  pcToken = strtok( acString, ":" );
557  if( !pcToken )
559  return m_iErrorState;
560  }
561  if( strcmp( pcToken, "PCAN" ) != 0 )
563  return m_iErrorState;
564  }
565  pcToken = strtok( NULL, "," );
566  if( !pcToken )
568  return m_iErrorState;
569  }
570 
571 
572 
573 
574  m_iDeviceId = atoi(pcToken);
575  //std::cout << m_iDeviceId << std::endl;
576 
577 
578  strncpy(m_DeviceName,pcToken,12);
579 
580  /*
581 
582  if (m_iDeviceId == 0)
583  {
584  strcpy(m_DeviceName,"/dev/pcan32");
585  }
586  else if (m_iDeviceId == 1)
587  {
588  strcpy(m_DeviceName,"/dev/pcan33");
589  }
590  else if (m_iDeviceId == 14)
591  {
592  strcpy(m_DeviceName,"/dev/pcan0");
593  }
594  else if (m_iDeviceId == 15)
595  {
596  strcpy(m_DeviceName,"/dev/pcan1");
597  }
598  else if (m_iDeviceId == 16)
599  {
600  strcpy(m_DeviceName,"/dev/pcan2");
601  }
602  else if (m_iDeviceId == 17)
603  {
604  strcpy(m_DeviceName,"/dev/pcan3");
605  }
606  else
607  {
608  printf("Warning: currently only support for 2 devices!\n");
609  }
610  */
611 
612  //printf("Device %s: %s\n",pcToken,m_DeviceName);
613 
614 
615  pcToken = strtok( NULL, "," );
616  if( !pcToken )
618  return m_iErrorState;
619  }
620  m_iBaudRate = atoi(pcToken);
621 
622 
623  try
624  {
625  m_handle = LINUX_CAN_Open(m_DeviceName,0);
626  if (! m_handle)
627  {
628  // Fatal error
629  printf("Error: Cannot open CAN on USB (%s): %s\n", m_DeviceName, strerror(errno));
630  iRetVal = -1;
631  }
632  else
633  {
634  printf("PCanDevice successfully opened on %s\n",m_DeviceName);
635  }
636  }
637  catch(...)
638  {
639  warning("open PCAN device failed no library found");
641  return m_iErrorState;
642  }
643 
644 
645  iRetVal = CAN_MsgFilter(m_handle, MSGID_ACK ,MSGID_ACK+m_iModuleCountMax,MSGTYPE_STANDARD);
646  if(iRetVal != CAN_ERR_OK)
647  {
648  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
649  getDeviceError(iRetVal);
651  return m_iErrorState;
652  }
653  //iRetVal = canIdAdd(m_hDevice, (MSGID_STATE + i));
654  iRetVal = CAN_MsgFilter(m_handle, MSGID_STATE ,MSGID_STATE+m_iModuleCountMax,MSGTYPE_STANDARD);
655  if(iRetVal != CAN_ERR_OK)
656  {
657  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
658  getDeviceError(iRetVal);
660  return m_iErrorState;
661  }
662 
663  //iRetVal = canIdAdd(m_hDevice, (MSGID_MP55_RECV + i));
664  iRetVal = CAN_MsgFilter(m_handle, MSGID_MP55_RECV ,MSGID_MP55_RECV+MAX_MP55,MSGTYPE_STANDARD);
665  if(iRetVal != CAN_ERR_OK)
666  {
667  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
668  getDeviceError(iRetVal);
670  return m_iErrorState;
671  }
672 
673  //iRetVal = canIdAdd(m_hDevice, (0x180 + i));
674  iRetVal = CAN_MsgFilter(m_handle, 0x180 ,0x180+MAX_MP55,MSGTYPE_STANDARD);
675  if(iRetVal != CAN_ERR_OK)
676  {
677  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
678  getDeviceError(iRetVal);
680  return m_iErrorState;
681  }
682 
683  //iRetVal = canIdAdd(m_hDevice, (MSGID_SCHUNK_RECV + i));
684  iRetVal = CAN_MsgFilter(m_handle, MSGID_SCHUNK_RECV ,MSGID_SCHUNK_RECV+MAX_SCHUNK,MSGTYPE_STANDARD);
685  if(iRetVal != CAN_ERR_OK)
686  {
687  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
688  getDeviceError(iRetVal);
690  return m_iErrorState;
691  }
692 
693  //iRetVal = canIdAdd(m_hSyncDevice, MSGID_ALL);
694  iRetVal = CAN_MsgFilter(m_handle, MSGID_ALL ,MSGID_ALL,MSGTYPE_STANDARD);
695  if(iRetVal != CAN_ERR_OK)
696  {
697  warning("Can_MsgFilter failed Errorcode: %d", iRetVal);
698  getDeviceError(iRetVal);
700  return m_iErrorState;
701  }
702 
703 
705  if(m_iErrorState != 0)
706  return m_iErrorState;
707 
709  if(m_iErrorState != 0)
710  return m_iErrorState;
711 
712  if(m_iErrorState == 0)
713  {
714  m_bInitFlag = true;
715  debug(0,"PCanDevice:init successfull!\n");
716  }
718 
719  return m_iErrorState;
720 }
721 
722 
723 
724 //ok
726 {
727  int iRetVal = 0;
728  m_iErrorState = 0;
729 
730  //printf diagnosis
731  //i
732  TPDIAG Diag;
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);
741 
742  if(!m_bInitFlag)
743  {
744  warning("exit:device not initialized");
746  return m_iErrorState;
747  }
748  EnterCriticalSection(&m_csDevice);
749  iRetVal = CAN_ERR_OK;
750  iRetVal = CAN_Close(m_handle);
751  if(iRetVal != CAN_ERR_OK)
752  {
753  warning("can close failed Errorcode: %d", iRetVal);
754  getDeviceError(iRetVal);
756  }
757  m_bInitFlag = false;
758  LeaveCriticalSection(&m_csDevice);
759  DeleteCriticalSection(&m_csDevice);
760  return m_iErrorState;
761 }
762 
764 {
765  int iRetVal = 0;
766  bool bRecieved = false;
767  TPCANRdMsg TPCMsg;
768  TPCMsg.Msg.LEN = 8;
769  TPCMsg.Msg.MSGTYPE = 0;
770  TPCMsg.Msg.ID = 0;
771 
772  m_iErrorState = 0;
773  iRetVal = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, m_uiTimeOut);
774 
775  m_iErrorState = 0;
776 
777  do
778  {
779  //iRetVal = canRead(m_hSyncDevice, &clESDProtocolMessage, &iNumberOfMessages, NULL);
780  iRetVal = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, m_uiTimeOut);
781  if(iRetVal != CAN_ERR_OK)
782  {
783  warning("can read failed Errorcode: 0x%04x", iRetVal);
784  m_iErrorState = getDeviceError(iRetVal);
785  return m_iErrorState;
786  }
787  bRecieved = true;
788  if(TPCMsg.Msg.ID != MSGID_ALL)
789  {
790  debug(1, "received CAN-ID %x, expected %x", TPCMsg.Msg.ID, MSGID_ALL);
791  bRecieved = false;
792  }
793  if(TPCMsg.Msg.DATA[0] != CMDID_STARTMOVE)
794  {
795  debug(1, "wrong command ID");
796  bRecieved = false;
797  }
798  }
799  while(!bRecieved);
800  return m_iErrorState;
801 }
802 
int setMessageId(unsigned long uiMessageId)
Definition: PCanDevice.cpp:169
#define ERRID_DEV_BADINITSTRING
Definition: m5apiw32.h:209
int setBaudRate()
Definition: PCanDevice.cpp:115
#define MSGID_MP55_RECV
#define READ
#define MSGID_SCHUNK_RECV
unsigned char m_aucMessageData[8]
#define MSGID_ACK
int updateModuleIdMap()
Definition: Device.cpp:3779
int initMessage(const char *pcClassName, int iDebuglevel=0, bool bDebug=true, bool bDebugFile=false)
Definition: Message.cpp:90
int reinit(unsigned char ucBaudRateId)
Definition: PCanDevice.cpp:211
CRITICAL_SECTION m_csDevice
unsigned short m_uiQueueSize
Definition: PCanDevice.h:42
int m_iModuleCountMax
Definition: Device.h:46
int readDevice(CProtocolMessage &rclProtocolMessage)
Definition: PCanDevice.cpp:348
#define ERRID_DEV_ISINITIALIZED
Definition: m5apiw32.h:223
void error(const int iErrorCode, const char *pcErrorMessage,...) const
Definition: Message.cpp:204
#define ERRID_DEV_WRITEERROR
Definition: m5apiw32.h:212
virtual ~CPCanDevice()
destructor
Definition: PCanDevice.cpp:441
int getDebugLevel() const
Definition: Message.cpp:79
#define ERRID_DEV_NOTINITIALIZED
Definition: m5apiw32.h:211
char * m_DeviceName
Definition: PCanDevice.h:56
unsigned long m_uiTimeOut
Definition: PCanDevice.h:43
unsigned char m_ucMessageLength
int printMessage(CProtocolMessage &rclProtocolMessage, bool read)
int g_iDebugLevel
Definition: Message.cpp:20
int m_iNoOfRetries
Definition: PCanDevice.h:44
#define MAX_MP55
unsigned long m_uiMessageId
#define MSGID_STATE
#define MAX_SCHUNK
void warning(const char *pcWarningMessage,...) const
Definition: Message.cpp:257
bool g_bDebug
Definition: Message.cpp:22
#define ERRID_DEV_INITERROR
Definition: m5apiw32.h:210
int m_iDeviceId
Definition: PCanDevice.h:39
bool g_bDebugFile
Definition: Message.cpp:21
bool m_bInitFlag
Definition: Device.h:40
void setQueueSize(unsigned short uiQueueSize)
Definition: PCanDevice.cpp:472
#define MSGID_ALL
#define ERRID_DEV_EXITERROR
Definition: m5apiw32.h:219
void debug(const int iDebugLevel, const char *pcDebugMessage,...) const
Definition: Message.cpp:332
#define CMDID_STARTMOVE
#define WRITE
int waitForStartMotionAll()
Definition: PCanDevice.cpp:763
#define ERRID_DEV_NOLIBRARY
Definition: m5apiw32.h:222
int getDeviceError(int iErrorState)
Definition: PCanDevice.cpp:35
int m_hSyncDevice
Definition: PCanDevice.h:38
HANDLE m_handle
Definition: PCanDevice.h:36
int m_iErrorState
Definition: Device.h:50
CPCanDevice & operator=(const CPCanDevice &rclPCanDevice)
Definition: PCanDevice.cpp:454
int m_hDevice
Definition: PCanDevice.h:37
#define ERRID_DEV_READERROR
Definition: m5apiw32.h:213
CPCanDevice()
default constructor
Definition: PCanDevice.cpp:429
char m_acInitString[128]
Definition: Device.h:42
int clearReadQueue()
Definition: PCanDevice.cpp:186
int writeDevice(CProtocolMessage &rclProtocolMessage)
Definition: PCanDevice.cpp:392
int m_iBaudRate
Definition: Device.h:44
void setTimeOut(unsigned long uiTimeOut)
Definition: PCanDevice.cpp:477
unsigned long m_uiBaudRate
Definition: PCanDevice.h:41


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19