7 #include <boost/asio.hpp> 11 using boost::asio::ip::udp;
13 int main(
int argc,
char** argv)
17 std::cerr <<
"USAGE: udp_listener <port>\n";
21 const int port = std::atoi(argv[1]);
22 if(port <= 0 || port > std::numeric_limits<uint16_t>::max())
24 std::cerr <<
"Port (=" << port <<
") must be between 1 and " 25 << std::numeric_limits<uint16_t>::max() <<
'\n';
29 boost::asio::io_service io;
30 udp::socket socket{io, udp::endpoint(udp::v4(), port)};
32 std::array<uint8_t, 9000> recvBuffer;
33 udp::endpoint senderEndpoint;
35 auto lastPrint = std::chrono::steady_clock::now();
37 std::cout <<
"Listening on UDP port " << port <<
"...\n";
43 const auto bytesRead =
44 socket.receive_from(boost::asio::buffer(recvBuffer), senderEndpoint);
45 decoder.
addNewData(recvBuffer.data(), bytesRead);
48 if((std::chrono::steady_clock::now() - lastPrint) >
49 std::chrono::seconds{1})
52 if(nav.position.is_initialized())
54 std::cout <<
"Position: \n" 55 << std::fixed << std::setprecision(9)
56 <<
" lat: " << nav.
position->latitude_deg <<
" deg\n" 57 <<
" lon: " << nav.position->longitude_deg <<
" deg\n" 58 << std::setprecision(2)
59 <<
" alt: " << nav.position->altitude_m <<
" m\n";
60 lastPrint = std::chrono::steady_clock::now();
66 catch(
const std::runtime_error& e)
68 std::cerr <<
"Parser error: " << e.what() <<
'\n';
void addNewData(const uint8_t *data, std::size_t length)
Add new binary data to the parser internal buffer The new data can only be a part of a frame...
boost::optional< Position > position
Data::BinaryNav getLastNavData(void) const
bool parseNextFrame()
Try to parse a frame from the parser internal buffer. Some binary data must have been added with the ...
Parser of a STDBIN IXblue message. This is the entry point of the library. Usage of this class is as ...
int main(int argc, char **argv)