rs71_usb_receiver.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. ******************************************************************************
  3. * @file : rs71_usb_receiver.h
  4. * @author : wangyingjie
  5. * @brief : None
  6. * @attention : None
  7. * @date : 2025/6/20
  8. ******************************************************************************
  9. */
  10. #ifndef RS71_USB_RECEIVER_H
  11. #define RS71_USB_RECEIVER_H
  12. #ifndef CAN_DEVICE_H
  13. #define CAN_DEVICE_H
  14. #include <vector>
  15. #include <string>
  16. #include <mutex>
  17. #ifdef WIN32
  18. #include <windows.h>
  19. #include <process.h>
  20. typedef HANDLE ThreadHandle;
  21. #define THREAD_RETURN unsigned __stdcall
  22. #else
  23. #include <pthread.h>
  24. #include <unistd.h>
  25. #define THREAD_RETURN void*
  26. typedef pthread_t ThreadHandle;
  27. #endif
  28. #include "controlcan.h"
  29. #define RX_BUFF_SIZE 1000
  30. #define RX_WAIT_TIME 100
  31. class RS71UsbReceiver
  32. {
  33. public:
  34. RS71UsbReceiver(unsigned devType, unsigned devIdx, unsigned chMask, unsigned baud);
  35. ~RS71UsbReceiver();
  36. bool init(unsigned devType, unsigned devIdx, unsigned chMask, unsigned baud);
  37. bool Open();
  38. void Close();
  39. bool Start();
  40. void Stop();
  41. struct CanFrame
  42. {
  43. unsigned int id;
  44. unsigned int timestamp;
  45. std::vector<unsigned char> data;
  46. };
  47. std::vector<CanFrame> GetReceivedData(unsigned channel);
  48. private:
  49. struct RX_CTX
  50. {
  51. unsigned channel;
  52. volatile bool stop;
  53. std::vector<CanFrame> buffer;
  54. std::mutex mtx;
  55. };
  56. static THREAD_RETURN RxThreadFunc(void *param);
  57. unsigned devType, devIdx, chMask, baud;
  58. RX_CTX rxContexts[4];
  59. ThreadHandle rxThreads[4];
  60. #ifdef WIN32
  61. static unsigned __stdcall RxThreadWrapper(void* param);
  62. #endif
  63. };
  64. #endif
  65. #endif //RS71_USB_RECEIVER_H