优化逻辑, 添加获取所有串口函数

This commit is contained in:
JIe 2024-09-24 19:34:34 +08:00
parent 9d718d9d41
commit 95b6c44a3b

View File

@ -9,37 +9,74 @@
#include <optional> #include <optional>
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <vector>
#include <format>
#include "../third/serialib.h" #include "../third/serialib.h"
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
template<typename T > namespace serial
concept SupportString = requires{ {
std::is_same_v<T, const char*>;
std::is_same_v<T, std::string>;
};
enum class static std::vector<std::string> GetUsbPorts()
[[maybe_unused]] {
ErrorCode{ std::vector<std::string> portArray;
std::string comname;
std::string showname;
for (int i = 0; i <= 256; i++)
{
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
std::format_to(std::back_inserter(showname), "COM{}", i);
// 创建或者打开一个文件或者I/O设备如执行成功则返回文件句柄 INVALID_HANDLE_VALUE 表示出错
const HANDLE m_handle =
::CreateFileA(comname.c_str(),
static_cast<DWORD>(GENERIC_WRITE) | GENERIC_READ,
0U,
nullptr,
OPEN_EXISTING,
0U,
nullptr
);
if (m_handle != INVALID_HANDLE_VALUE)
{
portArray.push_back(showname);
CloseHandle(m_handle);
}
comname.clear();
showname.clear();
}
return portArray;
}
template <typename T>
concept SupportString = requires {
std::is_same_v<T, const char *>;
std::is_same_v<T, std::string>;
};
enum class
[[maybe_unused]] ErrorCode
{
SUCCESS, SUCCESS,
TIMEOUT, TIMEOUT,
SETTIMEOUTERROR, SETTIMEOUTERROR,
WRITEINGERROR, WRITEINGERROR,
READINGERROR, READINGERROR,
}; };
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;
public: public:
Serial() = default; Serial() = default;
std::string GetTimeNow(){ 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];
@ -47,61 +84,78 @@ public:
return std::string(buffer); return std::string(buffer);
} }
template<SupportString T> bool IsOpen(){
bool OpenDevice(T PortName, unsigned int bauds) return ser.isDeviceOpen();
}
template <SupportString T>
bool OpenDevice(T portName, unsigned int bauds = 115200)
{ {
std::string reallyPortName;
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}", portName);
if(ser.isDeviceOpen()) return true;
int code; int code;
if constexpr (std::is_same_v<T, std::string>){ code = ser.openDevice(reallyPortName.c_str(), bauds);
code = ser.openDevice(PortName.c_str(), bauds); if (code == 1)
} {
else{
code = ser.openDevice(PortName, bauds);
}
if(code == 1){
return true; return true;
}else{ }
else
{
return false; return false;
} }
} }
void Log(const std::string& log){ void Log(const std::string &log)
if(logCallBack){ {
auto msg = GetTimeNow() + " "+ log + "\n"; if (logCallBack)
{
auto msg = GetTimeNow() + " " + log + "\n";
logCallBack(msg); logCallBack(msg);
} }
} }
void SetLogCallBack(std::function<void(const std::string&)> callBack){ void SetLogCallBack(std::function<void(const std::string &)> callBack)
{
logCallBack = callBack; logCallBack = callBack;
} }
void CloseDevice()
void CloseDevice(){ {
if(!ser.isDeviceOpen()) return;
ser.closeDevice(); ser.closeDevice();
} }
~Serial() = default; ~Serial() = default;
template<SupportString T> template<SupportString T>
std::optional<std::string> GetAtResponse(T command, int timeout = 50){ std::optional<std::string> DelayGetResponse(int delayTime, T command, int timeout = 50){
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
return GetAtResponse(command, timeout);
}
template <SupportString T>
std::optional<std::string> GetAtResponse(T command, int timeout = 50)
{
ser.flushReceiver(); ser.flushReceiver();
std::string reallyCommand; std::string reallyCommand;
std::string response; std::string response;
if constexpr (std::is_same_v<T, std::string>){ if constexpr (std::is_same_v<T, std::string>)
{
reallyCommand = command + endChar; reallyCommand = command + endChar;
} }
else{ else
{
reallyCommand = std::string(command) + endChar; 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);
// char buffer[ser.available()] = {0};
auto availableSize = ser.available(); auto availableSize = ser.available();
char* buffer = new char[availableSize+1]; char *buffer = new char[availableSize + 1];
std::memset(buffer, 0, availableSize); std::memset(buffer, 0, availableSize);
auto size = ser.readBytes(buffer, availableSize, timeout); auto size = ser.readBytes(buffer, availableSize, timeout);
if(size > 0){ if (size > 0)
{
buffer[size] = '\0'; buffer[size] = '\0';
response = std::string(buffer); response = std::string(buffer);
Log("Receive: " + response); Log("Receive: " + response);
@ -112,34 +166,40 @@ public:
return std::nullopt; return std::nullopt;
} }
template<SupportString T> template <SupportString T>
bool GetAtUntil(T command, T expect = "OK",int timeout = 50){ bool GetAtUntil(T command, T expect = "OK", int timeout = 50)
{
auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout); auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout);
ser.flushReceiver(); ser.flushReceiver();
std::string reallyCommand; std::string reallyCommand;
if constexpr (std::is_same_v<T, std::string>){ if constexpr (std::is_same_v<T, std::string>)
{
reallyCommand = command + endChar; reallyCommand = command + endChar;
} }
else{ else
{
reallyCommand = std::string(command) + endChar; 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 = new char[availableSize+1]; auto buffer = new char[availableSize + 1];
auto size = ser.readBytes(buffer, availableSize, timeout); auto size = ser.readBytes(buffer, availableSize, timeout);
buffer[size] = '\0';
auto str = std::string(buffer); auto str = std::string(buffer);
delete[] buffer; delete[] buffer;
if(size > 0) if (size > 0)
Log("Receive: "+str); Log("Receive: " + str);
if(str.find(expect) != std::string::npos){ if (str.find(expect) != std::string::npos)
{
return true; return true;
} }
} }
return false; return false;
} }
}; };
}
#endif // SERIAL_H #endif // SERIAL_H