Skip to content

Commit

Permalink
Small adjustments for port serial opening
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Dudnytskyi committed Mar 25, 2024
1 parent c6ed7d5 commit c11dfa8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
8 changes: 1 addition & 7 deletions tools/remote_serial_bridge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions tools/utils/serial_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions tools/utils/simulator_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c11dfa8

Please sign in to comment.