升级至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

View File

@ -3,11 +3,12 @@ cmake_minimum_required(VERSION 3.26)
set(PROJECT_N demo) set(PROJECT_N demo)
project(${PROJECT_N} VERSION 1.0) project(${PROJECT_N} VERSION 1.0)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(${CMAKE_SOURCE_DIR}/include) include_directories(${CMAKE_SOURCE_DIR}/include/)
include_directories(${CMAKE_SOURCE_DIR}/third/)
add_executable(${PROJECT_N} add_executable(${PROJECT_N}
${CMAKE_SOURCE_DIR}/third/serialib.cpp ${CMAKE_SOURCE_DIR}/third/serialib.cpp

View File

@ -1,150 +1,134 @@
#ifndef SERIAL_H #ifndef SERIAL_H
#define SERIAL_H #define SERIAL_H
#include <cstring>
#include <type_traits>
#include <iostream>
#include <string>
#include <chrono> #include <chrono>
#include <optional> #include <cstring>
#include <thread>
#include <functional>
#include <vector>
#include <format> #include <format>
#include "../third/serialib.h" #include <functional>
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
#include <thread>
#include <type_traits>
#include <vector>
#include <expected>
#include "serialib.h"
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
namespace ranges = std::ranges;
namespace views = std::views;
namespace serial namespace serial {
{
static std::vector<std::string> GetUsbPorts() static std::vector<std::string> GetUsbPorts() {
{
std::vector<std::string> portArray; std::vector<std::string> portArray;
std::string comname; std::string comname;
std::string showname; std::string showname;
for (int i = 0; i <= 256; i++) ranges::for_each(views::iota(1, 256), [&](int i) {
{
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i); std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
std::format_to(std::back_inserter(showname), "COM{}", i); std::format_to(std::back_inserter(showname), "COM{}", i);
// 创建或者打开一个文件或者I/O设备如执行成功则返回文件句柄 INVALID_HANDLE_VALUE 表示出错 const HANDLE m_handle = ::CreateFileA(
const HANDLE m_handle = comname.c_str(), static_cast<DWORD>(GENERIC_WRITE) | GENERIC_READ,
::CreateFileA(comname.c_str(), 0U, nullptr, OPEN_EXISTING, 0U, nullptr);
static_cast<DWORD>(GENERIC_WRITE) | GENERIC_READ, if (m_handle != INVALID_HANDLE_VALUE) {
0U,
nullptr,
OPEN_EXISTING,
0U,
nullptr
);
if (m_handle != INVALID_HANDLE_VALUE)
{
portArray.push_back(showname); portArray.push_back(showname);
CloseHandle(m_handle); CloseHandle(m_handle);
} }
comname.clear(); comname.clear();
showname.clear(); showname.clear();
} });
return portArray; return portArray;
} }
template <typename T> template <typename T>
concept SupportString = requires { concept SupportString = requires {
std::is_same_v<T, const char *>; std::is_same_v<T, const char *>;
std::is_same_v<T, std::string>; std::is_same_v<T, std::string>;
}; };
enum class enum class [[maybe_unused]] SerialErrorCode{
[[maybe_unused]] ErrorCode
{
SUCCESS, SUCCESS,
TIMEOUT, TIMEOUT,
SETTIMEOUTERROR, SETTIMEOUTERROR,
WRITEINGERROR, WRITEINGERROR,
READINGERROR, READINGERROR,
}; };
class Serial class Serial {
{
private: private:
serialib ser; serialib ser;
const char *endChar = "\r\n"; const char *endChar = "\r\n";
std::function<void(const std::string &)> logCallBack; std::function<void(const std::string &)> logCallBack;
bool removeEcho = true;
public: public:
Serial() = default; Serial() = default;
~Serial() { CloseDevice(); }
Serial(const Serial &other) = delete;
Serial(Serial &&other) = delete;
Serial &operator=(const Serial &other) = delete;
Serial &operator=(Serial &&other) = delete;
std::string GetTimeNow() auto SetRemoveEcho(bool remove) { removeEcho = remove; }
{
static std::string GetTimeNow() {
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now); auto now_c = std::chrono::system_clock::to_time_t(now);
char buffer[32]; char buffer[32];
ctime_s(buffer, 32, &now_c); auto _ = ctime_s(buffer, 32, &now_c);
return std::string(buffer); return buffer;
} }
bool IsOpen(){ bool IsOpen() { return ser.isDeviceOpen(); }
return ser.isDeviceOpen();
}
template <SupportString T> template <SupportString T>
bool OpenDevice(T portName, unsigned int bauds = 115200) bool OpenDevice(T portName, unsigned int bauds = 115200) {
{
std::string reallyPortName; std::string reallyPortName;
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}", portName); std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
if(ser.isDeviceOpen()) return true; portName);
int code; if (ser.isDeviceOpen())
code = ser.openDevice(reallyPortName.c_str(), bauds);
if (code == 1)
{
return true; return true;
} int code = ser.openDevice(reallyPortName.c_str(), bauds);
else if (code == 1) {
{ return true;
} else {
return false; return false;
} }
} }
void Log(const std::string &log) void Log(const std::string &log) const {
{ if (logCallBack) {
if (logCallBack) auto msg = GetTimeNow() + " " + log;
{
auto msg = GetTimeNow() + " " + log + "\n";
logCallBack(msg); logCallBack(msg);
} }
} }
void SetLogCallBack(std::function<void(const std::string &)> callBack) void
{ SetLogCallBack(const std::function<void(const std::string &)> &callBack) {
logCallBack = callBack; logCallBack = callBack;
} }
void CloseDevice() void CloseDevice() {
{ if (!ser.isDeviceOpen())
if(!ser.isDeviceOpen()) return; return;
ser.closeDevice(); ser.closeDevice();
} }
~Serial() = default; template <SupportString T>
std::expected<std::string, SerialErrorCode> DelayGetResponse(int delayTime, T command,
template<SupportString T> int timeout = 50) {
std::optional<std::string> DelayGetResponse(int delayTime, T command, int timeout = 50){
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime)); std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
return GetAtResponse(command, timeout); return GetAtResponse(command, timeout);
} }
template <SupportString T> template <SupportString T>
std::optional<std::string> GetAtResponse(T command, int timeout = 50) std::expected<std::string, SerialErrorCode> GetAtResponse(T command, int timeout = 50) {
{
ser.flushReceiver(); ser.flushReceiver();
std::string reallyCommand; std::string reallyCommand;
std::string response; if constexpr (std::is_same_v<T, std::string>) {
if constexpr (std::is_same_v<T, std::string>)
{
reallyCommand = command + endChar; reallyCommand = command + endChar;
} } else {
else
{
reallyCommand = std::string(command) + endChar; reallyCommand = std::string(command) + endChar;
} }
ser.writeString(reallyCommand.c_str()); ser.writeString(reallyCommand.c_str());
@ -154,36 +138,42 @@ namespace serial
char *buffer = new char[availableSize + 1]; char *buffer = new char[availableSize + 1];
std::memset(buffer, 0, availableSize); std::memset(buffer, 0, availableSize);
auto size = ser.readBytes(buffer, availableSize, timeout); auto size = ser.readBytes(buffer, availableSize, timeout);
if (size > 0)
{ if (size > 0) {
buffer[size] = '\0'; buffer[size] = '\0';
response = std::string(buffer); std::string response = std::string(buffer);
Log("Receive: " + response); Log("Receive: " + response);
delete[] buffer; delete[] buffer;
if (removeEcho)
response.replace(0, reallyCommand.length(), "");
return response; return response;
} }
delete[] buffer; delete[] buffer;
return std::nullopt; return std::unexpected(SerialErrorCode::TIMEOUT);
} }
template <SupportString T> template <SupportString T>
bool GetAtUntil(T command, T expect = "OK", int timeout = 50) auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
{ -> void {
auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout); for (int i = 0; i <= repeatTime; i++) {
auto _ = GetAtResponse(command, timeout);
}
}
template <SupportString T>
bool GetAtUntil(T command, T expect = "OK", int timeout = 200) {
auto endTime = std::chrono::system_clock::now() +
std::chrono::milliseconds(timeout);
ser.flushReceiver(); ser.flushReceiver();
std::string reallyCommand; std::string reallyCommand;
if constexpr (std::is_same_v<T, std::string>) if constexpr (std::is_same_v<T, std::string>) {
{
reallyCommand = command + endChar; reallyCommand = command + endChar;
} } else {
else
{
reallyCommand = std::string(command) + endChar; reallyCommand = std::string(command) + endChar;
} }
ser.writeString(reallyCommand.c_str()); ser.writeString(reallyCommand.c_str());
Log("Send : " + reallyCommand); Log("Send : " + reallyCommand);
while (std::chrono::system_clock::now() < endTime) while (std::chrono::system_clock::now() < endTime) {
{
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
auto availableSize = ser.available(); auto availableSize = ser.available();
auto buffer = new char[availableSize + 1]; auto buffer = new char[availableSize + 1];
@ -193,13 +183,12 @@ namespace serial
delete[] buffer; delete[] buffer;
if (size > 0) if (size > 0)
Log("Receive: " + str); Log("Receive: " + str);
if (str.find(expect) != std::string::npos) if (str.find(expect) != std::string::npos) {
{
return true; return true;
} }
} }
return false; return false;
} }
}; };
} } // namespace serial
#endif // SERIAL_H #endif // SERIAL_H

65
main.cc
View File

@ -1,24 +1,59 @@
#include "include/serial.h" #include <algorithm>
#include <iostream>
#include <chrono> #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 std::literals::chrono_literals;
using namespace serial;
namespace ranges = std::ranges;
namespace views = std::views;
void PrintLog(const std::string& msg){ void PrintLog(const std::string &msg) { std::cout << msg << std::endl; }
std::cout<<msg<<std::endl;
}
int main(int argc, char** const argv){ int main(int argc, char **const argv) {
Serial serial; 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); serial.SetLogCallBack(PrintLog);
if(!serial.OpenDevice(R"(\\.\COM11)", 115200)){ while (true) {
std::cout<<"Open device failed"<<std::endl; command.clear();
return 0; std::getline(std::cin, command);
} system("cls");
auto startTime = std::chrono::system_clock::now(); auto startTime = std::chrono::system_clock::now();
// auto res = serial.GetAtUntil("AT+CFUN=1","OK", 200); if (command.find(":") != std::string::npos) {
// std::cout<<res.value_or("ERROR")<<std::endl; command.erase(std::remove_if(command.begin(), command.end(),
auto res = serial.GetAtResponse("AT+CGSN=0", 200); [](char c) { return c == ':'; }),
std::cout<<res.value_or("ERROR")<<std::endl; 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(); auto endTime = std::chrono::system_clock::now();
std::cout<<std::chrono::duration_cast<std::chrono::milliseconds>(endTime-startTime).count(); 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;
}
}
return 0;
} }