2024-09-29 16:43:03 +08:00
|
|
|
#include <algorithm>
|
2024-09-23 19:50:16 +08:00
|
|
|
#include <chrono>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <ranges>
|
|
|
|
#include <limits>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include "include/serial.h"
|
|
|
|
#undef max
|
2024-09-23 19:50:16 +08:00
|
|
|
using namespace std::literals::chrono_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-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();
|
|
|
|
if (command.find(":") != std::string::npos) {
|
|
|
|
command.erase(std::remove_if(command.begin(), command.end(),
|
|
|
|
[](char c) { return c == ':'; }),
|
|
|
|
command.end());
|
|
|
|
auto reallyCommand = command.substr(0, command.find_first_of(' '));
|
|
|
|
auto expect = command.substr(command.find_first_of(' ') + 1,
|
|
|
|
command.length());
|
|
|
|
auto res = serial.GetAtUntil(reallyCommand, expect, 2000);
|
|
|
|
auto endTime = std::chrono::system_clock::now();
|
|
|
|
std::cout << "dura: "
|
|
|
|
<< std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
endTime - startTime)
|
|
|
|
.count()
|
|
|
|
<< std::endl;
|
|
|
|
} 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-09-23 19:50:16 +08:00
|
|
|
}
|