@@ -555,21 +555,27 @@ bool UrDriver::sendScript(const std::string& program)
555555 const uint8_t * data = reinterpret_cast <const uint8_t *>(program_with_newline.c_str ());
556556 size_t written;
557557
558- if (secondary_stream_->write (data, len, written))
558+ const auto send_script_contents = [this , program_with_newline, data, len,
559+ &written](const std::string&& description) -> bool {
560+ if (secondary_stream_->write (data, len, written))
561+ {
562+ URCL_LOG_DEBUG (" Sent program to robot:\n %s" , program_with_newline.c_str ());
563+ return true ;
564+ }
565+ const std::string error_message = " Could not send program to robot: " + description;
566+ URCL_LOG_ERROR (error_message.c_str ());
567+ return false ;
568+ };
569+
570+ if (send_script_contents (" initial attempt" ))
559571 {
560- URCL_LOG_DEBUG (" Sent program to robot:\n %s" , program_with_newline.c_str ());
561572 return true ;
562573 }
563- URCL_LOG_ERROR (" Could not send program to robot" );
564574
565- URCL_LOG_INFO (" Reconnecting secondary stream to retry sending program..." );
566- secondary_stream_->close ();
567- if (secondary_stream_->connect () && secondary_stream_->write (data, len, written))
575+ if (reconnectSecondaryStream ())
568576 {
569- URCL_LOG_DEBUG (" Sent program to robot:\n %s" , program_with_newline.c_str ());
570- return true ;
577+ return send_script_contents (" after reconnecting secondary stream" );
571578 }
572- URCL_LOG_ERROR (" Retry sending program failed!" );
573579
574580 return false ;
575581}
@@ -596,7 +602,7 @@ bool UrDriver::reconnectSecondaryStream()
596602 URCL_LOG_DEBUG (" Secondary stream connected" );
597603 return true ;
598604 }
599- URCL_LOG_ERROR (" Failed to reconnect secodary stream!" );
605+ URCL_LOG_ERROR (" Failed to reconnect secondary stream!" );
600606 return false ;
601607}
602608
0 commit comments