diff --git a/demo.exe b/demo.exe deleted file mode 100644 index dc7ab82..0000000 Binary files a/demo.exe and /dev/null differ diff --git a/include/serial.h b/include/serial.h index f558802..5dd424a 100644 --- a/include/serial.h +++ b/include/serial.h @@ -70,18 +70,18 @@ template concept _SupportString = requires { _IsCastable::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::string>) { return str; } else if constexpr (std::is_same_v, std::string_view>) { return std::move(std::string(str.data())); - } else if constexpr (std::is_same_v, const char*>) { + } else if constexpr (std::is_same_v, const char *>) { return std::string(str); } } static std::vector GetUsbPorts() { +#if defined(_WIN32) || defined(_WIN64) std::vector portArray; std::string comname; std::string showname; @@ -99,6 +99,9 @@ static std::vector 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 { diff --git a/main.cc b/main.cc index 1e3babb..4ed1e3f 100644 --- a/main.cc +++ b/main.cc @@ -25,16 +25,6 @@ std::vector split(T str, T d) { }); return std::vector(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; -} -*/ \ No newline at end of file +} \ No newline at end of file