diff --git a/include/serial.h b/include/serial.h index 050c8e4..bed5fd0 100644 --- a/include/serial.h +++ b/include/serial.h @@ -1,8 +1,10 @@ #ifndef SERIAL_H #define SERIAL_H +#include #include #include +#include #include #include #include @@ -12,7 +14,6 @@ #include #include #include -#include #include "serialib.h" @@ -48,7 +49,7 @@ concept SupportString = requires { std::is_same_v; }; -enum class [[maybe_unused]] SerialErrorCode{ +enum class [[maybe_unused]] SerialErrorCode { SUCCESS, TIMEOUT, SETTIMEOUTERROR, @@ -83,17 +84,18 @@ class Serial { bool IsOpen() { return ser.isDeviceOpen(); } - template - bool OpenDeviceDelay(T portName, unsigned int baudRate, int delay){ + template + bool OpenDeviceDelay(T portName, unsigned int baudRate, int delay) { std::this_thread::sleep_for(std::chrono::seconds(delay)); return OpenDevice(portName, baudRate); } template - bool OpenDevice(T portName, unsigned int bauds = 115200) { + bool OpenDevice(T portName, unsigned int bauds = 115200, int delayTime = 0) { std::string reallyPortName; std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}", portName); + std::this_thread::sleep_for(std::chrono::milliseconds(delayTime)); if (ser.isDeviceOpen()) return true; int code = ser.openDevice(reallyPortName.c_str(), bauds); @@ -122,14 +124,15 @@ class Serial { } template - std::expected DelayGetResponse(int delayTime, T command, - int timeout = 50) { + std::expected + DelayGetResponse(int delayTime, T command, int timeout = 50) { std::this_thread::sleep_for(std::chrono::milliseconds(delayTime)); return GetAtResponse(command, timeout); } template - std::expected GetAtResponse(T command, int timeout = 50) { + std::expected + GetAtResponse(T command, int timeout = 50) { ser.flushReceiver(); std::string reallyCommand; if constexpr (std::is_same_v) { @@ -166,8 +169,8 @@ class Serial { } } - template - bool GetAtUntil(T command, T expect = "OK", int timeout = 200) { + template + bool GetAtUntil(T command, Args... expect) { auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout); ser.flushReceiver(); @@ -189,7 +192,7 @@ class Serial { delete[] buffer; if (size > 0) Log("Receive: " + str); - if (str.find(expect) != std::string::npos) { + if (((str.find(expect) != std::string::npos) && ...)) { return true; } }