升级至C++23, 使用std::expected替代std::optional, 用range替代for,

This commit is contained in:
JIe
2024-09-29 16:43:03 +08:00
parent 95b6c44a3b
commit a64832cb5d
3 changed files with 233 additions and 208 deletions

67
main.cc
View File

@@ -1,24 +1,59 @@
#include "include/serial.h"
#include <iostream>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <ranges>
#include <limits>
#include <string_view>
#include "include/serial.h"
#undef max
using namespace std::literals::chrono_literals;
using namespace serial;
namespace ranges = std::ranges;
namespace views = std::views;
void PrintLog(const std::string& msg){
std::cout<<msg<<std::endl;
}
void PrintLog(const std::string &msg) { std::cout << msg << std::endl; }
int main(int argc, char** const argv){
int main(int argc, char **const argv) {
Serial serial;
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);
serial.SetLogCallBack(PrintLog);
if(!serial.OpenDevice(R"(\\.\COM11)", 115200)){
std::cout<<"Open device failed"<<std::endl;
return 0;
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;
}
}
auto startTime = std::chrono::system_clock::now();
// auto res = serial.GetAtUntil("AT+CFUN=1","OK", 200);
// std::cout<<res.value_or("ERROR")<<std::endl;
auto res = serial.GetAtResponse("AT+CGSN=0", 200);
std::cout<<res.value_or("ERROR")<<std::endl;
auto endTime = std::chrono::system_clock::now();
std::cout<<std::chrono::duration_cast<std::chrono::milliseconds>(endTime-startTime).count();
return 0;
}