mirror of
https://github.com/gnss-sdr/gnss-sdr
synced 2024-12-13 11:40:33 +00:00
Changed example code for navigation message listener:
- old print_content() function hid blocking udp call (unintuitive, less modular) - member variable nav_msg unnecessary (and confusing. Was previously assigned to itself in read_nav_msg fct after being passed in as fct argument) - udp_listener class now can be used as library to integrate into custom projects Signed-off-by: Lenhart <malte.lenhart@mailbox.org>
This commit is contained in:
parent
dc87113279
commit
419fb3925c
@ -34,7 +34,15 @@ int main(int argc, char *argv[])
|
||||
|
||||
while (true)
|
||||
{
|
||||
udp_listener.print_content();
|
||||
gnss_sdr::navMsg message;
|
||||
if (udp_listener.read_nav_message(message))
|
||||
{
|
||||
udp_listener.print_message(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Error: the message cannot be parsed.\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception &e)
|
||||
|
@ -25,44 +25,40 @@ Nav_Msg_Udp_Listener::Nav_Msg_Udp_Listener(unsigned short port)
|
||||
socket.bind(endpoint, error); // Bind the socket to the given local endpoint.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* !\brief blocking call to read nav_message from UDP port
|
||||
* \param[out] message navigation message class to contain parsed output
|
||||
* \return true if message parsed succesfully, false ow
|
||||
*/
|
||||
bool Nav_Msg_Udp_Listener::read_nav_message(gnss_sdr::navMsg &message)
|
||||
{
|
||||
char buff[8192]; // Buffer for storing the received data.
|
||||
|
||||
message_ = message;
|
||||
// This call will block until one or more bytes of data has been received.
|
||||
int bytes = socket.receive(boost::asio::buffer(buff));
|
||||
|
||||
std::string data(&buff[0], bytes);
|
||||
// Deserialize a stock of Nav_Msg objects from the binary string.
|
||||
return message_.ParseFromString(data);
|
||||
return message.ParseFromString(data);
|
||||
}
|
||||
|
||||
|
||||
bool Nav_Msg_Udp_Listener::print_content()
|
||||
/*
|
||||
* !\brief prints navigation message content
|
||||
* \param[in] message nav message to be printed
|
||||
*/
|
||||
void Nav_Msg_Udp_Listener::print_message(gnss_sdr::navMsg &message) const
|
||||
{
|
||||
if (read_nav_message(message_))
|
||||
{
|
||||
std::string system = message_.system();
|
||||
std::string signal = message_.signal();
|
||||
int prn = message_.prn();
|
||||
int tow_at_current_symbol_ms = message_.tow_at_current_symbol_ms();
|
||||
std::string nav_message = message_.nav_message();
|
||||
std::string system = message.system();
|
||||
std::string signal = message.signal();
|
||||
int prn = message.prn();
|
||||
int tow_at_current_symbol_ms = message.tow_at_current_symbol_ms();
|
||||
std::string nav_message = message.nav_message();
|
||||
|
||||
std::cout << "\nNew Data received:\n";
|
||||
std::cout << "System: " << system << '\n';
|
||||
std::cout << "Signal: " << signal << '\n';
|
||||
std::cout << "PRN: " << prn << '\n';
|
||||
std::cout << "TOW of last symbol [ms]: "
|
||||
<< tow_at_current_symbol_ms << '\n';
|
||||
std::cout << "Nav message: " << nav_message << "\n\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Error: the message cannot be parsed.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
std::cout << "\nNew Data received:\n";
|
||||
std::cout << "System: " << system << '\n';
|
||||
std::cout << "Signal: " << signal << '\n';
|
||||
std::cout << "PRN: " << prn << '\n';
|
||||
std::cout << "TOW of last symbol [ms]: "
|
||||
<< tow_at_current_symbol_ms << '\n';
|
||||
std::cout << "Nav message: " << nav_message << "\n\n";
|
||||
}
|
||||
|
@ -23,15 +23,14 @@ class Nav_Msg_Udp_Listener
|
||||
{
|
||||
public:
|
||||
explicit Nav_Msg_Udp_Listener(unsigned short port);
|
||||
bool print_content();
|
||||
void print_message(gnss_sdr::navMsg &message) const;
|
||||
bool read_nav_message(gnss_sdr::navMsg &message);
|
||||
|
||||
private:
|
||||
bool read_nav_message(gnss_sdr::navMsg &message);
|
||||
boost::asio::io_service io_service;
|
||||
boost::asio::ip::udp::socket socket;
|
||||
boost::system::error_code error;
|
||||
boost::asio::ip::udp::endpoint endpoint;
|
||||
gnss_sdr::navMsg message_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user