diff --git a/src/rtde/rtde_client.cpp b/src/rtde/rtde_client.cpp index 8667d466..c804041a 100644 --- a/src/rtde/rtde_client.cpp +++ b/src/rtde/rtde_client.cpp @@ -90,19 +90,27 @@ bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::m } unsigned int attempts = 0; + std::stringstream ss; while (attempts < max_initialization_attempts) { - setupCommunication(max_connection_attempts, reconnection_timeout); + try + { + setupCommunication(max_connection_attempts, reconnection_timeout); + } + catch (const UrException& exc) + { + ss << exc.what() << std::endl; + } if (client_state_ == ClientState::INITIALIZED) + { return true; - + } if (++attempts < max_initialization_attempts) { URCL_LOG_ERROR("Failed to initialize RTDE client, retrying in %d seconds", initialization_timeout.count() / 1000); std::this_thread::sleep_for(initialization_timeout); } } - std::stringstream ss; ss << "Failed to initialize RTDE client after " << max_initialization_attempts << " attempts"; throw UrException(ss.str()); } @@ -709,4 +717,4 @@ std::vector RTDEClient::splitVariableTypes(const std::string& varia return result; } } // namespace rtde_interface -} // namespace urcl +} // namespace urcl \ No newline at end of file diff --git a/tests/test_rtde_client.cpp b/tests/test_rtde_client.cpp index 2831d6b4..cd87c235 100644 --- a/tests/test_rtde_client.cpp +++ b/tests/test_rtde_client.cpp @@ -363,13 +363,14 @@ TEST_F(RTDEClientTest, connect_non_running_robot) client_.reset( new rtde_interface::RTDEClient("192.168.56.123", notifier_, resources_output_recipe_, resources_input_recipe_)); auto start = std::chrono::system_clock::now(); - EXPECT_THROW(client_->init(2, std::chrono::milliseconds(500)), UrException); + EXPECT_THROW(client_->init(2, std::chrono::milliseconds(500), 1), UrException); auto end = std::chrono::system_clock::now(); auto elapsed = end - start; // This is only a rough estimate, obviously. // Since this isn't done on the loopback device, trying to open a socket on a non-existing address // takes considerably longer. - EXPECT_LT(elapsed, 2 * comm::TCPSocket::DEFAULT_RECONNECTION_TIME); + EXPECT_LT(std::chrono::duration_cast(elapsed).count(), + 2 * comm::TCPSocket::DEFAULT_RECONNECTION_TIME.count()); } TEST_F(RTDEClientTest, check_all_rtde_output_variables_exist) @@ -426,4 +427,4 @@ int main(int argc, char* argv[]) } return RUN_ALL_TESTS(); -} +} \ No newline at end of file