添加相关函数

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

View File

@ -73,9 +73,11 @@ concept _SupportString = requires {
template <_SupportString T> [[maybe_unused]] std::string _to_string(T&& str) { template <_SupportString T> [[maybe_unused]] std::string _to_string(T&& str) {
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) { if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
return str; 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())); 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); return std::string(str);
} }
} }
@ -183,7 +185,8 @@ class Serial {
static_cast<::SerialStopBits>(stopBits)); static_cast<::SerialStopBits>(stopBits));
if (code == 1) { if (code == 1) {
return true; return true;
} else { }
else {
return false; return false;
} }
} }
@ -283,6 +286,33 @@ class Serial {
} }
return false; 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 } // namespace serial
#endif // SERIAL_H #endif // SERIAL_H