Compare commits

...

8 Commits

Author SHA1 Message Date
JIe
4a10173c5f remove .idea 2025-01-15 11:29:35 +08:00
JIe
01dba9107e 添加读取串口函数 2024-12-05 18:03:20 +08:00
jie
10d47de9b8 修复串口扫描函数无法扫描到已经被打开的串口的问题 2024-11-07 17:24:38 +08:00
Jie
74e5d380f5 优化Until逻辑 2024-11-01 16:26:51 +08:00
JIe
6605f0604a 添加相关函数 2024-11-01 15:22:42 +08:00
JIe
6ec5ffd22f 添加数据位, 奇偶校验, 停止位设置 2024-10-12 14:46:49 +08:00
JIe
517c4699d4 优化约束 2024-10-10 15:59:53 +08:00
JIe
e0094441c2 项目命名修改, 添加按次数重复发送函数 2024-10-10 10:52:45 +08:00
8 changed files with 326 additions and 236 deletions

8
.idea/.gitignore generated vendored
View File

@@ -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

View File

@@ -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
View File

@@ -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
View File

@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CIDR" type="CPP_MODULE" version="4" />

View File

@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.26) cmake_minimum_required(VERSION 3.26)
set(PROJECT_N demo) set(PROJECT_N serialt)
project(${PROJECT_N} VERSION 1.0) project(${PROJECT_N} VERSION 1.0)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)

BIN
demo.exe

Binary file not shown.

View File

@@ -3,12 +3,12 @@
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <cstring>
#include <expected> #include <expected>
#include <format> #include <format>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <ranges> #include <ranges>
#include <sstream>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <thread> #include <thread>
@@ -22,180 +22,323 @@ namespace ranges = std::ranges;
namespace views = std::views; namespace views = std::views;
namespace serial { namespace serial {
template <typename... Types> struct _StrongType {};
static std::vector<std::string> GetUsbPorts() { template <typename T> struct _StrongType<T> {
std::vector<std::string> portArray; using Type = T;
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> template <typename Ta, typename Tb> struct _StrongType<Ta, Tb> {
concept SupportString = requires { using Type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
std::is_same_v<T, const char *>; };
std::is_same_v<T, std::string>;
};
enum class [[maybe_unused]] SerialErrorCode { template <typename T, typename... Types> struct _StrongType<T, Types...> {
SUCCESS, using Type =
TIMEOUT, typename _StrongType<T, typename _StrongType<Types...>::Type>::Type;
SETTIMEOUTERROR, };
WRITEINGERROR,
READINGERROR,
};
class Serial { template <typename... Types>
private: using _strongType_t = typename _StrongType<Types...>::Type;
serialib ser;
const char *endChar = "\r\n";
std::function<void(const std::string &)> logCallBack;
bool removeEcho = true;
public: template <typename... Args> struct _IsCastable {};
Serial() = default;
~Serial() { CloseDevice(); }
Serial(const Serial &other) = delete;
Serial(Serial &&other) = delete;
Serial &operator=(const Serial &other) = delete;
Serial &operator=(Serial &&other) = delete;
auto SetRemoveEcho(bool remove) { removeEcho = remove; } template <typename T> struct _IsCastable<T> {
static constexpr bool value = true;
using Type = T;
};
static std::string GetTimeNow() { template <typename T, typename U> struct _IsCastable<T, U> {
auto now = std::chrono::system_clock::now(); using __TRUE = char;
auto now_c = std::chrono::system_clock::to_time_t(now); using __FALSE = struct {
char buffer[32]; char _[2];
auto _ = ctime_s(buffer, 32, &now_c); };
return buffer; 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> template <typename T>
bool OpenDeviceDelay(T portName, unsigned int baudRate, int delay) { concept _SupportString = requires {
std::this_thread::sleep_for(std::chrono::seconds(delay)); _IsCastable<T, const char*, char*, std::string, std::string_view>::value;
return OpenDevice(portName, baudRate); };
} 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> static std::vector<std::string> GetUsbPorts() {
bool OpenDevice(T portName, unsigned int bauds = 115200, #if defined(_WIN32) || defined(_WIN64)
int delayTime = 0) { std::vector<std::string> portArray;
std::string reallyPortName; std::string comname;
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}", std::string showname;
portName); ranges::for_each(views::iota(1, 256), [&](int i) {
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime)); std::format_to(std::back_inserter(comname), "\\\\.\\COM{}", i);
if (ser.isDeviceOpen()) std::format_to(std::back_inserter(showname), "COM{}", i);
return true; const HANDLE m_handle = ::CreateFileA(
int code = ser.openDevice(reallyPortName.c_str(), bauds); comname.c_str(), static_cast<DWORD>(GENERIC_WRITE) | GENERIC_READ,
if (code == 1) { 0U, nullptr, OPEN_EXISTING, 0U, nullptr);
return true; if (m_handle != INVALID_HANDLE_VALUE) {
} else { portArray.emplace_back(showname);
return false; 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 { #endif
if (logCallBack) { }
auto msg = GetTimeNow() + " " + log;
logCallBack(msg);
}
}
void enum class [[maybe_unused]] SerialErrorCode {
SetLogCallBack(const std::function<void(const std::string &)> &callBack) { SUCCESS,
logCallBack = callBack; TIMEOUT,
} SETTIMEOUTERROR,
void CloseDevice() { WRITEINGERROR,
if (!ser.isDeviceOpen()) READINGERROR,
return; };
ser.closeDevice();
}
template <SupportString T> enum SerialDataBits {
std::expected<std::string, SerialErrorCode> DATABIT_5 = 0,
DelayGetResponse(int delayTime, T command, int timeout = 50) { DATABIT_6,
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime)); DATABIT_7,
return GetAtResponse(command, timeout); 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> class Serial {
std::expected<std::string, SerialErrorCode> private:
GetAtResponse(T command, int timeout = 50) { serialib ser;
ser.flushReceiver(); const char* endChar = "\r\n";
std::string reallyCommand; std::function<void(const std::string&)> logCallBack;
if constexpr (std::is_same_v<T, std::string>) { bool removeEcho = true; // 剔除回显
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();
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) { public:
buffer[size] = '\0'; Serial() = default;
std::string response = std::string(buffer); ~Serial() { CloseDevice(); }
Log("Receive: " + response); Serial(const Serial& other) = delete;
if (removeEcho) Serial(Serial&& other) = delete;
response.replace(0, reallyCommand.length(), ""); Serial& operator=(const Serial& other) = delete;
return response; Serial& operator=(Serial&& other) = delete;
}
return std::unexpected(SerialErrorCode::TIMEOUT);
}
template <SupportString T> auto SetRemoveEcho(bool remove) { removeEcho = remove; }
auto GetAtResponseRepeat(T command, int timeout = 200, int repeatTime = 1) auto SetDtr(bool flag) { return flag ? ser.setDTR() : ser.clearDTR(); }
-> void { auto SetRts(bool flag) { return flag ? ser.setRTS() : ser.clearRTS(); }
for (int i = 0; i <= repeatTime; i++) {
auto _ = GetAtResponse(command, timeout);
}
}
template <int timeout = 200, SupportString T, SupportString... Args> static std::string GetTimeNow() {
bool GetAtUntil(T command, Args... expect) { auto now = std::chrono::system_clock::now();
auto endTime = std::chrono::system_clock::now() + auto now_c = std::chrono::system_clock::to_time_t(now);
std::chrono::milliseconds(timeout); char buffer[32];
ser.flushReceiver(); auto _ = ctime_s(buffer, 32, &now_c);
std::string reallyCommand; return buffer;
if constexpr (std::is_same_v<T, std::string>) { }
reallyCommand = command + endChar;
} else { bool IsOpen() { return ser.isDeviceOpen(); }
reallyCommand = std::string(command) + endChar;
} template <_SupportString T>
ser.writeString(reallyCommand.c_str()); bool OpenDevice(T portName, unsigned int bauds = 115200, int delayTime = 0,
Log("Send : " + reallyCommand); SerialDataBits dataBits = SerialDataBits::DATABIT_8,
while (std::chrono::system_clock::now() < endTime) { SerialStopBits stopBits = SerialStopBits::STOPBIT_1,
std::this_thread::sleep_for(10ms); SerialParity parity = SerialParity::SERIAL_PARITY_NONE) {
auto availableSize = ser.available(); #if defined(_WIN32) || defined(__WIN64)
auto buffer = std::make_unique<char[]>(availableSize + 1); std::string reallyPortName;
auto size = ser.readBytes(buffer.get(), availableSize, timeout); std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
buffer[size] = '\0'; portName);
auto str = std::string(buffer.get()); #elif defined(__linux__)
if (size > 0) std::string reallyPortName = std::string(portName);
Log("Receive: " + str); #endif
if (((str.find(expect) != std::string::npos) && ...)) { std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
return true; 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 } // namespace serial
#endif // SERIAL_H #endif // SERIAL_H

39
main.cc
View File

@@ -1,19 +1,30 @@
#include "include/serial.h"
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <cstdio>
#include <iostream> #include <iostream>
#include <ranges>
#include <limits> #include <limits>
#include <ranges>
#include <string_view> #include <string_view>
#include "include/serial.h"
#undef max #undef max
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
using namespace std::literals::string_view_literals;
using namespace std::literals::string_literals;
using namespace serial; using namespace serial;
namespace ranges = std::ranges; namespace ranges = std::ranges;
namespace views = std::views; namespace views = std::views;
void PrintLog(const std::string &msg) { std::cout << msg << std::endl; } 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) { int main(int argc, char **const argv) {
Serial serial; Serial serial;
auto ports = serial::GetUsbPorts(); auto ports = serial::GetUsbPorts();
@@ -30,20 +41,20 @@ int main(int argc, char **const argv) {
std::getline(std::cin, command); std::getline(std::cin, command);
system("cls"); system("cls");
auto startTime = std::chrono::system_clock::now(); auto startTime = std::chrono::system_clock::now();
if (command.find(":") != std::string::npos) { if (command.at(0) == ':') {
command.erase(std::remove_if(command.begin(), command.end(), command = command.substr(1, command.length() - 1);
[](char c) { return c == ':'; }), auto sp = split(command, " "s);
command.end()); auto reallyCommand = sp[0];
auto reallyCommand = command.substr(0, command.find_first_of(' ')); auto expect = sp[1];
auto expect = command.substr(command.find_first_of(' ') + 1, auto res = serial.GetAtUntil(reallyCommand, 2000, expect);
command.length());
auto res = serial.GetAtUntil(reallyCommand, expect, 2000);
auto endTime = std::chrono::system_clock::now(); auto endTime = std::chrono::system_clock::now();
std::cout << "dura: " res ? std::cout
<< "dura: "
<< std::chrono::duration_cast<std::chrono::milliseconds>( << std::chrono::duration_cast<std::chrono::milliseconds>(
endTime - startTime) endTime - startTime)
.count() .count()
<< std::endl; << std::endl
: std::cout << "Recive Error";
} else { } else {
auto resp = serial.GetAtResponse(command); auto resp = serial.GetAtResponse(command);
auto endTime = std::chrono::system_clock::now(); auto endTime = std::chrono::system_clock::now();
@@ -56,4 +67,4 @@ int main(int argc, char **const argv) {
} }
} }
return 0; return 0;
} }