From e0094441c211ca0c9504fa1587aa2075b139f81f Mon Sep 17 00:00:00 2001 From: JIe Date: Thu, 10 Oct 2024 10:52:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=91=BD=E5=90=8D=E4=BF=AE?= =?UTF-8?q?=E6=94=B9,=20=E6=B7=BB=E5=8A=A0=E6=8C=89=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=8F=91=E9=80=81=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- include/serial.h | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 195c52d..445f825 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.26) -set(PROJECT_N demo) +set(PROJECT_N serialt) project(${PROJECT_N} VERSION 1.0) set(CMAKE_CXX_STANDARD 23) diff --git a/include/serial.h b/include/serial.h index b4e2abe..3e34f07 100644 --- a/include/serial.h +++ b/include/serial.h @@ -3,12 +3,12 @@ #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -62,7 +62,7 @@ class Serial { serialib ser; const char *endChar = "\r\n"; std::function logCallBack; - bool removeEcho = true; + bool removeEcho = true; // 剔除回显 public: Serial() = default; @@ -84,12 +84,6 @@ class Serial { bool IsOpen() { return ser.isDeviceOpen(); } - 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, int delayTime = 0) { @@ -131,6 +125,22 @@ class Serial { return GetAtResponse(command, timeout); } + template + 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(command, args...)) + return true; + } + return false; + } + template std::expected GetAtResponse(T command, int timeout = 50) {