2024-09-23 19:50:16 +08:00
|
|
|
#ifndef SERIAL_H
|
|
|
|
#define SERIAL_H
|
|
|
|
|
2024-10-09 17:04:41 +08:00
|
|
|
#include <algorithm>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <chrono>
|
2024-10-09 17:04:41 +08:00
|
|
|
#include <expected>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <format>
|
|
|
|
#include <functional>
|
2024-09-23 19:50:16 +08:00
|
|
|
#include <iostream>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <ranges>
|
2024-10-10 10:52:45 +08:00
|
|
|
#include <sstream>
|
2024-09-23 19:50:16 +08:00
|
|
|
#include <string>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <string_view>
|
2024-09-23 19:50:16 +08:00
|
|
|
#include <thread>
|
2024-09-29 16:43:03 +08:00
|
|
|
#include <type_traits>
|
2024-09-24 19:34:34 +08:00
|
|
|
#include <vector>
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-09-29 16:43:03 +08:00
|
|
|
#include "serialib.h"
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-09-29 16:43:03 +08:00
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
namespace ranges = std::ranges;
|
|
|
|
namespace views = std::views;
|
|
|
|
|
|
|
|
namespace serial {
|
2024-10-10 15:59:53 +08:00
|
|
|
template <typename... Types> struct _StrongType {};
|
|
|
|
|
|
|
|
template <typename T> struct _StrongType<T> {
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
|
|
|
|
using Type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename... Types> struct _StrongType<T, Types...> {
|
|
|
|
using Type =
|
|
|
|
typename _StrongType<T, typename _StrongType<Types...>::Type>::Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Types>
|
|
|
|
using _strongType_t = typename _StrongType<Types...>::Type;
|
|
|
|
|
|
|
|
template <typename... Args> struct _IsCastable {};
|
|
|
|
|
|
|
|
template <typename T> struct _IsCastable<T> {
|
|
|
|
static constexpr bool value = true;
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename U> struct _IsCastable<T, U> {
|
|
|
|
using __TRUE = char;
|
|
|
|
using __FALSE = struct {
|
|
|
|
char _[2];
|
|
|
|
};
|
|
|
|
static consteval __TRUE __TEST(U);
|
|
|
|
static consteval __FALSE __TEST(...);
|
|
|
|
static constexpr bool value =
|
|
|
|
sizeof(__TEST(std::declval<T>())) == sizeof(__TRUE);
|
|
|
|
using Type = std::conditional_t<value, U, void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename... Args> struct _IsCastable<T, Args...> {
|
|
|
|
static constexpr bool value =
|
|
|
|
_IsCastable<T, typename _IsCastable<Args...>::Type>::value;
|
|
|
|
using Type =
|
|
|
|
std::conditional_t<value, typename _IsCastable<Args...>::Type, void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
concept _SupportString = requires {
|
|
|
|
_IsCastable<T, const char *, char *, std::string, std::string_view>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <_SupportString T> std::string _to_string(T &&str) {
|
|
|
|
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
|
|
|
|
return str;
|
|
|
|
} else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) {
|
|
|
|
return std::move(std::string(str.data()));
|
|
|
|
} else if constexpr (std::is_same_v<std::decay_t<T>, const char*>) {
|
|
|
|
return std::string(str);
|
|
|
|
}
|
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
|
|
|
|
static std::vector<std::string> GetUsbPorts() {
|
|
|
|
std::vector<std::string> portArray;
|
|
|
|
std::string comname;
|
|
|
|
std::string showname;
|
|
|
|
ranges::for_each(views::iota(1, 256), [&](int i) {
|
|
|
|
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
|
|
|
|
std::format_to(std::back_inserter(showname), "COM{}", i);
|
|
|
|
const HANDLE m_handle = ::CreateFileA(
|
|
|
|
comname.c_str(), static_cast<DWORD>(GENERIC_WRITE) | GENERIC_READ,
|
|
|
|
0U, nullptr, OPEN_EXISTING, 0U, nullptr);
|
|
|
|
if (m_handle != INVALID_HANDLE_VALUE) {
|
|
|
|
portArray.push_back(showname);
|
|
|
|
CloseHandle(m_handle);
|
2024-09-23 19:50:16 +08:00
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
comname.clear();
|
|
|
|
showname.clear();
|
|
|
|
});
|
|
|
|
return portArray;
|
|
|
|
}
|
|
|
|
|
2024-10-09 17:04:41 +08:00
|
|
|
enum class [[maybe_unused]] SerialErrorCode {
|
2024-09-29 16:43:03 +08:00
|
|
|
SUCCESS,
|
|
|
|
TIMEOUT,
|
|
|
|
SETTIMEOUTERROR,
|
|
|
|
WRITEINGERROR,
|
|
|
|
READINGERROR,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Serial {
|
|
|
|
private:
|
|
|
|
serialib ser;
|
|
|
|
const char *endChar = "\r\n";
|
|
|
|
std::function<void(const std::string &)> logCallBack;
|
2024-10-10 10:52:45 +08:00
|
|
|
bool removeEcho = true; // 剔除回显
|
2024-09-29 16:43:03 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
Serial() = default;
|
|
|
|
~Serial() { CloseDevice(); }
|
|
|
|
Serial(const Serial &other) = delete;
|
|
|
|
Serial(Serial &&other) = delete;
|
|
|
|
Serial &operator=(const Serial &other) = delete;
|
|
|
|
Serial &operator=(Serial &&other) = delete;
|
|
|
|
|
|
|
|
auto SetRemoveEcho(bool remove) { removeEcho = remove; }
|
2024-10-10 15:59:53 +08:00
|
|
|
auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
|
|
|
|
auto SetRts(bool flag) { return flag ? ser.setRTS() : ser.clearRTS(); }
|
2024-09-29 16:43:03 +08:00
|
|
|
|
|
|
|
static std::string GetTimeNow() {
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
auto now_c = std::chrono::system_clock::to_time_t(now);
|
|
|
|
char buffer[32];
|
|
|
|
auto _ = ctime_s(buffer, 32, &now_c);
|
|
|
|
return buffer;
|
2024-09-23 19:50:16 +08:00
|
|
|
}
|
|
|
|
|
2024-09-29 16:43:03 +08:00
|
|
|
bool IsOpen() { return ser.isDeviceOpen(); }
|
|
|
|
|
2024-10-10 15:59:53 +08:00
|
|
|
template <_SupportString T>
|
2024-10-09 17:11:40 +08:00
|
|
|
bool OpenDevice(T portName, unsigned int bauds = 115200,
|
|
|
|
int delayTime = 0) {
|
2024-09-29 16:43:03 +08:00
|
|
|
std::string reallyPortName;
|
|
|
|
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
|
|
|
|
portName);
|
2024-10-09 17:04:41 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
2024-09-29 16:43:03 +08:00
|
|
|
if (ser.isDeviceOpen())
|
|
|
|
return true;
|
|
|
|
int code = ser.openDevice(reallyPortName.c_str(), bauds);
|
|
|
|
if (code == 1) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2024-09-24 19:34:34 +08:00
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
}
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-09-29 16:43:03 +08:00
|
|
|
void Log(const std::string &log) const {
|
|
|
|
if (logCallBack) {
|
|
|
|
auto msg = GetTimeNow() + " " + log;
|
|
|
|
logCallBack(msg);
|
2024-09-24 19:34:34 +08:00
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
}
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-09-29 16:43:03 +08:00
|
|
|
void
|
|
|
|
SetLogCallBack(const std::function<void(const std::string &)> &callBack) {
|
|
|
|
logCallBack = callBack;
|
|
|
|
}
|
|
|
|
void CloseDevice() {
|
|
|
|
if (!ser.isDeviceOpen())
|
|
|
|
return;
|
|
|
|
ser.closeDevice();
|
|
|
|
}
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-10-10 15:59:53 +08:00
|
|
|
template <_SupportString T>
|
2024-10-09 17:04:41 +08:00
|
|
|
std::expected<std::string, SerialErrorCode>
|
|
|
|
DelayGetResponse(int delayTime, T command, int timeout = 50) {
|
2024-09-29 16:43:03 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
|
|
|
return GetAtResponse(command, timeout);
|
|
|
|
}
|
2024-09-24 19:34:34 +08:00
|
|
|
|
2024-10-10 10:52:45 +08:00
|
|
|
template <int repeatTime = 5, int delayTime = 200, int timeout = 200,
|
2024-10-10 15:59:53 +08:00
|
|
|
_SupportString T, _SupportString... Args>
|
2024-10-10 10:52:45 +08:00
|
|
|
bool GetAtUntilRepeat(T command, Args... args) {
|
|
|
|
std::stringstream ss;
|
|
|
|
int i = 0;
|
|
|
|
while (i++ < repeatTime) {
|
|
|
|
ss.str("");
|
|
|
|
ss << "Count: " << i << "\n";
|
|
|
|
Log(ss.str());
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
|
|
|
if (GetAtUntil<timeout>(command, args...))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-10 15:59:53 +08:00
|
|
|
template <_SupportString T>
|
2024-10-09 17:04:41 +08:00
|
|
|
std::expected<std::string, SerialErrorCode>
|
|
|
|
GetAtResponse(T command, int timeout = 50) {
|
2024-09-29 16:43:03 +08:00
|
|
|
ser.flushReceiver();
|
2024-10-10 15:59:53 +08:00
|
|
|
std::string reallyCommand = std::string(command) + endChar;
|
2024-09-29 16:43:03 +08:00
|
|
|
ser.writeString(reallyCommand.c_str());
|
|
|
|
Log("Send: " + reallyCommand);
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
auto availableSize = ser.available();
|
2024-10-09 17:11:40 +08:00
|
|
|
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
|
|
|
std::memset(buffer.get(), 0, availableSize);
|
|
|
|
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
2024-09-29 16:43:03 +08:00
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
buffer[size] = '\0';
|
2024-10-10 15:59:53 +08:00
|
|
|
std::string response = std::string(buffer.get());
|
2024-09-29 16:43:03 +08:00
|
|
|
Log("Receive: " + response);
|
|
|
|
if (removeEcho)
|
|
|
|
response.replace(0, reallyCommand.length(), "");
|
|
|
|
return response;
|
2024-09-23 19:50:16 +08:00
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
return std::unexpected(SerialErrorCode::TIMEOUT);
|
|
|
|
}
|
2024-09-23 19:50:16 +08:00
|
|
|
|
2024-10-10 15:59:53 +08:00
|
|
|
template <_SupportString T>
|
2024-09-29 16:43:03 +08:00
|
|
|
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
|
|
|
|
-> void {
|
|
|
|
for (int i = 0; i <= repeatTime; i++) {
|
|
|
|
auto _ = GetAtResponse(command, timeout);
|
2024-09-23 19:50:16 +08:00
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
}
|
2024-09-24 19:34:34 +08:00
|
|
|
|
2024-10-10 15:59:53 +08:00
|
|
|
template <int timeout = 200, _SupportString T, _SupportString... Args>
|
2024-10-09 17:04:41 +08:00
|
|
|
bool GetAtUntil(T command, Args... expect) {
|
2024-09-29 16:43:03 +08:00
|
|
|
auto endTime = std::chrono::system_clock::now() +
|
|
|
|
std::chrono::milliseconds(timeout);
|
|
|
|
ser.flushReceiver();
|
2024-10-10 15:59:53 +08:00
|
|
|
std::string reallyCommand = std::string(command) + endChar;
|
2024-09-29 16:43:03 +08:00
|
|
|
ser.writeString(reallyCommand.c_str());
|
|
|
|
Log("Send : " + reallyCommand);
|
|
|
|
while (std::chrono::system_clock::now() < endTime) {
|
2024-09-23 19:50:16 +08:00
|
|
|
std::this_thread::sleep_for(10ms);
|
2024-09-24 16:59:08 +08:00
|
|
|
auto availableSize = ser.available();
|
2024-10-09 17:11:40 +08:00
|
|
|
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
|
|
|
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
2024-09-29 16:43:03 +08:00
|
|
|
buffer[size] = '\0';
|
2024-10-09 17:11:40 +08:00
|
|
|
auto str = std::string(buffer.get());
|
2024-09-29 16:43:03 +08:00
|
|
|
if (size > 0)
|
|
|
|
Log("Receive: " + str);
|
2024-10-09 17:04:41 +08:00
|
|
|
if (((str.find(expect) != std::string::npos) && ...)) {
|
2024-09-29 16:43:03 +08:00
|
|
|
return true;
|
2024-09-24 19:34:34 +08:00
|
|
|
}
|
|
|
|
}
|
2024-09-29 16:43:03 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace serial
|
2024-09-23 19:50:16 +08:00
|
|
|
#endif // SERIAL_H
|