添加数据位, 奇偶校验, 停止位设置

This commit is contained in:
JIe 2024-10-12 14:46:49 +08:00
parent 517c4699d4
commit 6ec5ffd22f
3 changed files with 41 additions and 19 deletions

BIN
demo.exe

Binary file not shown.

View File

@ -70,8 +70,7 @@ template <typename T>
concept _SupportString = requires { concept _SupportString = requires {
_IsCastable<T, const char *, char *, std::string, std::string_view>::value; _IsCastable<T, const char *, char *, std::string, std::string_view>::value;
}; };
template <_SupportString T> [[maybe_unused]] std::string _to_string(T &&str) {
template <_SupportString T> std::string _to_string(T &&str) {
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) { if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
return str; return str;
} else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) { } else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>) {
@ -82,6 +81,7 @@ template <_SupportString T> std::string _to_string(T &&str) {
} }
static std::vector<std::string> GetUsbPorts() { static std::vector<std::string> GetUsbPorts() {
#if defined(_WIN32) || defined(_WIN64)
std::vector<std::string> portArray; std::vector<std::string> portArray;
std::string comname; std::string comname;
std::string showname; std::string showname;
@ -99,6 +99,9 @@ static std::vector<std::string> GetUsbPorts() {
showname.clear(); showname.clear();
}); });
return portArray; return portArray;
#elif defined(__linux__)
#endif
} }
enum class [[maybe_unused]] SerialErrorCode { enum class [[maybe_unused]] SerialErrorCode {
@ -109,6 +112,26 @@ enum class [[maybe_unused]] SerialErrorCode {
READINGERROR, READINGERROR,
}; };
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,
};
class Serial { class Serial {
private: private:
serialib ser; serialib ser;
@ -139,15 +162,25 @@ class Serial {
bool IsOpen() { return ser.isDeviceOpen(); } bool IsOpen() { return ser.isDeviceOpen(); }
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,
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::string reallyPortName;
std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}", std::format_to(std::back_inserter(reallyPortName), "\\\\.\\{}",
portName); portName);
#elif defined(__linux__)
std::string reallyPortName = std::string(portName);
#endif
std::this_thread::sleep_for(std::chrono::milliseconds(delayTime)); std::this_thread::sleep_for(std::chrono::milliseconds(delayTime));
if (ser.isDeviceOpen()) if (ser.isDeviceOpen())
return true; ser.closeDevice();
int code = ser.openDevice(reallyPortName.c_str(), bauds);
int code = ser.openDevice(reallyPortName.c_str(), bauds,
static_cast<::SerialDataBits>(dataBits),
static_cast<::SerialParity>(parity),
static_cast<::SerialStopBits>(stopBits));
if (code == 1) { if (code == 1) {
return true; return true;
} else { } else {

11
main.cc
View File

@ -25,16 +25,6 @@ std::vector<T> split(T str, T d) {
}); });
return std::vector<T>(v.begin(), v.end()); return std::vector<T>(v.begin(), v.end());
} }
int main() {
Serial ser;
ser.OpenDevice("COM11", 115200);
ser.SetLogCallBack(PrintLog);
ser.GetAtUntilRepeat<5>("AT", "OK");
return 0;
}
/*
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();
@ -78,4 +68,3 @@ int main(int argc, char **const argv) {
} }
return 0; return 0;
} }
*/