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(); } }