Compare commits
10 Commits
45a14b5d19
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a10173c5f | |||
| 01dba9107e | |||
| 10d47de9b8 | |||
| 74e5d380f5 | |||
| 6605f0604a | |||
| 6ec5ffd22f | |||
| 517c4699d4 | |||
| e0094441c2 | |||
| 478421ee07 | |||
| 1417770918 |
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
@@ -1,8 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
46
.idea/inspectionProfiles/Project_Default.xml
generated
46
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -1,46 +0,0 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredPackages">
|
||||
<value>
|
||||
<list size="24">
|
||||
<item index="0" class="java.lang.String" itemvalue="blinker" />
|
||||
<item index="1" class="java.lang.String" itemvalue="autopep8" />
|
||||
<item index="2" class="java.lang.String" itemvalue="Flask-Cors" />
|
||||
<item index="3" class="java.lang.String" itemvalue="Werkzeug" />
|
||||
<item index="4" class="java.lang.String" itemvalue="Flask-HTTPAuth" />
|
||||
<item index="5" class="java.lang.String" itemvalue="SQLAlchemy" />
|
||||
<item index="6" class="java.lang.String" itemvalue="cryptography" />
|
||||
<item index="7" class="java.lang.String" itemvalue="MarkupSafe" />
|
||||
<item index="8" class="java.lang.String" itemvalue="click" />
|
||||
<item index="9" class="java.lang.String" itemvalue="Flask-SQLAlchemy" />
|
||||
<item index="10" class="java.lang.String" itemvalue="casbin" />
|
||||
<item index="11" class="java.lang.String" itemvalue="pip-review" />
|
||||
<item index="12" class="java.lang.String" itemvalue="Authlib" />
|
||||
<item index="13" class="java.lang.String" itemvalue="filelock" />
|
||||
<item index="14" class="java.lang.String" itemvalue="platformdirs" />
|
||||
<item index="15" class="java.lang.String" itemvalue="certifi" />
|
||||
<item index="16" class="java.lang.String" itemvalue="virtualenv" />
|
||||
<item index="17" class="java.lang.String" itemvalue="charset-normalizer" />
|
||||
<item index="18" class="java.lang.String" itemvalue="casbin-sqlalchemy-adapter" />
|
||||
<item index="19" class="java.lang.String" itemvalue="gevent" />
|
||||
<item index="20" class="java.lang.String" itemvalue="Flask" />
|
||||
<item index="21" class="java.lang.String" itemvalue="typing_extensions" />
|
||||
<item index="22" class="java.lang.String" itemvalue="greenlet" />
|
||||
<item index="23" class="java.lang.String" itemvalue="Flask-APScheduler" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="ignoredErrors">
|
||||
<list>
|
||||
<option value="N803" />
|
||||
<option value="N802" />
|
||||
<option value="N806" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/serialt.iml" filepath="$PROJECT_DIR$/.idea/serialt.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
2
.idea/serialt.iml
generated
2
.idea/serialt.iml
generated
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CIDR" type="CPP_MODULE" version="4" />
|
||||
@@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
set(PROJECT_N demo)
|
||||
set(PROJECT_N serialt)
|
||||
project(${PROJECT_N} VERSION 1.0)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
460
include/serial.h
460
include/serial.h
@@ -1,18 +1,19 @@
|
||||
#ifndef SERIAL_H
|
||||
#define SERIAL_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <expected>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <ranges>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <expected>
|
||||
|
||||
#include "serialib.h"
|
||||
|
||||
@@ -21,180 +22,323 @@ namespace ranges = std::ranges;
|
||||
namespace views = std::views;
|
||||
|
||||
namespace serial {
|
||||
template <typename... Types> struct _StrongType {};
|
||||
|
||||
static std::vector<std::string> GetUsbPorts() {
|
||||
std::vector<std::string> portArray;
|
||||
std::string comname;
|
||||
std::string showname;
|
||||
ranges::for_each(views::iota(1, 256), [&](int i) {
|
||||
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
|
||||
std::format_to(std::back_inserter(showname), "COM{}", i);
|
||||
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> struct _StrongType<T> {
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
concept SupportString = requires {
|
||||
std::is_same_v<T, const char *>;
|
||||
std::is_same_v<T, std::string>;
|
||||
};
|
||||
template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
|
||||
using Type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
|
||||
};
|
||||
|
||||
enum class [[maybe_unused]] SerialErrorCode{
|
||||
SUCCESS,
|
||||
TIMEOUT,
|
||||
SETTIMEOUTERROR,
|
||||
WRITEINGERROR,
|
||||
READINGERROR,
|
||||
};
|
||||
template <typename T, typename... Types> struct _StrongType<T, Types...> {
|
||||
using Type =
|
||||
typename _StrongType<T, typename _StrongType<Types...>::Type>::Type;
|
||||
};
|
||||
|
||||
class Serial {
|
||||
private:
|
||||
serialib ser;
|
||||
const char *endChar = "\r\n";
|
||||
std::function<void(const std::string &)> logCallBack;
|
||||
bool removeEcho = true;
|
||||
template <typename... Types>
|
||||
using _strongType_t = typename _StrongType<Types...>::Type;
|
||||
|
||||
public:
|
||||
Serial() = default;
|
||||
~Serial() { CloseDevice(); }
|
||||
Serial(const Serial &other) = delete;
|
||||
Serial(Serial &&other) = delete;
|
||||
Serial &operator=(const Serial &other) = delete;
|
||||
Serial &operator=(Serial &&other) = delete;
|
||||
template <typename... Args> struct _IsCastable {};
|
||||
|
||||
auto SetRemoveEcho(bool remove) { removeEcho = remove; }
|
||||
template <typename T> struct _IsCastable<T> {
|
||||
static constexpr bool value = true;
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
static std::string GetTimeNow() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||
char buffer[32];
|
||||
auto _ = ctime_s(buffer, 32, &now_c);
|
||||
return buffer;
|
||||
}
|
||||
template <typename T, typename U> struct _IsCastable<T, U> {
|
||||
using __TRUE = char;
|
||||
using __FALSE = struct {
|
||||
char _[2];
|
||||
};
|
||||
static consteval __TRUE __TEST(U);
|
||||
static consteval __FALSE __TEST(...);
|
||||
static constexpr bool value =
|
||||
sizeof(__TEST(std::declval<T>())) == sizeof(__TRUE);
|
||||
using Type = std::conditional_t<value, U, void>;
|
||||
};
|
||||
|
||||
bool IsOpen() { return ser.isDeviceOpen(); }
|
||||
template <typename T, typename... Args> struct _IsCastable<T, Args...> {
|
||||
static constexpr bool value =
|
||||
_IsCastable<T, typename _IsCastable<Args...>::Type>::value;
|
||||
using Type =
|
||||
std::conditional_t<value, typename _IsCastable<Args...>::Type, void>;
|
||||
};
|
||||
|
||||
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 <typename T>
|
||||
concept _SupportString = requires {
|
||||
_IsCastable<T, const char*, char*, std::string, std::string_view>::value;
|
||||
};
|
||||
template <_SupportString T> [[maybe_unused]] std::string _to_string(T&& str) {
|
||||
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
|
||||
return str;
|
||||
}
|
||||
else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) {
|
||||
return std::move(std::string(str.data()));
|
||||
}
|
||||
else if constexpr (std::is_same_v<std::decay_t<T>, const char*>) {
|
||||
return std::string(str);
|
||||
}
|
||||
}
|
||||
|
||||
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 = ser.openDevice(reallyPortName.c_str(), bauds);
|
||||
if (code == 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
static std::vector<std::string> GetUsbPorts() {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
std::vector<std::string> portArray;
|
||||
std::string comname;
|
||||
std::string showname;
|
||||
ranges::for_each(views::iota(1, 256), [&](int i) {
|
||||
std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
|
||||
std::format_to(std::back_inserter(showname), "COM{}", i);
|
||||
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.emplace_back(showname);
|
||||
CloseHandle(m_handle);
|
||||
}else if(GetLastError() == ERROR_ACCESS_DENIED){
|
||||
portArray.emplace_back(showname);
|
||||
}
|
||||
comname.clear();
|
||||
showname.clear();
|
||||
});
|
||||
return portArray;
|
||||
#elif defined(__linux__)
|
||||
|
||||
void Log(const std::string &log) const {
|
||||
if (logCallBack) {
|
||||
auto msg = GetTimeNow() + " " + log;
|
||||
logCallBack(msg);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
SetLogCallBack(const std::function<void(const std::string &)> &callBack) {
|
||||
logCallBack = callBack;
|
||||
}
|
||||
void CloseDevice() {
|
||||
if (!ser.isDeviceOpen())
|
||||
return;
|
||||
ser.closeDevice();
|
||||
}
|
||||
enum class [[maybe_unused]] SerialErrorCode {
|
||||
SUCCESS,
|
||||
TIMEOUT,
|
||||
SETTIMEOUTERROR,
|
||||
WRITEINGERROR,
|
||||
READINGERROR,
|
||||
};
|
||||
|
||||
template <SupportString T>
|
||||
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);
|
||||
}
|
||||
enum SerialDataBits {
|
||||
DATABIT_5 = 0,
|
||||
DATABIT_6,
|
||||
DATABIT_7,
|
||||
DATABIT_8,
|
||||
DATABIT_16,
|
||||
};
|
||||
enum SerialStopBits {
|
||||
STOPBIT_1 = 0,
|
||||
STOPBIT_1_5,
|
||||
STOPBIT_2,
|
||||
};
|
||||
enum SerialParity {
|
||||
SERIAL_PARITY_NONE = 0,
|
||||
SERIAL_PARITY_EVEN,
|
||||
SERIAL_PARITY_ODD,
|
||||
SERIAL_PARITY_MARK,
|
||||
SERIAL_PARITY_SPACE,
|
||||
};
|
||||
|
||||
template <SupportString T>
|
||||
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>) {
|
||||
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);
|
||||
class Serial {
|
||||
private:
|
||||
serialib ser;
|
||||
const char* endChar = "\r\n";
|
||||
std::function<void(const std::string&)> logCallBack;
|
||||
bool removeEcho = true; // 剔除回显
|
||||
|
||||
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);
|
||||
}
|
||||
public:
|
||||
Serial() = default;
|
||||
~Serial() { CloseDevice(); }
|
||||
Serial(const Serial& other) = delete;
|
||||
Serial(Serial&& other) = delete;
|
||||
Serial& operator=(const Serial& other) = delete;
|
||||
Serial& operator=(Serial&& other) = delete;
|
||||
|
||||
template <SupportString T>
|
||||
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
|
||||
-> void {
|
||||
for (int i = 0; i <= repeatTime; i++) {
|
||||
auto _ = GetAtResponse(command, timeout);
|
||||
}
|
||||
}
|
||||
auto SetRemoveEcho(bool remove) { removeEcho = remove; }
|
||||
auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
|
||||
auto SetRts(bool flag) { return flag ? ser.setRTS() : ser.clearRTS(); }
|
||||
|
||||
template <SupportString T>
|
||||
bool GetAtUntil(T command, T expect = "OK", int timeout = 200) {
|
||||
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;
|
||||
static std::string GetTimeNow() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||
char buffer[32];
|
||||
auto _ = ctime_s(buffer, 32, &now_c);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool IsOpen() { return ser.isDeviceOpen(); }
|
||||
|
||||
template <_SupportString T>
|
||||
bool OpenDevice(T portName, unsigned int bauds = 115200, int delayTime = 0,
|
||||
SerialDataBits dataBits = SerialDataBits::DATABIT_8,
|
||||
SerialStopBits stopBits = SerialStopBits::STOPBIT_1,
|
||||
SerialParity parity = SerialParity::SERIAL_PARITY_NONE) {
|
||||
#if defined(_WIN32) || defined(__WIN64)
|
||||
std::string reallyPortName;
|
||||
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
|
||||
portName);
|
||||
#elif defined(__linux__)
|
||||
std::string reallyPortName = std::string(portName);
|
||||
#endif
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
||||
if (ser.isDeviceOpen())
|
||||
ser.closeDevice();
|
||||
|
||||
int code = ser.openDevice(reallyPortName.c_str(), bauds,
|
||||
static_cast<::SerialDataBits>(dataBits),
|
||||
static_cast<::SerialParity>(parity),
|
||||
static_cast<::SerialStopBits>(stopBits));
|
||||
if (code == 1) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Log(const std::string& log) const {
|
||||
if (logCallBack) {
|
||||
auto msg = GetTimeNow() + " " + log;
|
||||
logCallBack(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SetLogCallBack(const std::function<void(const std::string&)>& callBack) {
|
||||
logCallBack = callBack;
|
||||
}
|
||||
void CloseDevice() {
|
||||
if (!ser.isDeviceOpen())
|
||||
return;
|
||||
ser.closeDevice();
|
||||
}
|
||||
|
||||
template <_SupportString T>
|
||||
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 <int repeatTime = 5, int delayTime = 200, int timeout = 200,
|
||||
_SupportString T, _SupportString... Args>
|
||||
bool GetAtUntilRepeat(T command, Args... args) {
|
||||
std::stringstream ss;
|
||||
int i = 0;
|
||||
while (i++ < repeatTime) {
|
||||
ss.str("");
|
||||
ss << "Count: " << i << "\n";
|
||||
Log(ss.str());
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
|
||||
if (GetAtUntil<timeout>(command, args...))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <_SupportString T>
|
||||
std::expected<std::string, SerialErrorCode>
|
||||
GetAtResponse(T command, int timeout = 50) {
|
||||
ser.flushReceiver();
|
||||
std::string reallyCommand = std::string(command) + endChar;
|
||||
ser.writeString(reallyCommand.c_str());
|
||||
Log("Send: " + reallyCommand);
|
||||
std::this_thread::sleep_for(10ms);
|
||||
auto availableSize = ser.available();
|
||||
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.get());
|
||||
Log("Receive: " + response);
|
||||
if (removeEcho)
|
||||
response.replace(0, reallyCommand.length(), "");
|
||||
return response;
|
||||
}
|
||||
return std::unexpected(SerialErrorCode::TIMEOUT);
|
||||
}
|
||||
|
||||
template <_SupportString T>
|
||||
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1)
|
||||
-> void {
|
||||
for (int i = 0; i <= repeatTime; i++) {
|
||||
auto _ = GetAtResponse(command, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
std::string resp;
|
||||
std::string 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 = std::make_unique<char[]>(availableSize + 1);
|
||||
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
||||
buffer[size] = '\0';
|
||||
auto str = std::string(buffer.get());
|
||||
if (size > 0){
|
||||
Log("Receive: " + str);
|
||||
resp += str;
|
||||
}
|
||||
if (((str.find(expect) != std::string::npos) && ...)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<int timeout = 1000, _SupportString... Args>
|
||||
bool ReadUntil(Args... args){
|
||||
auto end_time = std::chrono::system_clock::now() + 1s;
|
||||
std::string resp;
|
||||
while(std::chrono::system_clock::now() < end_time){
|
||||
std::this_thread::sleep_for(10ms);
|
||||
auto available_size = ser.available();
|
||||
auto buffer = std::unique_ptr<char[]>{available_size + 1};
|
||||
auto read_size = ser.readBytes(buffer.get(), available_size, timeout);
|
||||
buffer[read_size] = '\0';
|
||||
auto str = std::string(buffer.get());
|
||||
resp += str;
|
||||
if(read_size > 0){
|
||||
Log("Read: " + str);
|
||||
}
|
||||
if(((resp.find(args) != std::string::npos) && ...)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template <int timeout = 200, _SupportString T, _SupportString... Args>
|
||||
std::string GetAtUntilAndReturn(T command, Args... expect)
|
||||
{
|
||||
auto endTime = std::chrono::system_clock::now() +
|
||||
std::chrono::milliseconds(timeout);
|
||||
ser.flushReceiver();
|
||||
std::string resp = "";
|
||||
std::string 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 = std::make_unique<char[]>(availableSize + 1);
|
||||
auto size = ser.readBytes(buffer.get(), availableSize, timeout);
|
||||
buffer[size] = '\0';
|
||||
auto str = std::string(buffer.get());
|
||||
resp += str;
|
||||
if (size > 0)
|
||||
Log("Receive: " + str);
|
||||
if (((resp.find(expect) != std::string::npos) && ...)) {
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
};
|
||||
} // namespace serial
|
||||
#endif // SERIAL_H
|
||||
|
||||
39
main.cc
39
main.cc
@@ -1,19 +1,30 @@
|
||||
#include "include/serial.h"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <ranges>
|
||||
#include <limits>
|
||||
#include <ranges>
|
||||
#include <string_view>
|
||||
|
||||
#include "include/serial.h"
|
||||
#undef max
|
||||
using namespace std::literals::chrono_literals;
|
||||
using namespace std::literals::string_view_literals;
|
||||
using namespace std::literals::string_literals;
|
||||
using namespace serial;
|
||||
namespace ranges = std::ranges;
|
||||
namespace views = std::views;
|
||||
|
||||
void PrintLog(const std::string &msg) { std::cout << msg << std::endl; }
|
||||
|
||||
template <typename T>
|
||||
requires std::is_same_v<T, std::string> ||
|
||||
std::is_same_v<T, std::string_view>
|
||||
std::vector<T> split(T str, T d) {
|
||||
auto v = views::split(str, d) | views::transform([](auto word) {
|
||||
return T(word.begin(), word.end());
|
||||
});
|
||||
return std::vector<T>(v.begin(), v.end());
|
||||
}
|
||||
int main(int argc, char **const argv) {
|
||||
Serial serial;
|
||||
auto ports = serial::GetUsbPorts();
|
||||
@@ -30,20 +41,20 @@ int main(int argc, char **const argv) {
|
||||
std::getline(std::cin, command);
|
||||
system("cls");
|
||||
auto startTime = std::chrono::system_clock::now();
|
||||
if (command.find(":") != std::string::npos) {
|
||||
command.erase(std::remove_if(command.begin(), command.end(),
|
||||
[](char c) { return c == ':'; }),
|
||||
command.end());
|
||||
auto reallyCommand = command.substr(0, command.find_first_of(' '));
|
||||
auto expect = command.substr(command.find_first_of(' ') + 1,
|
||||
command.length());
|
||||
auto res = serial.GetAtUntil(reallyCommand, expect, 2000);
|
||||
if (command.at(0) == ':') {
|
||||
command = command.substr(1, command.length() - 1);
|
||||
auto sp = split(command, " "s);
|
||||
auto reallyCommand = sp[0];
|
||||
auto expect = sp[1];
|
||||
auto res = serial.GetAtUntil(reallyCommand, 2000, expect);
|
||||
auto endTime = std::chrono::system_clock::now();
|
||||
std::cout << "dura: "
|
||||
res ? std::cout
|
||||
<< "dura: "
|
||||
<< std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
endTime - startTime)
|
||||
.count()
|
||||
<< std::endl;
|
||||
<< std::endl
|
||||
: std::cout << "Recive Error";
|
||||
} else {
|
||||
auto resp = serial.GetAtResponse(command);
|
||||
auto endTime = std::chrono::system_clock::now();
|
||||
@@ -56,4 +67,4 @@ int main(int argc, char **const argv) {
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user