diff --git a/lib/Slic3r/GUI/Controller/PrinterPanel.pm b/lib/Slic3r/GUI/Controller/PrinterPanel.pm index eaff5756f..7a5acb7da 100644 --- a/lib/Slic3r/GUI/Controller/PrinterPanel.pm +++ b/lib/Slic3r/GUI/Controller/PrinterPanel.pm @@ -301,12 +301,7 @@ sub connect { if (!$res) { $self->set_status("Connection failed"); } - { - # set up a timeout - my $timestamp = time(); - 1 until $self->sender->is_connected || (time - $timestamp) >= CONNECTION_TIMEOUT; - } - if ($self->sender->is_connected) { + if ($self->sender->wait_connected) { $self->set_status("Printer is online. You can now start printing from the queue on the right."); $self->status_timer->Start(STATUS_TIMER_INTERVAL, wxTIMER_CONTINUOUS); $self->temp_timer->Start(TEMP_TIMER_INTERVAL, wxTIMER_CONTINUOUS); diff --git a/lib/Slic3r/GUI/Tab.pm b/lib/Slic3r/GUI/Tab.pm index 3d65b5f7a..bf4e08184 100644 --- a/lib/Slic3r/GUI/Tab.pm +++ b/lib/Slic3r/GUI/Tab.pm @@ -1094,16 +1094,7 @@ sub build { $self->{config}->serial_port, $self->{config}->serial_speed, ); - if ($res) { - { - # set up a timeout - my $timestamp = time(); - my $CONNECTION_TIMEOUT = 3; # seconds - 1 until $sender->is_connected || (time - $timestamp) >= $CONNECTION_TIMEOUT; - } - $res = $sender->is_connected; - } - if ($res) { + if ($res && $sender->wait_connected) { Slic3r::GUI::show_info($self, "Connection to printer works correctly.", "Success!"); } else { Slic3r::GUI::show_error($self, "Connection failed."); diff --git a/xs/src/libslic3r/GCodeSender.cpp b/xs/src/libslic3r/GCodeSender.cpp index 65b4fbfd3..15e8a20a7 100644 --- a/xs/src/libslic3r/GCodeSender.cpp +++ b/xs/src/libslic3r/GCodeSender.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #if __APPLE__ @@ -134,6 +135,18 @@ GCodeSender::is_connected() const return this->connected; } +bool +GCodeSender::wait_connected(unsigned int timeout) const +{ + using namespace boost::posix_time; + ptime t0 = second_clock::local_time() + seconds(timeout); + while (!this->connected) { + if (second_clock::local_time() > t0) return false; + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + } + return true; +} + size_t GCodeSender::queue_size() const { diff --git a/xs/src/libslic3r/GCodeSender.hpp b/xs/src/libslic3r/GCodeSender.hpp index 29a53c50d..56a116ab1 100644 --- a/xs/src/libslic3r/GCodeSender.hpp +++ b/xs/src/libslic3r/GCodeSender.hpp @@ -24,6 +24,7 @@ class GCodeSender : private boost::noncopyable { void disconnect(); bool error_status() const; bool is_connected() const; + bool wait_connected(unsigned int timeout = 3) const; size_t queue_size() const; void pause_queue(); void resume_queue(); diff --git a/xs/xsp/GCodeSender.xsp b/xs/xsp/GCodeSender.xsp index 104b9b70c..4559f8993 100644 --- a/xs/xsp/GCodeSender.xsp +++ b/xs/xsp/GCodeSender.xsp @@ -13,8 +13,9 @@ bool connect(std::string port, unsigned int baud_rate); void disconnect(); - bool is_connected() const; - int queue_size() const; + bool is_connected(); + bool wait_connected(unsigned int timeout = 3); + int queue_size(); void send(std::string s, bool priority = false); void pause_queue(); void resume_queue();