添加相关函数

This commit is contained in:
JIe 2024-11-01 15:22:42 +08:00
parent 6ec5ffd22f
commit 6605f0604a

View File

@ -22,32 +22,32 @@ namespace ranges = std::ranges;
namespace views = std::views;
namespace serial {
template <typename... Types> struct _StrongType {};
template <typename... Types> struct _StrongType {};
template <typename T> struct _StrongType<T> {
template <typename T> struct _StrongType<T> {
using Type = T;
};
};
template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
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...> {
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... Types>
using _strongType_t = typename _StrongType<Types...>::Type;
template <typename... Args> struct _IsCastable {};
template <typename... Args> struct _IsCastable {};
template <typename T> struct _IsCastable<T> {
template <typename T> struct _IsCastable<T> {
static constexpr bool value = true;
using Type = T;
};
};
template <typename T, typename U> struct _IsCastable<T, U> {
template <typename T, typename U> struct _IsCastable<T, U> {
using __TRUE = char;
using __FALSE = struct {
char _[2];
@ -57,30 +57,32 @@ template <typename T, typename U> struct _IsCastable<T, U> {
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...> {
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> [[maybe_unused]] std::string _to_string(T &&str) {
template <typename T>
concept _SupportString = requires {
_IsCastable<T, const char*, char*, std::string, std::string_view>::value;
};
template <_SupportString T> [[maybe_unused]] 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>) {
}
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 *>) {
}
else if constexpr (std::is_same_v<std::decay_t<T>, const char*>) {
return std::string(str);
}
}
}
static std::vector<std::string> GetUsbPorts() {
static std::vector<std::string> GetUsbPorts() {
#if defined(_WIN32) || defined(_WIN64)
std::vector<std::string> portArray;
std::string comname;
@ -102,50 +104,50 @@ static std::vector<std::string> GetUsbPorts() {
#elif defined(__linux__)
#endif
}
}
enum class [[maybe_unused]] SerialErrorCode {
enum class [[maybe_unused]] SerialErrorCode {
SUCCESS,
TIMEOUT,
SETTIMEOUTERROR,
WRITEINGERROR,
READINGERROR,
};
};
enum SerialDataBits {
enum SerialDataBits {
DATABIT_5 = 0,
DATABIT_6,
DATABIT_7,
DATABIT_8,
DATABIT_16,
};
enum SerialStopBits {
};
enum SerialStopBits {
STOPBIT_1 = 0,
STOPBIT_1_5,
STOPBIT_2,
};
enum SerialParity {
};
enum SerialParity {
SERIAL_PARITY_NONE = 0,
SERIAL_PARITY_EVEN,
SERIAL_PARITY_ODD,
SERIAL_PARITY_MARK,
SERIAL_PARITY_SPACE,
};
};
class Serial {
class Serial {
private:
serialib ser;
const char *endChar = "\r\n";
std::function<void(const std::string &)> logCallBack;
const char* endChar = "\r\n";
std::function<void(const std::string&)> logCallBack;
bool removeEcho = true; // 剔除回显
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;
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; }
auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
@ -183,12 +185,13 @@ class Serial {
static_cast<::SerialStopBits>(stopBits));
if (code == 1) {
return true;
} else {
}
else {
return false;
}
}
void Log(const std::string &log) const {
void Log(const std::string& log) const {
if (logCallBack) {
auto msg = GetTimeNow() + " " + log;
logCallBack(msg);
@ -196,7 +199,7 @@ class Serial {
}
void
SetLogCallBack(const std::function<void(const std::string &)> &callBack) {
SetLogCallBack(const std::function<void(const std::string&)>& callBack) {
logCallBack = callBack;
}
void CloseDevice() {
@ -283,6 +286,33 @@ class Serial {
}
return false;
}
};
template <int timeout = 200, _SupportString T, _SupportString... Args>
std::string GetAtUntilAndReturn(T command, Args... expect)
{
auto endTime = std::chrono::system_clock::now() +
std::chrono::milliseconds(timeout);
ser.flushReceiver();
std::string resp = "";
std::string reallyCommand = std::string(command) + endChar;
ser.writeString(reallyCommand.c_str());
Log("Send : " + reallyCommand);
while (std::chrono::system_clock::now() < endTime) {
std::this_thread::sleep_for(10ms);
auto availableSize = ser.available();
auto buffer = std::make_unique<char[]>(availableSize + 1);
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
buffer[size] = '\0';
auto str = std::string(buffer.get());
resp += str;
if (size > 0)
Log("Receive: " + str);
if (((str.find(expect) != std::string::npos) && ...)) {
return resp;
}
}
return resp;
}
};
} // namespace serial
#endif // SERIAL_H