From 01dba9107e02f035fe8a7eb69eb4912630c9396e Mon Sep 17 00:00:00 2001 From: JIe Date: Thu, 5 Dec 2024 18:03:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=BB=E5=8F=96=E4=B8=B2?= =?UTF-8?q?=E5=8F=A3=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/serial.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/serial.h b/include/serial.h index 1745e57..364389a 100644 --- a/include/serial.h +++ b/include/serial.h @@ -292,6 +292,27 @@ namespace serial { return false; } + template + bool ReadUntil(Args... args){ + auto end_time = std::chrono::system_clock::now() + 1s; + std::string resp; + while(std::chrono::system_clock::now() < end_time){ + std::this_thread::sleep_for(10ms); + auto available_size = ser.available(); + auto buffer = std::unique_ptr{available_size + 1}; + auto read_size = ser.readBytes(buffer.get(), available_size, timeout); + buffer[read_size] = '\0'; + auto str = std::string(buffer.get()); + resp += str; + if(read_size > 0){ + Log("Read: " + str); + } + if(((resp.find(args) != std::string::npos) && ...)){ + return true; + } + } + } + template std::string GetAtUntilAndReturn(T command, Args... expect) {