From cea52092225afcd793deb1548e3f87dcb9b985b6 Mon Sep 17 00:00:00 2001 From: JIe Date: Thu, 7 Nov 2024 14:20:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ComKit.cc | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/ComKit.cc b/src/ComKit.cc index 8c510a8..257f494 100644 --- a/src/ComKit.cc +++ b/src/ComKit.cc @@ -138,26 +138,30 @@ void ComKit::Start(std::string watchingPort, std::string input_commands) { } void ComKit::Work() { if (isWorking) { - auto currentPortStructures = GetAllComPortNames(); - std::vector currentPorts; - for (auto s : *currentPortStructures) { - currentPorts.emplace_back(s.comPortName); - } - for (auto &port : watchingComPort) { - if (std::find(currentPorts.begin(), currentPorts.end(), port) != - currentPorts.end()) { - spdlog::info(std::format("Start Work With Port [{}]", port)); - device.OpenDevice(port); - for (auto &command : commands) { - spdlog::info(std::format("Send Command: {}", command)); - std::this_thread::sleep_for(50ms); - auto resp = device.GetAtUntilAndReturn(command, "OK"); - spdlog::info(std::format("resp: {}", resp)); - FlushLog(); - } - device.CloseDevice(); + std::thread([]() { + std::lock_guard lock(m); + auto currentPortStructures = GetAllComPortNames(); + std::vector currentPorts; + for (auto s : *currentPortStructures) { + currentPorts.emplace_back(s.comPortName); } - } + for (auto &port : watchingComPort) { + if (std::find(currentPorts.begin(), currentPorts.end(), port) != + currentPorts.end()) { + spdlog::info( + std::format("Start Work With Port [{}]", port)); + device.OpenDevice(port); + for (auto &command : commands) { + spdlog::info(std::format("Send Command: {}", command)); + std::this_thread::sleep_for(50ms); + auto resp = device.GetAtUntilAndReturn(command, "OK"); + spdlog::info(std::format("resp: {}", resp)); + FlushLog(); + } + device.CloseDevice(); + } + } + }).detach(); } }