diff --git a/src/modules/mavlink/mavlink_main.cpp b/src/modules/mavlink/mavlink_main.cpp index 81776b520f8a1788235948a7f887f3d0fca52915..24e7ddc0bfb859726e100eb5ca48202fd4a5f86a 100644 --- a/src/modules/mavlink/mavlink_main.cpp +++ b/src/modules/mavlink/mavlink_main.cpp @@ -885,13 +885,20 @@ Mavlink::send_message(const uint8_t msgid, const void *msg, uint8_t component_ID if ((_mode != MAVLINK_MODE_ONBOARD) && (!get_client_source_initialized() || (hrt_elapsed_time(&tstatus.heartbeat_time) > 3 * 1000 * 1000)) - && (msgid == MAVLINK_MSG_ID_HEARTBEAT) - && _broadcast_address_found) { + && (msgid == MAVLINK_MSG_ID_HEARTBEAT)) { - int bret = sendto(_socket_fd, buf, packet_len, 0, (struct sockaddr *)&_bcast_addr, sizeof(_bcast_addr)); + if (!_broadcast_address_found) { + // Try to initialize UDP and broadcast address again. + init_udp(); + } + + if (_broadcast_address_found) { - if (bret <= 0) { - PX4_WARN("sending broadcast failed, errno: %d: %s", errno, strerror(errno)); + int bret = sendto(_socket_fd, buf, packet_len, 0, (struct sockaddr *)&_bcast_addr, sizeof(_bcast_addr)); + + if (bret <= 0) { + PX4_WARN("sending broadcast failed, errno: %d: %s", errno, strerror(errno)); + } } }