Compare commits
No commits in common. "478421ee0750d0a74d33f0c6ec9c99a2d30bfc99" and "45a14b5d191122d4a5ba458441871fe3a717de7d" have entirely different histories.
478421ee07
...
45a14b5d19
@ -1,10 +1,8 @@
|
|||||||
#ifndef SERIAL_H
|
#ifndef SERIAL_H
|
||||||
#define SERIAL_H
|
#define SERIAL_H
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <expected>
|
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -14,6 +12,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <expected>
|
||||||
|
|
||||||
#include "serialib.h"
|
#include "serialib.h"
|
||||||
|
|
||||||
@ -49,7 +48,7 @@ concept SupportString = requires {
|
|||||||
std::is_same_v<T, std::string>;
|
std::is_same_v<T, std::string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class [[maybe_unused]] SerialErrorCode {
|
enum class [[maybe_unused]] SerialErrorCode{
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
TIMEOUT,
|
TIMEOUT,
|
||||||
SETTIMEOUTERROR,
|
SETTIMEOUTERROR,
|
||||||
@ -84,19 +83,17 @@ class Serial {
|
|||||||
|
|
||||||
bool IsOpen() { return ser.isDeviceOpen(); }
|
bool IsOpen() { return ser.isDeviceOpen(); }
|
||||||
|
|
||||||
template <SupportString T>
|
template<SupportString T>
|
||||||
bool OpenDeviceDelay(T portName, unsigned int baudRate, int delay) {
|
bool OpenDeviceDelay(T portName, unsigned int baudRate, int delay){
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(delay));
|
std::this_thread::sleep_for(std::chrono::seconds(delay));
|
||||||
return OpenDevice(portName, baudRate);
|
return OpenDevice(portName, baudRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <SupportString T>
|
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::string reallyPortName;
|
||||||
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
|
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
|
||||||
portName);
|
portName);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
|
||||||
if (ser.isDeviceOpen())
|
if (ser.isDeviceOpen())
|
||||||
return true;
|
return true;
|
||||||
int code = ser.openDevice(reallyPortName.c_str(), bauds);
|
int code = ser.openDevice(reallyPortName.c_str(), bauds);
|
||||||
@ -125,15 +122,14 @@ class Serial {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <SupportString T>
|
template <SupportString T>
|
||||||
std::expected<std::string, SerialErrorCode>
|
std::expected<std::string, SerialErrorCode> DelayGetResponse(int delayTime, T command,
|
||||||
DelayGetResponse(int delayTime, T command, int timeout = 50) {
|
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 <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 reallyCommand;
|
||||||
if constexpr (std::is_same_v<T, std::string>) {
|
if constexpr (std::is_same_v<T, std::string>) {
|
||||||
@ -145,18 +141,20 @@ class Serial {
|
|||||||
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);
|
char *buffer = new char[availableSize + 1];
|
||||||
std::memset(buffer.get(), 0, availableSize);
|
std::memset(buffer, 0, availableSize);
|
||||||
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
auto size = ser.readBytes(buffer, availableSize, timeout);
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
buffer[size] = '\0';
|
buffer[size] = '\0';
|
||||||
std::string response = std::string(buffer);
|
std::string response = std::string(buffer);
|
||||||
Log("Receive: " + response);
|
Log("Receive: " + response);
|
||||||
|
delete[] buffer;
|
||||||
if (removeEcho)
|
if (removeEcho)
|
||||||
response.replace(0, reallyCommand.length(), "");
|
response.replace(0, reallyCommand.length(), "");
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
delete[] buffer;
|
||||||
return std::unexpected(SerialErrorCode::TIMEOUT);
|
return std::unexpected(SerialErrorCode::TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,8 +166,8 @@ class Serial {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <int timeout = 200, SupportString T, SupportString... Args>
|
template <SupportString T>
|
||||||
bool GetAtUntil(T command, Args... expect) {
|
bool GetAtUntil(T command, T expect = "OK", int timeout = 200) {
|
||||||
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();
|
||||||
@ -184,13 +182,14 @@ class Serial {
|
|||||||
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 = new char[availableSize + 1];
|
||||||
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
auto size = ser.readBytes(buffer, availableSize, timeout);
|
||||||
buffer[size] = '\0';
|
buffer[size] = '\0';
|
||||||
auto str = std::string(buffer.get());
|
auto str = std::string(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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user