main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <QCoreApplication>
  2. #include <QObject>
  3. #include <signal.h>
  4. #include "nvcan.h"
  5. #include "canctrl.h"
  6. #include <unistd.h>
  7. #include <thread>
  8. #include <iostream>
  9. #include <xmlparam.h>
  10. #include "ivversion.h"
  11. #include "ivexit.h"
  12. #include <signal.h>
  13. static canctrl * gpcanctrl;
  14. void exitfunc()
  15. {
  16. qDebug("enter exit func.");
  17. gpcanctrl->requestInterruption();
  18. QTime xTime;
  19. xTime.start();
  20. while(xTime.elapsed()<1000)
  21. {
  22. if(gpcanctrl->isFinished())
  23. {
  24. qDebug("canctrl complete.");
  25. delete gpcanctrl;
  26. break;
  27. }
  28. }
  29. }
  30. void signal_handler(int sig)
  31. {
  32. if(sig == SIGINT)
  33. {
  34. exitfunc();
  35. }
  36. }
  37. #ifdef TEST_PROG
  38. void threadtest()
  39. {
  40. QTimer xTimer;
  41. xTimer.setTimerType(Qt::PreciseTimer);
  42. void * pa = iv::modulecomm::RegisterSend("cansend0",100000,100);
  43. while(true)
  44. {
  45. iv::can::canmsg xmsg;
  46. xmsg.set_channel(0);
  47. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  48. xmsg.set_index(0);
  49. int i;
  50. for(i=0;i<10;i++)
  51. {
  52. iv::can::canraw * pcanraw = xmsg.add_rawmsg();
  53. pcanraw->set_bext(false);
  54. pcanraw->set_id(11);
  55. pcanraw->set_bremote(false);
  56. char xdata[8];
  57. xdata[0] = 11;
  58. pcanraw->set_data(xdata,8);
  59. pcanraw->set_len(8);
  60. }
  61. int nbytesize = xmsg.ByteSize();
  62. std::shared_ptr<char> str_ptr = std::shared_ptr<char>(new char[nbytesize]);
  63. if(xmsg.SerializeToArray(str_ptr.get(),nbytesize))
  64. iv::modulecomm::ModuleSendMsg(pa,str_ptr.get(),nbytesize);
  65. // std::this_thread::sleep_for(std::chrono::milliseconds(1));
  66. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  67. }
  68. }
  69. #endif
  70. int main(int argc, char *argv[])
  71. {
  72. showversion("driver_can_nvidia_agx");
  73. QCoreApplication a(argc, argv);
  74. QString strpath = QCoreApplication::applicationDirPath();
  75. if(argc < 2)
  76. strpath = strpath + "/driver_can_socket.xml";
  77. else
  78. strpath = argv[1];
  79. std::cout<<strpath.toStdString()<<std::endl;
  80. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  81. std::string strmemsend0 = xp.GetParam("cansend0_agx","cansend0");
  82. std::string strmemrecv0 = xp.GetParam("canrecv0_agx","canrecv0");
  83. std::string strcanname = xp.GetParam("canname","can0");
  84. gpcanctrl = new canctrl(strmemsend0.data(),strmemrecv0.data(),strcanname.data());
  85. gpcanctrl->start();
  86. iv::ivexit::RegIVExitCall(exitfunc);
  87. signal(SIGINT,signal_handler);
  88. #ifdef TEST_PROG
  89. std::thread * pthread = new std::thread(threadtest);
  90. #endif
  91. // std::thread b(func);
  92. return a.exec();
  93. }