优化逻辑, 添加获取所有串口函数
This commit is contained in:
parent
9d718d9d41
commit
95b6c44a3b
284
include/serial.h
284
include/serial.h
@ -9,137 +9,197 @@
|
|||||||
#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
|
|
||||||
[[maybe_unused]]
|
|
||||||
ErrorCode{
|
|
||||||
SUCCESS,
|
|
||||||
TIMEOUT,
|
|
||||||
SETTIMEOUTERROR,
|
|
||||||
WRITEINGERROR,
|
|
||||||
READINGERROR,
|
|
||||||
};
|
|
||||||
|
|
||||||
class Serial
|
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
serialib ser;
|
|
||||||
const char* endChar = "\r\n";
|
|
||||||
std::function<void(const std::string&)> logCallBack;
|
|
||||||
|
|
||||||
public:
|
static std::vector<std::string> GetUsbPorts()
|
||||||
Serial() = default;
|
|
||||||
|
|
||||||
std::string GetTimeNow(){
|
|
||||||
auto now = std::chrono::system_clock::now();
|
|
||||||
auto now_c = std::chrono::system_clock::to_time_t(now);
|
|
||||||
char buffer[32];
|
|
||||||
ctime_s(buffer, 32, &now_c);
|
|
||||||
return std::string(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<SupportString T>
|
|
||||||
bool OpenDevice(T PortName, unsigned int bauds)
|
|
||||||
{
|
{
|
||||||
int code;
|
std::vector<std::string> portArray;
|
||||||
if constexpr (std::is_same_v<T, std::string>){
|
std::string comname;
|
||||||
code = ser.openDevice(PortName.c_str(), bauds);
|
std::string showname;
|
||||||
}
|
for (int i = 0; i <= 256; i++)
|
||||||
else{
|
{
|
||||||
code = ser.openDevice(PortName, bauds);
|
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
|
||||||
}
|
std::format_to(std::back_inserter(showname), "COM{}", i);
|
||||||
if(code == 1){
|
// 创建或者打开一个文件或者I/O设备,如执行成功,则返回文件句柄 INVALID_HANDLE_VALUE 表示出错
|
||||||
return true;
|
const HANDLE m_handle =
|
||||||
}else{
|
::CreateFileA(comname.c_str(),
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log(const std::string& log){
|
template <typename T>
|
||||||
if(logCallBack){
|
concept SupportString = requires {
|
||||||
auto msg = GetTimeNow() + " "+ log + "\n";
|
std::is_same_v<T, const char *>;
|
||||||
logCallBack(msg);
|
std::is_same_v<T, std::string>;
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
void SetLogCallBack(std::function<void(const std::string&)> callBack){
|
enum class
|
||||||
logCallBack = callBack;
|
[[maybe_unused]] ErrorCode
|
||||||
}
|
{
|
||||||
|
SUCCESS,
|
||||||
|
TIMEOUT,
|
||||||
|
SETTIMEOUTERROR,
|
||||||
|
WRITEINGERROR,
|
||||||
|
READINGERROR,
|
||||||
|
};
|
||||||
|
|
||||||
void CloseDevice(){
|
class Serial
|
||||||
ser.closeDevice();
|
{
|
||||||
}
|
private:
|
||||||
|
serialib ser;
|
||||||
|
const char *endChar = "\r\n";
|
||||||
|
std::function<void(const std::string &)> logCallBack;
|
||||||
|
|
||||||
~Serial() = default;
|
public:
|
||||||
|
Serial() = default;
|
||||||
|
|
||||||
|
std::string GetTimeNow()
|
||||||
|
{
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||||
|
char buffer[32];
|
||||||
|
ctime_s(buffer, 32, &now_c);
|
||||||
|
return std::string(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
template<SupportString T>
|
bool IsOpen(){
|
||||||
std::optional<std::string> GetAtResponse(T command, int timeout = 50){
|
return ser.isDeviceOpen();
|
||||||
ser.flushReceiver();
|
|
||||||
std::string reallyCommand;
|
|
||||||
std::string response;
|
|
||||||
if constexpr (std::is_same_v<T, std::string>){
|
|
||||||
reallyCommand = command + endChar;
|
|
||||||
}
|
}
|
||||||
else{
|
|
||||||
reallyCommand = std::string(command) + endChar;
|
|
||||||
}
|
|
||||||
ser.writeString(reallyCommand.c_str());
|
|
||||||
Log("Send: " + reallyCommand);
|
|
||||||
std::this_thread::sleep_for(10ms);
|
|
||||||
// char buffer[ser.available()] = {0};
|
|
||||||
auto availableSize = ser.available();
|
|
||||||
char* buffer = new char[availableSize+1];
|
|
||||||
std::memset(buffer, 0, availableSize);
|
|
||||||
auto size = ser.readBytes(buffer, availableSize, timeout);
|
|
||||||
if(size > 0){
|
|
||||||
buffer[size] = '\0';
|
|
||||||
response = std::string(buffer);
|
|
||||||
Log("Receive: " + response);
|
|
||||||
delete[] buffer;
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
delete[] buffer;
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<SupportString T>
|
template <SupportString T>
|
||||||
bool GetAtUntil(T command, T expect = "OK",int timeout = 50){
|
bool OpenDevice(T portName, unsigned int bauds = 115200)
|
||||||
auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout);
|
{
|
||||||
ser.flushReceiver();
|
std::string reallyPortName;
|
||||||
std::string reallyCommand;
|
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}", portName);
|
||||||
if constexpr (std::is_same_v<T, std::string>){
|
if(ser.isDeviceOpen()) return true;
|
||||||
reallyCommand = command + endChar;
|
int code;
|
||||||
}
|
code = ser.openDevice(reallyPortName.c_str(), bauds);
|
||||||
else{
|
if (code == 1)
|
||||||
reallyCommand = std::string(command) + endChar;
|
{
|
||||||
}
|
|
||||||
ser.writeString(reallyCommand.c_str());
|
|
||||||
Log("Send : " + reallyCommand);
|
|
||||||
while(std::chrono::system_clock::now() < endTime){
|
|
||||||
std::this_thread::sleep_for(10ms);
|
|
||||||
auto availableSize = ser.available();
|
|
||||||
auto buffer = new char[availableSize+1];
|
|
||||||
auto size = ser.readBytes(buffer, availableSize, timeout);
|
|
||||||
auto str = std::string(buffer);
|
|
||||||
delete[] buffer;
|
|
||||||
if(size > 0)
|
|
||||||
Log("Receive: "+str);
|
|
||||||
if(str.find(expect) != std::string::npos){
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
void Log(const std::string &log)
|
||||||
|
{
|
||||||
|
if (logCallBack)
|
||||||
|
{
|
||||||
|
auto msg = GetTimeNow() + " " + log + "\n";
|
||||||
|
logCallBack(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetLogCallBack(std::function<void(const std::string &)> callBack)
|
||||||
|
{
|
||||||
|
logCallBack = callBack;
|
||||||
|
}
|
||||||
|
void CloseDevice()
|
||||||
|
{
|
||||||
|
if(!ser.isDeviceOpen()) return;
|
||||||
|
ser.closeDevice();
|
||||||
|
}
|
||||||
|
|
||||||
|
~Serial() = default;
|
||||||
|
|
||||||
|
template<SupportString T>
|
||||||
|
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();
|
||||||
|
std::string reallyCommand;
|
||||||
|
std::string response;
|
||||||
|
if constexpr (std::is_same_v<T, std::string>)
|
||||||
|
{
|
||||||
|
reallyCommand = command + endChar;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reallyCommand = std::string(command) + endChar;
|
||||||
|
}
|
||||||
|
ser.writeString(reallyCommand.c_str());
|
||||||
|
Log("Send: " + reallyCommand);
|
||||||
|
std::this_thread::sleep_for(10ms);
|
||||||
|
auto availableSize = ser.available();
|
||||||
|
char *buffer = new char[availableSize + 1];
|
||||||
|
std::memset(buffer, 0, availableSize);
|
||||||
|
auto size = ser.readBytes(buffer, availableSize, timeout);
|
||||||
|
if (size > 0)
|
||||||
|
{
|
||||||
|
buffer[size] = '\0';
|
||||||
|
response = std::string(buffer);
|
||||||
|
Log("Receive: " + response);
|
||||||
|
delete[] buffer;
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
delete[] buffer;
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <SupportString T>
|
||||||
|
bool GetAtUntil(T command, T expect = "OK", int timeout = 50)
|
||||||
|
{
|
||||||
|
auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout);
|
||||||
|
ser.flushReceiver();
|
||||||
|
std::string reallyCommand;
|
||||||
|
if constexpr (std::is_same_v<T, std::string>)
|
||||||
|
{
|
||||||
|
reallyCommand = command + endChar;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reallyCommand = std::string(command) + endChar;
|
||||||
|
}
|
||||||
|
ser.writeString(reallyCommand.c_str());
|
||||||
|
Log("Send : " + reallyCommand);
|
||||||
|
while (std::chrono::system_clock::now() < endTime)
|
||||||
|
{
|
||||||
|
std::this_thread::sleep_for(10ms);
|
||||||
|
auto availableSize = ser.available();
|
||||||
|
auto buffer = new char[availableSize + 1];
|
||||||
|
auto size = ser.readBytes(buffer, availableSize, timeout);
|
||||||
|
buffer[size] = '\0';
|
||||||
|
auto str = std::string(buffer);
|
||||||
|
delete[] buffer;
|
||||||
|
if (size > 0)
|
||||||
|
Log("Receive: " + str);
|
||||||
|
if (str.find(expect) != std::string::npos)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
#endif // SERIAL_H
|
#endif // SERIAL_H
|
||||||
|
Loading…
Reference in New Issue
Block a user