Compare commits

...

2 Commits

Author SHA1 Message Date
JIe
478421ee07 用unique_ptr 替代new 2024-10-09 17:11:40 +08:00
JIe
1417770918 优化Until函数 2024-10-09 17:04:41 +08:00

View File

@ -1,8 +1,10 @@
#ifndef SERIAL_H
#define SERIAL_H
#include <algorithm>
#include <chrono>
#include <cstring>
#include <expected>
#include <format>
#include <functional>
#include <iostream>
@ -12,7 +14,6 @@
#include <thread>
#include <type_traits>
#include <vector>
#include <expected>
#include "serialib.h"
@ -48,7 +49,7 @@ concept SupportString = requires {
std::is_same_v<T, std::string>;
};
enum class [[maybe_unused]] SerialErrorCode{
enum class [[maybe_unused]] SerialErrorCode {
SUCCESS,
TIMEOUT,
SETTIMEOUTERROR,
@ -83,17 +84,19 @@ class Serial {
bool IsOpen() { return ser.isDeviceOpen(); }
template<SupportString T>
bool OpenDeviceDelay(T portName, unsigned int baudRate, int delay){
template <SupportString T>
bool OpenDeviceDelay(T portName, unsigned int baudRate, int delay) {
std::this_thread::sleep_for(std::chrono::seconds(delay));
return OpenDevice(portName, baudRate);
}
template <SupportString T>
bool OpenDevice(T portName, unsigned int bauds = 115200) {
bool OpenDevice(T portName, unsigned int bauds = 115200,
int delayTime = 0) {
std::string reallyPortName;
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
portName);
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
if (ser.isDeviceOpen())
return true;
int code = ser.openDevice(reallyPortName.c_str(), bauds);
@ -122,14 +125,15 @@ class Serial {
}
template <SupportString T>
std::expected<std::string, SerialErrorCode> DelayGetResponse(int delayTime, T command,
int timeout = 50) {
std::expected<std::string, SerialErrorCode>
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::expected<std::string, SerialErrorCode> GetAtResponse(T command, int timeout = 50) {
std::expected<std::string, SerialErrorCode>
GetAtResponse(T command, int timeout = 50) {
ser.flushReceiver();
std::string reallyCommand;
if constexpr (std::is_same_v<T, std::string>) {
@ -141,20 +145,18 @@ class Serial {
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);
auto buffer = std::make_unique<char[]>(availableSize + 1);
std::memset(buffer.get(), 0, availableSize);
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
if (size > 0) {
buffer[size] = '\0';
std::string response = std::string(buffer);
Log("Receive: " + response);
delete[] buffer;
if (removeEcho)
response.replace(0, reallyCommand.length(), "");
return response;
}
delete[] buffer;
return std::unexpected(SerialErrorCode::TIMEOUT);
}
@ -166,8 +168,8 @@ class Serial {
}
}
template <SupportString T>
bool GetAtUntil(T command, T expect = "OK", int timeout = 200) {
template <int timeout = 200, SupportString T, SupportString... Args>
bool GetAtUntil(T command, Args... expect) {
auto endTime = std::chrono::system_clock::now() +
std::chrono::milliseconds(timeout);
ser.flushReceiver();
@ -182,14 +184,13 @@ class Serial {
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 buffer = std::make_unique<char[]>(availableSize + 1);
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
buffer[size] = '\0';
auto str = std::string(buffer);
delete[] buffer;
auto str = std::string(buffer.get());
if (size > 0)
Log("Receive: " + str);
if (str.find(expect) != std::string::npos) {
if (((str.find(expect) != std::string::npos) && ...)) {
return true;
}
}