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.

main.cpp 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <iostream>
  2. #include<signal.h>
  3. #include "DataAccess/ArduinoSerial.h"
  4. constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
  5. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  6. std::string hexStr(std::string str)
  7. {
  8. std::string s(str.size() * 2, ' ');
  9. for (int i = 0; i < str.size(); ++i) {
  10. s[2 * i] = hexmap[(str[i] & 0xF0) >> 4];
  11. s[2 * i + 1] = hexmap[str[i] & 0x0F];
  12. }
  13. return s;
  14. }
  15. int lastSignal = 0;
  16. void sig_handler(int signo)
  17. {
  18. std::cout << "Signal catched " << signo << std::endl;
  19. lastSignal = signo;
  20. }
  21. int main()
  22. {
  23. signal(SIGINT, sig_handler);
  24. signal(SIGTERM, sig_handler);
  25. signal(SIGKILL, sig_handler);
  26. ArduinoSerial arduinoSerial("/dev/ttyUSB0", SERIAL_BAUDRATE);
  27. auto res = arduinoSerial.open();
  28. if (!res) {
  29. res.print();
  30. return 1;
  31. }
  32. while (lastSignal == 0)
  33. {
  34. auto data = arduinoSerial.read();
  35. if (!data) {
  36. data.print();
  37. return 2;
  38. }
  39. std::cout << (int) data.getData().first << " " << hexStr(data.getData().second)
  40. << " " << data.getData().second << std::endl;
  41. if (data.getData().first == PACKET_SELF_TEST) {
  42. SERIAL_PACKET_TYPE_INT status = 42;
  43. arduinoSerial.write((const char*)&status, sizeof(status));
  44. }
  45. }
  46. return 0;
  47. }