实现串口监控功能,重构 ComKit::Work 方法以支持指定端口
This commit is contained in:
parent
cea5209222
commit
7316c326d0
@ -38,7 +38,7 @@ class ComKit {
|
|||||||
static std::vector<ComStruct> *GetAllComPortNames();
|
static std::vector<ComStruct> *GetAllComPortNames();
|
||||||
static void Start(std::string, std::string);
|
static void Start(std::string, std::string);
|
||||||
static void End();
|
static void End();
|
||||||
static void Work();
|
static void Work(const std::string& port_name);
|
||||||
static bool ChangeComPortNumber(const std::string &oldPortName,
|
static bool ChangeComPortNumber(const std::string &oldPortName,
|
||||||
const std::string &newPortName);
|
const std::string &newPortName);
|
||||||
static std::vector<std::string> watchingComPort;
|
static std::vector<std::string> watchingComPort;
|
||||||
|
28
include/ComWatch.h
Normal file
28
include/ComWatch.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef COMWATCH_H
|
||||||
|
#include "serial.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
enum class SIGNAL_TYPE{
|
||||||
|
REMOVE_,
|
||||||
|
ADD_,
|
||||||
|
};
|
||||||
|
|
||||||
|
class SerialWatch {
|
||||||
|
private:
|
||||||
|
std::function<void(const std::string&, SIGNAL_TYPE type)> callback;
|
||||||
|
std::jthread work_thread;
|
||||||
|
std::stop_source s;
|
||||||
|
std::mutex m;
|
||||||
|
public:
|
||||||
|
SerialWatch() = default;
|
||||||
|
~SerialWatch();
|
||||||
|
void set_callback(std::function<void(const std::string&, SIGNAL_TYPE type)> func);
|
||||||
|
void work();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // !COMWATCH_H
|
518
include/serial.h
518
include/serial.h
@ -22,300 +22,298 @@ namespace ranges = std::ranges;
|
|||||||
namespace views = std::views;
|
namespace views = std::views;
|
||||||
|
|
||||||
namespace serial {
|
namespace serial {
|
||||||
template <typename... Types> struct _StrongType {};
|
template <typename... Types> struct _StrongType {};
|
||||||
|
|
||||||
template <typename T> struct _StrongType<T> {
|
template <typename T> struct _StrongType<T> {
|
||||||
using Type = T;
|
using Type = T;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
|
template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
|
||||||
using Type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
|
using Type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename... Types> struct _StrongType<T, Types...> {
|
template <typename T, typename... Types> struct _StrongType<T, Types...> {
|
||||||
using Type =
|
using Type =
|
||||||
typename _StrongType<T, typename _StrongType<Types...>::Type>::Type;
|
typename _StrongType<T, typename _StrongType<Types...>::Type>::Type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename... Types>
|
template <typename... Types>
|
||||||
using _strongType_t = typename _StrongType<Types...>::Type;
|
using _strongType_t = typename _StrongType<Types...>::Type;
|
||||||
|
|
||||||
template <typename... Args> struct _IsCastable {};
|
template <typename... Args> struct _IsCastable {};
|
||||||
|
|
||||||
template <typename T> struct _IsCastable<T> {
|
template <typename T> struct _IsCastable<T> {
|
||||||
static constexpr bool value = true;
|
static constexpr bool value = true;
|
||||||
using Type = T;
|
using Type = T;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename U> struct _IsCastable<T, U> {
|
template <typename T, typename U> struct _IsCastable<T, U> {
|
||||||
using __TRUE = char;
|
using __TRUE = char;
|
||||||
using __FALSE = struct {
|
using __FALSE = struct {
|
||||||
char _[2];
|
char _[2];
|
||||||
};
|
};
|
||||||
static consteval __TRUE __TEST(U);
|
static consteval __TRUE __TEST(U);
|
||||||
static consteval __FALSE __TEST(...);
|
static consteval __FALSE __TEST(...);
|
||||||
static constexpr bool value =
|
static constexpr bool value =
|
||||||
sizeof(__TEST(std::declval<T>())) == sizeof(__TRUE);
|
sizeof(__TEST(std::declval<T>())) == sizeof(__TRUE);
|
||||||
using Type = std::conditional_t<value, U, void>;
|
using Type = std::conditional_t<value, U, void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename... Args> struct _IsCastable<T, Args...> {
|
template <typename T, typename... Args> struct _IsCastable<T, Args...> {
|
||||||
static constexpr bool value =
|
static constexpr bool value =
|
||||||
_IsCastable<T, typename _IsCastable<Args...>::Type>::value;
|
_IsCastable<T, typename _IsCastable<Args...>::Type>::value;
|
||||||
using Type =
|
using Type =
|
||||||
std::conditional_t<value, typename _IsCastable<Args...>::Type, void>;
|
std::conditional_t<value, typename _IsCastable<Args...>::Type, void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept _SupportString = requires {
|
concept _SupportString = requires {
|
||||||
_IsCastable<T, const char*, char*, std::string, std::string_view>::value;
|
_IsCastable<T, const char *, char *, std::string, std::string_view>::value;
|
||||||
};
|
};
|
||||||
template <_SupportString T> [[maybe_unused]] std::string _to_string(T&& str) {
|
template <_SupportString T> [[maybe_unused]] std::string _to_string(T &&str) {
|
||||||
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
|
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
|
||||||
return str;
|
return str;
|
||||||
}
|
} else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) {
|
||||||
else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) {
|
return std::move(std::string(str.data()));
|
||||||
return std::move(std::string(str.data()));
|
} else if constexpr (std::is_same_v<std::decay_t<T>, const char *>) {
|
||||||
}
|
return std::string(str);
|
||||||
else if constexpr (std::is_same_v<std::decay_t<T>, const char*>) {
|
}
|
||||||
return std::string(str);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::vector<std::string> GetUsbPorts() {
|
static std::vector<std::string> GetUsbPorts() {
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
std::vector<std::string> portArray;
|
std::vector<std::string> portArray;
|
||||||
std::string comname;
|
std::string comname;
|
||||||
std::string showname;
|
std::string showname;
|
||||||
ranges::for_each(views::iota(1, 256), [&](int i) {
|
ranges::for_each(views::iota(1, 256), [&](int i) {
|
||||||
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
|
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
|
||||||
std::format_to(std::back_inserter(showname), "COM{}", i);
|
std::format_to(std::back_inserter(showname), "COM{}", i);
|
||||||
const HANDLE m_handle = ::CreateFileA(
|
const HANDLE m_handle = ::CreateFileA(
|
||||||
comname.c_str(), static_cast<DWORD>(GENERIC_WRITE) | GENERIC_READ,
|
comname.c_str(), static_cast<DWORD>(GENERIC_WRITE) | GENERIC_READ,
|
||||||
0U, nullptr, OPEN_EXISTING, 0U, nullptr);
|
0U, nullptr, OPEN_EXISTING, 0U, nullptr);
|
||||||
if (m_handle != INVALID_HANDLE_VALUE) {
|
if (m_handle != INVALID_HANDLE_VALUE) {
|
||||||
portArray.push_back(showname);
|
portArray.emplace_back(showname);
|
||||||
CloseHandle(m_handle);
|
CloseHandle(m_handle);
|
||||||
}
|
} else if(GetLastError() == ERROR_ACCESS_DENIED){
|
||||||
comname.clear();
|
portArray.emplace_back(showname);
|
||||||
showname.clear();
|
}
|
||||||
});
|
comname.clear();
|
||||||
return portArray;
|
showname.clear();
|
||||||
|
});
|
||||||
|
return portArray;
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class [[maybe_unused]] SerialErrorCode {
|
enum class [[maybe_unused]] SerialErrorCode {
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
TIMEOUT,
|
TIMEOUT,
|
||||||
SETTIMEOUTERROR,
|
SETTIMEOUTERROR,
|
||||||
WRITEINGERROR,
|
WRITEINGERROR,
|
||||||
READINGERROR,
|
READINGERROR,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum SerialDataBits {
|
enum SerialDataBits {
|
||||||
DATABIT_5 = 0,
|
DATABIT_5 = 0,
|
||||||
DATABIT_6,
|
DATABIT_6,
|
||||||
DATABIT_7,
|
DATABIT_7,
|
||||||
DATABIT_8,
|
DATABIT_8,
|
||||||
DATABIT_16,
|
DATABIT_16,
|
||||||
};
|
};
|
||||||
enum SerialStopBits {
|
enum SerialStopBits {
|
||||||
STOPBIT_1 = 0,
|
STOPBIT_1 = 0,
|
||||||
STOPBIT_1_5,
|
STOPBIT_1_5,
|
||||||
STOPBIT_2,
|
STOPBIT_2,
|
||||||
};
|
};
|
||||||
enum SerialParity {
|
enum SerialParity {
|
||||||
SERIAL_PARITY_NONE = 0,
|
SERIAL_PARITY_NONE = 0,
|
||||||
SERIAL_PARITY_EVEN,
|
SERIAL_PARITY_EVEN,
|
||||||
SERIAL_PARITY_ODD,
|
SERIAL_PARITY_ODD,
|
||||||
SERIAL_PARITY_MARK,
|
SERIAL_PARITY_MARK,
|
||||||
SERIAL_PARITY_SPACE,
|
SERIAL_PARITY_SPACE,
|
||||||
};
|
};
|
||||||
|
|
||||||
class Serial {
|
class Serial {
|
||||||
private:
|
private:
|
||||||
serialib ser;
|
serialib ser;
|
||||||
const char* endChar = "\r\n";
|
const char *endChar = "\r\n";
|
||||||
std::function<void(const std::string&)> logCallBack;
|
std::function<void(const std::string &)> logCallBack;
|
||||||
bool removeEcho = true; // 剔除回显
|
bool removeEcho = true; // 剔除回显
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Serial() = default;
|
Serial() = default;
|
||||||
~Serial() { CloseDevice(); }
|
~Serial() { CloseDevice(); }
|
||||||
Serial(const Serial& other) = delete;
|
Serial(const Serial &other) = delete;
|
||||||
Serial(Serial&& other) = delete;
|
Serial(Serial &&other) = delete;
|
||||||
Serial& operator=(const Serial& other) = delete;
|
Serial &operator=(const Serial &other) = delete;
|
||||||
Serial& operator=(Serial&& other) = delete;
|
Serial &operator=(Serial &&other) = delete;
|
||||||
|
|
||||||
auto SetRemoveEcho(bool remove) { removeEcho = remove; }
|
auto SetRemoveEcho(bool remove) { removeEcho = remove; }
|
||||||
auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
|
auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
|
||||||
auto SetRts(bool flag) { return flag ? ser.setRTS() : ser.clearRTS(); }
|
auto SetRts(bool flag) { return flag ? ser.setRTS() : ser.clearRTS(); }
|
||||||
|
|
||||||
static std::string GetTimeNow() {
|
static std::string GetTimeNow() {
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
auto now_c = std::chrono::system_clock::to_time_t(now);
|
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||||
char buffer[32];
|
char buffer[32];
|
||||||
auto _ = ctime_s(buffer, 32, &now_c);
|
auto _ = ctime_s(buffer, 32, &now_c);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsOpen() { return ser.isDeviceOpen(); }
|
bool IsOpen() { return ser.isDeviceOpen(); }
|
||||||
|
|
||||||
template <_SupportString T>
|
template <_SupportString T>
|
||||||
bool OpenDevice(T portName, unsigned int bauds = 115200, int delayTime = 0,
|
bool OpenDevice(T portName, unsigned int bauds = 115200, int delayTime = 0,
|
||||||
SerialDataBits dataBits = SerialDataBits::DATABIT_8,
|
SerialDataBits dataBits = SerialDataBits::DATABIT_8,
|
||||||
SerialStopBits stopBits = SerialStopBits::STOPBIT_1,
|
SerialStopBits stopBits = SerialStopBits::STOPBIT_1,
|
||||||
SerialParity parity = SerialParity::SERIAL_PARITY_NONE) {
|
SerialParity parity = SerialParity::SERIAL_PARITY_NONE) {
|
||||||
#if defined(_WIN32) || defined(__WIN64)
|
#if defined(_WIN32) || defined(__WIN64)
|
||||||
std::string reallyPortName;
|
std::string reallyPortName;
|
||||||
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
|
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
|
||||||
portName);
|
portName);
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
std::string reallyPortName = std::string(portName);
|
std::string reallyPortName = std::string(portName);
|
||||||
#endif
|
#endif
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
||||||
if (ser.isDeviceOpen())
|
if (ser.isDeviceOpen())
|
||||||
ser.closeDevice();
|
ser.closeDevice();
|
||||||
|
|
||||||
int code = ser.openDevice(reallyPortName.c_str(), bauds,
|
int code = ser.openDevice(reallyPortName.c_str(), bauds,
|
||||||
static_cast<::SerialDataBits>(dataBits),
|
static_cast<::SerialDataBits>(dataBits),
|
||||||
static_cast<::SerialParity>(parity),
|
static_cast<::SerialParity>(parity),
|
||||||
static_cast<::SerialStopBits>(stopBits));
|
static_cast<::SerialStopBits>(stopBits));
|
||||||
if (code == 1) {
|
if (code == 1) {
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
else {
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Log(const std::string& log) const {
|
void Log(const std::string &log) const {
|
||||||
if (logCallBack) {
|
if (logCallBack) {
|
||||||
auto msg = GetTimeNow() + " " + log;
|
auto msg = GetTimeNow() + " " + log;
|
||||||
logCallBack(msg);
|
logCallBack(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
SetLogCallBack(const std::function<void(const std::string&)>& callBack) {
|
SetLogCallBack(const std::function<void(const std::string &)> &callBack) {
|
||||||
logCallBack = callBack;
|
logCallBack = callBack;
|
||||||
}
|
}
|
||||||
void CloseDevice() {
|
void CloseDevice() {
|
||||||
if (!ser.isDeviceOpen())
|
if (!ser.isDeviceOpen())
|
||||||
return;
|
return;
|
||||||
ser.closeDevice();
|
ser.closeDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <_SupportString T>
|
template <_SupportString T>
|
||||||
std::expected<std::string, SerialErrorCode>
|
std::expected<std::string, SerialErrorCode>
|
||||||
DelayGetResponse(int delayTime, T command, int timeout = 50) {
|
DelayGetResponse(int delayTime, T command, int timeout = 50) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
||||||
return GetAtResponse(command, timeout);
|
return GetAtResponse(command, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <int repeatTime = 5, int delayTime = 200, int timeout = 200,
|
template <int repeatTime = 5, int delayTime = 200, int timeout = 200,
|
||||||
_SupportString T, _SupportString... Args>
|
_SupportString T, _SupportString... Args>
|
||||||
bool GetAtUntilRepeat(T command, Args... args) {
|
bool GetAtUntilRepeat(T command, Args... args) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i++ < repeatTime) {
|
while (i++ < repeatTime) {
|
||||||
ss.str("");
|
ss.str("");
|
||||||
ss << "Count: " << i << "\n";
|
ss << "Count: " << i << "\n";
|
||||||
Log(ss.str());
|
Log(ss.str());
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
||||||
if (GetAtUntil<timeout>(command, args...))
|
if (GetAtUntil<timeout>(command, args...))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <_SupportString T>
|
template <_SupportString T>
|
||||||
std::expected<std::string, SerialErrorCode>
|
std::expected<std::string, SerialErrorCode>
|
||||||
GetAtResponse(T command, int timeout = 50) {
|
GetAtResponse(T command, int timeout = 50) {
|
||||||
ser.flushReceiver();
|
ser.flushReceiver();
|
||||||
std::string reallyCommand = std::string(command) + endChar;
|
std::string reallyCommand = std::string(command) + endChar;
|
||||||
ser.writeString(reallyCommand.c_str());
|
ser.writeString(reallyCommand.c_str());
|
||||||
Log("Send: " + reallyCommand);
|
Log("Send: " + reallyCommand);
|
||||||
std::this_thread::sleep_for(10ms);
|
std::this_thread::sleep_for(10ms);
|
||||||
auto availableSize = ser.available();
|
auto availableSize = ser.available();
|
||||||
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
||||||
std::memset(buffer.get(), 0, availableSize);
|
std::memset(buffer.get(), 0, availableSize);
|
||||||
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
buffer[size] = '\0';
|
buffer[size] = '\0';
|
||||||
std::string response = std::string(buffer.get());
|
std::string response = std::string(buffer.get());
|
||||||
Log("Receive: " + response);
|
Log("Receive: " + response);
|
||||||
if (removeEcho)
|
if (removeEcho)
|
||||||
response.replace(0, reallyCommand.length(), "");
|
response.replace(0, reallyCommand.length(), "");
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
return std::unexpected(SerialErrorCode::TIMEOUT);
|
return std::unexpected(SerialErrorCode::TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <_SupportString T>
|
template <_SupportString T>
|
||||||
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
|
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
|
||||||
-> void {
|
-> void {
|
||||||
for (int i = 0; i <= repeatTime; i++) {
|
for (int i = 0; i <= repeatTime; i++) {
|
||||||
auto _ = GetAtResponse(command, timeout);
|
auto _ = GetAtResponse(command, timeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <int timeout = 200, _SupportString T, _SupportString... Args>
|
template <int timeout = 200, _SupportString T, _SupportString... Args>
|
||||||
bool GetAtUntil(T command, Args... expect) {
|
bool GetAtUntil(T command, Args... expect) {
|
||||||
auto endTime = std::chrono::system_clock::now() +
|
auto endTime = std::chrono::system_clock::now() +
|
||||||
std::chrono::milliseconds(timeout);
|
std::chrono::milliseconds(timeout);
|
||||||
ser.flushReceiver();
|
ser.flushReceiver();
|
||||||
std::string resp;
|
std::string resp;
|
||||||
std::string reallyCommand = std::string(command) + endChar;
|
std::string reallyCommand = std::string(command) + endChar;
|
||||||
ser.writeString(reallyCommand.c_str());
|
ser.writeString(reallyCommand.c_str());
|
||||||
Log("Send : " + reallyCommand);
|
Log("Send : " + reallyCommand);
|
||||||
while (std::chrono::system_clock::now() < endTime) {
|
while (std::chrono::system_clock::now() < endTime) {
|
||||||
std::this_thread::sleep_for(10ms);
|
std::this_thread::sleep_for(10ms);
|
||||||
auto availableSize = ser.available();
|
auto availableSize = ser.available();
|
||||||
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
||||||
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
||||||
buffer[size] = '\0';
|
buffer[size] = '\0';
|
||||||
auto str = std::string(buffer.get());
|
auto str = std::string(buffer.get());
|
||||||
if (size > 0){
|
if (size > 0) {
|
||||||
Log("Receive: " + str);
|
Log("Receive: " + str);
|
||||||
resp += str;
|
resp += str;
|
||||||
}
|
}
|
||||||
if (((str.find(expect) != std::string::npos) && ...)) {
|
if (((str.find(expect) != std::string::npos) && ...)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <int timeout = 200, _SupportString T, _SupportString... Args>
|
template <int timeout = 200, _SupportString T, _SupportString... Args>
|
||||||
std::string GetAtUntilAndReturn(T command, Args... expect)
|
std::string GetAtUntilAndReturn(T command, Args... expect) {
|
||||||
{
|
auto endTime = std::chrono::system_clock::now() +
|
||||||
auto endTime = std::chrono::system_clock::now() +
|
std::chrono::milliseconds(timeout);
|
||||||
std::chrono::milliseconds(timeout);
|
ser.flushReceiver();
|
||||||
ser.flushReceiver();
|
std::string resp = "";
|
||||||
std::string resp = "";
|
std::string reallyCommand = std::string(command) + endChar;
|
||||||
std::string reallyCommand = std::string(command) + endChar;
|
ser.writeString(reallyCommand.c_str());
|
||||||
ser.writeString(reallyCommand.c_str());
|
Log("Send : " + reallyCommand);
|
||||||
Log("Send : " + reallyCommand);
|
while (std::chrono::system_clock::now() < endTime) {
|
||||||
while (std::chrono::system_clock::now() < endTime) {
|
std::this_thread::sleep_for(10ms);
|
||||||
std::this_thread::sleep_for(10ms);
|
auto availableSize = ser.available();
|
||||||
auto availableSize = ser.available();
|
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
||||||
auto buffer = std::make_unique<char[]>(availableSize + 1);
|
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
||||||
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
buffer[size] = '\0';
|
||||||
buffer[size] = '\0';
|
auto str = std::string(buffer.get());
|
||||||
auto str = std::string(buffer.get());
|
resp += str;
|
||||||
resp += str;
|
if (size > 0)
|
||||||
if (size > 0)
|
Log("Receive: " + str);
|
||||||
Log("Receive: " + str);
|
if (((resp.find(expect) != std::string::npos) && ...)) {
|
||||||
if (((resp.find(expect) != std::string::npos) && ...)) {
|
return resp;
|
||||||
return resp;
|
}
|
||||||
}
|
}
|
||||||
}
|
return resp;
|
||||||
return resp;
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
} // namespace serial
|
} // namespace serial
|
||||||
#endif // SERIAL_H
|
#endif // SERIAL_H
|
||||||
|
@ -136,32 +136,21 @@ void ComKit::Start(std::string watchingPort, std::string input_commands) {
|
|||||||
commands = std::move(Split(input_commands));
|
commands = std::move(Split(input_commands));
|
||||||
isWorking = true;
|
isWorking = true;
|
||||||
}
|
}
|
||||||
void ComKit::Work() {
|
void ComKit::Work(const std::string &port_name) {
|
||||||
if (isWorking) {
|
if (isWorking) {
|
||||||
std::thread([]() {
|
if (ranges::find(watchingComPort, port_name) != watchingComPort.end())
|
||||||
std::lock_guard<std::mutex> lock(m);
|
std::thread([&port_name]() {
|
||||||
auto currentPortStructures = GetAllComPortNames();
|
spdlog::info(std::format("start working with [{}]", port_name));
|
||||||
std::vector<std::string> currentPorts;
|
device.OpenDevice(port_name);
|
||||||
for (auto s : *currentPortStructures) {
|
for (auto &command : commands) {
|
||||||
currentPorts.emplace_back(s.comPortName);
|
spdlog::info(std::format("Send Command: {}", command));
|
||||||
}
|
std::this_thread::sleep_for(50ms);
|
||||||
for (auto &port : watchingComPort) {
|
auto resp = device.GetAtUntilAndReturn(command, "OK");
|
||||||
if (std::find(currentPorts.begin(), currentPorts.end(), port) !=
|
spdlog::info(std::format("resp: {}", resp));
|
||||||
currentPorts.end()) {
|
FlushLog();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
device.CloseDevice();
|
||||||
}).detach();
|
}).detach();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
45
src/ComWatch.cc
Normal file
45
src/ComWatch.cc
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include "ComWatch.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <ranges>
|
||||||
|
namespace ranges = std::ranges;
|
||||||
|
namespace views = std::ranges::views;
|
||||||
|
using namespace std::literals::chrono_literals;
|
||||||
|
SerialWatch::~SerialWatch() {
|
||||||
|
s.request_stop();
|
||||||
|
if (work_thread.joinable()) {
|
||||||
|
work_thread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SerialWatch::set_callback(
|
||||||
|
std::function<void(const std::string&, SIGNAL_TYPE type)> func) {
|
||||||
|
this->callback = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SerialWatch::work() {
|
||||||
|
this->work_thread = std::jthread(
|
||||||
|
[this](std::stop_token token) {
|
||||||
|
std::vector<std::string> pre_ports;
|
||||||
|
while (!token.stop_requested()) {
|
||||||
|
auto curr_ports = serial::GetUsbPorts();
|
||||||
|
for (auto &&port : pre_ports) {
|
||||||
|
if (ranges::find(curr_ports, port) == curr_ports.end()) {
|
||||||
|
std::lock_guard<std::mutex> lock(m);
|
||||||
|
if (callback)
|
||||||
|
callback(port, SIGNAL_TYPE::REMOVE_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto &&port : curr_ports) {
|
||||||
|
if (ranges::find(pre_ports, port) == pre_ports.end()) {
|
||||||
|
std::lock_guard<std::mutex> lock(m);
|
||||||
|
if (callback)
|
||||||
|
callback(port, SIGNAL_TYPE::ADD_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pre_ports = std::move(curr_ports);
|
||||||
|
std::this_thread::sleep_for(500ms);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
s.get_token());
|
||||||
|
}
|
20
src/main.cc
20
src/main.cc
@ -1,4 +1,5 @@
|
|||||||
#include "ComKit.h"
|
#include "ComKit.h"
|
||||||
|
#include "ComWatch.h"
|
||||||
#include "imgui_helper.h"
|
#include "imgui_helper.h"
|
||||||
#include "imgui_stdlib.h"
|
#include "imgui_stdlib.h"
|
||||||
#include "spdlog/sinks/basic_file_sink.h"
|
#include "spdlog/sinks/basic_file_sink.h"
|
||||||
@ -67,6 +68,25 @@ int main(int argc, char **) {
|
|||||||
filterComName = config.at("ComPorts").as_string();
|
filterComName = config.at("ComPorts").as_string();
|
||||||
inputCommands = config.at("Commands").as_string();
|
inputCommands = config.at("Commands").as_string();
|
||||||
}
|
}
|
||||||
|
SerialWatch watcher;
|
||||||
|
watcher.set_callback([](const std::string &port_name, SIGNAL_TYPE type) {
|
||||||
|
switch (type) {
|
||||||
|
case SIGNAL_TYPE::REMOVE_:
|
||||||
|
spdlog::log(spdlog::level::debug,
|
||||||
|
std::format("port: [{}] removed", port_name));
|
||||||
|
ComKit::FlushLog();
|
||||||
|
ComKit::GetAllComPortNames();
|
||||||
|
break;
|
||||||
|
case SIGNAL_TYPE::ADD_:
|
||||||
|
spdlog::log(spdlog::level::debug,
|
||||||
|
std::format("port: [{}] add", port_name));
|
||||||
|
ComKit::FlushLog();
|
||||||
|
ComKit::GetAllComPortNames();
|
||||||
|
ComKit::Work(port_name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
watcher.work();
|
||||||
while (isRunning)
|
while (isRunning)
|
||||||
helper.Render(
|
helper.Render(
|
||||||
[&]() {
|
[&]() {
|
||||||
|
@ -90,34 +90,7 @@ LRESULT ImGuiHelper::WndProc(HWND hWnd, UINT msg, WPARAM wParam,
|
|||||||
PDEV_BROADCAST_HDR pHdr = reinterpret_cast<PDEV_BROADCAST_HDR>(lParam);
|
PDEV_BROADCAST_HDR pHdr = reinterpret_cast<PDEV_BROADCAST_HDR>(lParam);
|
||||||
switch (wParam) {
|
switch (wParam) {
|
||||||
case DBT_DEVICEARRIVAL:
|
case DBT_DEVICEARRIVAL:
|
||||||
if (pHdr->dbch_devicetype == DBT_DEVTYP_PORT) {
|
|
||||||
PDEV_BROADCAST_PORT pPort =
|
|
||||||
reinterpret_cast<PDEV_BROADCAST_PORT>(pHdr);
|
|
||||||
spdlog::log(spdlog::level::debug,
|
|
||||||
std::format("port: [{}] add", pPort->dbcp_name));
|
|
||||||
ComKit::FlushLog();
|
|
||||||
ComKit::GetAllComPortNames();
|
|
||||||
ComKit::Work();
|
|
||||||
} else {
|
|
||||||
spdlog::log(spdlog::level::debug,
|
|
||||||
std::format("port: other device [{}] add",
|
|
||||||
pHdr->dbch_devicetype));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case DBT_DEVICEREMOVECOMPLETE:
|
case DBT_DEVICEREMOVECOMPLETE:
|
||||||
if (pHdr->dbch_devicetype == DBT_DEVTYP_PORT) {
|
|
||||||
PDEV_BROADCAST_PORT pPort =
|
|
||||||
reinterpret_cast<PDEV_BROADCAST_PORT>(pHdr);
|
|
||||||
spdlog::log(
|
|
||||||
spdlog::level::debug,
|
|
||||||
std::format("port: [{}] removed", pPort->dbcp_name));
|
|
||||||
ComKit::FlushLog();
|
|
||||||
ComKit::GetAllComPortNames();
|
|
||||||
} else {
|
|
||||||
spdlog::log(spdlog::level::debug,
|
|
||||||
std::format("port: other device [{}] add",
|
|
||||||
pHdr->dbch_devicetype));
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user