2024-10-10 15:59:53 +08:00
|
|
|
#include "include/serial.h"
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <algorithm>
|
2024-09-23 19:50:16 +08:00
|
|
|
#include <chrono>
|
2024-10-10 15:59:53 +08:00
|
|
|
#include <cstdio>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
2024-10-10 15:59:53 +08:00
|
|
|
#include <ranges>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <string_view>
|
|
|
|
#undef max
|
2024-09-23 19:50:16 +08:00
|
|
|
using namespace std::literals::chrono_literals;
|
2024-10-10 15:59:53 +08:00
|
|
|
using namespace std::literals::string_view_literals;
|
|
|
|
using namespace std::literals::string_literals;
|
2024-09-29 16:43:03 +08:00
|
|
|
using namespace serial;
|
|
|
|
namespace ranges = std::ranges;
|
|
|
|
namespace views = std::views;
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-09-29 16:43:03 +08:00
|
|
|
void PrintLog(const std::string &msg) { std::cout << msg << std::endl; }
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-10-10 15:59:53 +08:00
|
|
|
template <typename T>
|
|
|
|
requires std::is_same_v<T, std::string> ||
|
|
|
|
std::is_same_v<T, std::string_view>
|
|
|
|
std::vector<T> split(T str, T d) {
|
|
|
|
auto v = views::split(str, d) | views::transform([](auto word) {
|
|
|
|
return T(word.begin(), word.end());
|
|
|
|
});
|
|
|
|
return std::vector<T>(v.begin(), v.end());
|
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
int main(int argc, char **const argv) {
|
2024-09-23 19:50:16 +08:00
|
|
|
Serial serial;
|
2024-09-29 16:43:03 +08:00
|
|
|
auto ports = serial::GetUsbPorts();
|
|
|
|
ranges::for_each(ports,
|
|
|
|
[](const auto &port) { std::cout << port << std::endl; });
|
|
|
|
std::string portName;
|
|
|
|
std::string command;
|
|
|
|
std::cin >> portName;
|
|
|
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
|
|
|
serial.OpenDevice(portName);
|
2024-09-23 19:50:16 +08:00
|
|
|
serial.SetLogCallBack(PrintLog);
|
2024-09-29 16:43:03 +08:00
|
|
|
while (true) {
|
|
|
|
command.clear();
|
|
|
|
std::getline(std::cin, command);
|
|
|
|
system("cls");
|
|
|
|
auto startTime = std::chrono::system_clock::now();
|
2024-10-10 15:59:53 +08:00
|
|
|
if (command.at(0) == ':') {
|
|
|
|
command = command.substr(1, command.length() - 1);
|
|
|
|
auto sp = split(command, " "s);
|
|
|
|
auto reallyCommand = sp[0];
|
|
|
|
auto expect = sp[1];
|
|
|
|
auto res = serial.GetAtUntil(reallyCommand, 2000, expect);
|
2024-09-29 16:43:03 +08:00
|
|
|
auto endTime = std::chrono::system_clock::now();
|
2024-10-10 15:59:53 +08:00
|
|
|
res ? std::cout
|
|
|
|
<< "dura: "
|
2024-09-29 16:43:03 +08:00
|
|
|
<< std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
endTime - startTime)
|
|
|
|
.count()
|
2024-10-10 15:59:53 +08:00
|
|
|
<< std::endl
|
|
|
|
: std::cout << "Recive Error";
|
2024-09-29 16:43:03 +08:00
|
|
|
} else {
|
|
|
|
auto resp = serial.GetAtResponse(command);
|
|
|
|
auto endTime = std::chrono::system_clock::now();
|
|
|
|
std::cout << "dura: "
|
|
|
|
<< std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
endTime - startTime)
|
|
|
|
.count()
|
|
|
|
<< std::endl;
|
|
|
|
std::cout << resp.value_or("Send Command Fail") << std::endl;
|
|
|
|
}
|
2024-09-23 19:50:16 +08:00
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
return 0;
|
2024-10-12 14:46:49 +08:00
|
|
|
}
|