init
This commit is contained in:
140
include/serial.h
Normal file
140
include/serial.h
Normal file
@@ -0,0 +1,140 @@
|
||||
#ifndef SERIAL_H
|
||||
#define SERIAL_H
|
||||
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include "../third/serialib.h"
|
||||
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
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,
|
||||
TIMEOUT,
|
||||
SETTIMEOUTERROR,
|
||||
WRITEINGERROR,
|
||||
READINGERROR,
|
||||
};
|
||||
|
||||
class Serial
|
||||
{
|
||||
private:
|
||||
serialib ser;
|
||||
const char* endChar = "\r\n";
|
||||
std::function<void(const std::string&)> logCallBack;
|
||||
|
||||
public:
|
||||
Serial() = default;
|
||||
|
||||
std::string GetTimeNow(){
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||
return std::ctime(&now_c);
|
||||
}
|
||||
|
||||
template<SupportString T>
|
||||
bool OpenDevice(T PortName, unsigned int bauds)
|
||||
{
|
||||
int code;
|
||||
if constexpr (std::is_same_v<T, std::string>){
|
||||
code = ser.openDevice(PortName.c_str(), bauds);
|
||||
}
|
||||
else{
|
||||
code = ser.openDevice(PortName, bauds);
|
||||
}
|
||||
if(code == 1){
|
||||
return true;
|
||||
}else{
|
||||
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(){
|
||||
ser.closeDevice();
|
||||
}
|
||||
|
||||
~Serial() = default;
|
||||
|
||||
|
||||
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);
|
||||
// char buffer[ser.available()] = {0};
|
||||
char* buffer = (char*)malloc(sizeof(char)*ser.available());
|
||||
std::cout<<sizeof(buffer)<<std::endl;
|
||||
auto size = ser.readBytes(buffer, sizeof(buffer), timeout);
|
||||
if(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 buffer = new char[ser.available()];
|
||||
auto size = ser.readBytes(buffer, sizeof(buffer), timeout);
|
||||
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
|
||||
Reference in New Issue
Block a user