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

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,18 +70,18 @@ template <typename T>
concept _SupportString = requires {
_IsCastable<T, const char *, char *, std::string, std::string_view>::value;
};
template <_SupportString T> std::string _to_string(T &&str) {
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*>) {
} else if constexpr (std::is_same_v<std::decay_t<T>, const char *>) {
return std::string(str);
}
}
static std::vector<std::string> GetUsbPorts() {
#if defined(_WIN32) || defined(_WIN64)
std::vector<std::string> portArray;
std::string comname;
std::string showname;
@ -99,6 +99,9 @@ static std::vector<std::string> GetUsbPorts() {
showname.clear();
});
return portArray;
#elif defined(__linux__)
#endif
}
enum class [[maybe_unused]] SerialErrorCode {
@ -109,6 +112,26 @@ enum class [[maybe_unused]] SerialErrorCode {
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 {
private:
serialib ser;
@ -139,15 +162,25 @@ class Serial {
bool IsOpen() { return ser.isDeviceOpen(); }
template <_SupportString T>
bool OpenDevice(T portName, unsigned int bauds = 115200,
int delayTime = 0) {
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())
return true;
int code = ser.openDevice(reallyPortName.c_str(), bauds);
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 {

13
main.cc
View File

@ -25,16 +25,6 @@ std::vector<T> split(T str, T d) {
});
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) {
Serial serial;
auto ports = serial::GetUsbPorts();
@ -77,5 +67,4 @@ int main(int argc, char **const argv) {
}
}
return 0;
}
*/
}