From c11dfa822722624a528d2945987d8344ef96cfc8 Mon Sep 17 00:00:00 2001 From: Pavlo Dudnytskyi Date: Mon, 25 Mar 2024 07:41:57 +0100 Subject: [PATCH] Small adjustments for port serial opening --- tools/remote_serial_bridge/main.cpp | 8 +------- tools/utils/serial_stream.cpp | 10 +++++++--- tools/utils/simulator_base.cpp | 8 +------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/tools/remote_serial_bridge/main.cpp b/tools/remote_serial_bridge/main.cpp index 880a425..9cba24f 100644 --- a/tools/remote_serial_bridge/main.cpp +++ b/tools/remote_serial_bridge/main.cpp @@ -77,13 +77,7 @@ int main(int argc, char** argv) { } HAIER_LOGI("Opening port %s", argv[1]); haier_protocol::set_log_handler(console_logger); - std::string port_path; -#if _WIN32 - port_path = std::string("\\\\.\\").append(argv[1]); -#else - port_path = argv[1]; -#endif - SerialStream serial_stream(port_path.c_str()); + SerialStream serial_stream(argv[1]); if (!serial_stream.is_valid()) { std::cout << "Can't open port " << argv[1] << std::endl; return 1; diff --git a/tools/utils/serial_stream.cpp b/tools/utils/serial_stream.cpp index 74ebe0e..c9dcd23 100644 --- a/tools/utils/serial_stream.cpp +++ b/tools/utils/serial_stream.cpp @@ -9,7 +9,11 @@ SerialStream::SerialStream(const std::string& port_path) : buffer_(SERIAL_BUFFER_SIZE) { #if _WIN32 - handle_ = CreateFile(port_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + constexpr char win_prefix[] = "\\\\.\\"; + std::string port_win = port_path; + if (port_win.rfind(win_prefix, 0) != 0) + port_win = std::string(win_prefix).append(port_win); + handle_ = CreateFile(port_win.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (is_valid()) { DCB serialParams = { 0 }; serialParams.DCBlength = sizeof(serialParams); @@ -63,8 +67,8 @@ SerialStream::SerialStream(const std::string& port_path) : buffer_(SERIAL_BUFFER cfsetispeed(&tty, B9600); cfsetospeed(&tty, B9600); if (tcsetattr(handle_, TCSANOW, &tty) != 0) { - handle_ = -1; - return; + handle_ = -1; + return; } } #endif diff --git a/tools/utils/simulator_base.cpp b/tools/utils/simulator_base.cpp index d70fc3f..321ee16 100644 --- a/tools/utils/simulator_base.cpp +++ b/tools/utils/simulator_base.cpp @@ -42,13 +42,7 @@ void simulator_main(const char* app_name, const char* port_name, message_handler void simulator_main(const char* app_name, const char* port_name, message_handlers mhandlers, answer_handlers ahandlers, keyboard_handlers khandlers, protocol_preloop ploop) { haier_protocol::set_log_handler(console_logger); - std::string port_path; -#if _WIN32 - port_path = std::string("\\\\.\\").append(port_name); -#else - port_path = port_name; -#endif - SerialStream serial_stream(port_path.c_str()); + SerialStream serial_stream(port_name); if (!serial_stream.is_valid()) { std::cout << "Can't open port " << port_name << std::endl; return;