多线程支持

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() {
if (isWorking) {
auto currentPortStructures = GetAllComPortNames();
std::vector<std::string> 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<std::mutex> lock(m);
auto currentPortStructures = GetAllComPortNames();
std::vector<std::string> 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();
}
}