You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <stdio.h>
  2. #include "zlinuxcomponents/zmainhelper.hpp"
  3. //
  4. #include "iflytopcpp/core/spdlogfactory/logger.hpp"
  5. #include "iflytopcpp/core/thread/thread.hpp"
  6. #include "spdlog/spdlog.h"
  7. //
  8. #include <linux/can.h>
  9. #include <linux/can/raw.h>
  10. #include <net/if.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/socket.h>
  16. #include <unistd.h>
  17. #include "iflytopcpp/core/driver/socketcan/socket_can.hpp"
  18. using namespace iflytop;
  19. using namespace core;
  20. using namespace std;
  21. using namespace clipp;
  22. // using sockcanpp::CanDriver;
  23. // using sockcanpp::CanId;
  24. // using sockcanpp::CanMessage;
  25. // using sockcanpp::exceptions::CanException;
  26. // using sockcanpp::exceptions::CanInitException;
  27. // using sockcanpp::exceptions::InvalidSocketException;
  28. /***********************************************************************/
  29. /***********************************************************************/
  30. /***********************************************************************/
  31. /***********************************************************************/
  32. class Main {
  33. ENABLE_LOGGER(Main);
  34. unique_ptr<Thread> thread;
  35. shared_ptr<SocketCan> socketCan;
  36. public:
  37. Main(/* args */) {}
  38. ~Main() {}
  39. void run(int argc, char* argv[]) {
  40. thread.reset(new Thread("main", [&]() { exit(main(argc, argv)); }));
  41. while (true) sleep(1000);
  42. }
  43. void onSIGINT() { exit(0); }
  44. int main(int argCount, char* argValues[]) {
  45. socketCan.reset(new SocketCan());
  46. shared_ptr<SocketCanConfig> socketCanConfig = make_shared<SocketCanConfig>();
  47. socketCanConfig->m_canName = "can0";
  48. socketCanConfig->m_canBaudrate = 500000;
  49. socketCanConfig->enablLoopback = false;
  50. socketCan->initialize(socketCanConfig);
  51. socketCan->startListen();
  52. socketCan->onSocketCanFrame.connect([this](shared_ptr<SocketCanFrame> frame) { logger->info("RX:{}", frame->toString()); });
  53. ThisThread thisThread;
  54. int i = 0;
  55. while (!thisThread.getExitFlag()) {
  56. uint8_t data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
  57. shared_ptr<SocketCanFrame> canframe = SocketCanFrame::createExtDataFrame(0x11223344, data, 8);
  58. logger->info("TX Frame {}", i);
  59. socketCan->sendFrame(canframe, 5);
  60. i++;
  61. }
  62. while (true) {
  63. sleep(1000);
  64. }
  65. return 0;
  66. }
  67. };
  68. MAIN_ENTRY();
  69. // ip link set can0 down
  70. // ip link set can0 type can bitrate 500000
  71. // ip link set can0 up