12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #include <iostream>
- #include<signal.h>
- #include "DataAccess/ArduinoSerial.h"
-
-
- constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
-
- std::string hexStr(std::string str)
- {
- std::string s(str.size() * 2, ' ');
- for (int i = 0; i < str.size(); ++i) {
- s[2 * i] = hexmap[(str[i] & 0xF0) >> 4];
- s[2 * i + 1] = hexmap[str[i] & 0x0F];
- }
- return s;
- }
-
- int lastSignal = 0;
-
- void sig_handler(int signo)
- {
- std::cout << "Signal catched " << signo << std::endl;
- lastSignal = signo;
- }
-
- int main()
- {
- signal(SIGINT, sig_handler);
- signal(SIGTERM, sig_handler);
- signal(SIGKILL, sig_handler);
-
- ArduinoSerial arduinoSerial("/dev/ttyUSB0", SERIAL_BAUDRATE);
- auto res = arduinoSerial.open();
- if (!res) {
- res.print();
- return 1;
- }
- while (lastSignal == 0)
- {
- auto data = arduinoSerial.read();
- if (!data) {
- data.print();
- return 2;
- }
- std::cout << (int) data.getData().first << " " << hexStr(data.getData().second)
- << " " << data.getData().second << std::endl;
- if (data.getData().first == PACKET_SELF_TEST) {
- SERIAL_PACKET_TYPE_INT status = ERROR_NONE;
- arduinoSerial.write((const char*)&status, sizeof(status));
- }
- }
- return 0;
-
- }
|