多线程支持

This commit is contained in:
JIe 2024-11-07 14:20:44 +08:00
parent 2404e17d10
commit cea5209222

View File

@ -138,26 +138,30 @@ void ComKit::Start(std::string watchingPort, std::string input_commands) {
} }
void ComKit::Work() { void ComKit::Work() {
if (isWorking) { if (isWorking) {
auto currentPortStructures = GetAllComPortNames(); std::thread([]() {
std::vector<std::string> currentPorts; std::lock_guard<std::mutex> lock(m);
for (auto s : *currentPortStructures) { auto currentPortStructures = GetAllComPortNames();
currentPorts.emplace_back(s.comPortName); std::vector<std::string> currentPorts;
} for (auto s : *currentPortStructures) {
for (auto &port : watchingComPort) { currentPorts.emplace_back(s.comPortName);
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();
} }
} 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();
} }
} }