can_sensor.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdint.h>
  2. #include <iostream>
  3. #include <assert.h>
  4. #include "can_sensor.h"
  5. TPCANStatus CCanSensor::Initialize()
  6. {
  7. int iBuffer;
  8. TPCANStatus stsResult;
  9. TPCANHandle _HandlesArray[16];
  10. _HandlesArray[0] = PCAN_USBBUS1;
  11. _HandlesArray[1] = PCAN_USBBUS2;
  12. _HandlesArray[2] = PCAN_USBBUS3;
  13. _HandlesArray[3] = PCAN_USBBUS4;
  14. _HandlesArray[4] = PCAN_USBBUS5;
  15. _HandlesArray[5] = PCAN_USBBUS6;
  16. _HandlesArray[6] = PCAN_USBBUS7;
  17. _HandlesArray[7] = PCAN_USBBUS8;
  18. _HandlesArray[8] = PCAN_USBBUS9;
  19. _HandlesArray[9] = PCAN_USBBUS10;
  20. _HandlesArray[10] = PCAN_USBBUS11;
  21. _HandlesArray[11] = PCAN_USBBUS12;
  22. _HandlesArray[12] = PCAN_USBBUS13;
  23. _HandlesArray[13] = PCAN_USBBUS14;
  24. _HandlesArray[14] = PCAN_USBBUS15;
  25. _HandlesArray[15] = PCAN_USBBUS16;
  26. for (int i = 0; i < (sizeof(_HandlesArray) / sizeof(TPCANHandle)); i++)
  27. {
  28. stsResult = CAN_GetValue(_HandlesArray[i], PCAN_CHANNEL_CONDITION, &iBuffer, sizeof(iBuffer));
  29. if (((stsResult) == PCAN_ERROR_OK) && ((iBuffer & PCAN_CHANNEL_AVAILABLE) == PCAN_CHANNEL_AVAILABLE))
  30. {
  31. stsResult = CAN_GetValue((TPCANHandle)_HandlesArray[i], PCAN_CHANNEL_FEATURES, (void*)&iBuffer, sizeof(iBuffer));
  32. _isFD = (stsResult == PCAN_ERROR_OK) && (iBuffer & FEATURE_FD_CAPABLE);
  33. _handle = _HandlesArray[i];
  34. break;
  35. }
  36. }
  37. if (_handle !=PCAN_NONEBUS)
  38. {
  39. stsResult= ::CAN_Initialize(_handle,PCAN_BAUD_250K, 0,0,0);
  40. if(stsResult==PCAN_ERROR_OK)
  41. {
  42. iBuffer=PCAN_PARAMETER_ON;
  43. stsResult=CAN_SetValue(_handle,PCAN_BUSOFF_AUTORESET,&iBuffer, sizeof(iBuffer));
  44. }
  45. return stsResult;
  46. }
  47. return stsResult;
  48. }
  49. TPCANStatus CCanSensor::GetErrorText(TPCANStatus Error, WORD Language, LPSTR Buffer)
  50. {
  51. return CAN_GetErrorText(Error, Language, Buffer);
  52. }
  53. TPCANStatus CCanSensor::Uninitialize()
  54. {
  55. return CAN_Uninitialize(_handle);
  56. }
  57. TPCANStatus CCanSensor::Read(TPCANMsg* MessageBuffer, TPCANTimestamp* TimestampBuffer)
  58. {
  59. auto ret= CAN_Read(_handle,MessageBuffer,TimestampBuffer);
  60. //assert(ret==PCAN_ERROR_OK||PCAN_ERROR_QRCVEMPTY==ret);
  61. return ret;
  62. }
  63. TPCANStatus CCanSensor::Write(TPCANMsg* MessageBuffer)
  64. {
  65. auto ret= CAN_Write(_handle,MessageBuffer);
  66. if(ret!=PCAN_ERROR_OK)
  67. {
  68. std::cout<<ret<<std::endl;
  69. CAN_Reset(_handle);
  70. }
  71. // assert(ret==PCAN_ERROR_OK);
  72. return ret;
  73. }