7 #include <boost/asio.hpp> 11 int main(
int argc,
char** argv)
15 std::cerr <<
"USAGE: serial_listener <port> <baudrate>\n\n" 16 <<
" Note: The serial setting should be Odd Parity and 2 stop bits.\n";
20 const int baudrate = std::atoi(argv[2]);
23 std::cerr <<
"Bad baudrate (=" << baudrate <<
"\n";
27 boost::asio::io_service io;
28 boost::asio::serial_port socket{io};
34 catch(
const boost::system::system_error& e)
36 std::cerr <<
"Failed to open the serial port: " << e.what();
40 using serial = boost::asio::serial_port_base;
41 socket.set_option(serial::baud_rate(baudrate));
42 socket.set_option(serial::character_size{8});
43 socket.set_option(serial::parity{serial::parity::odd});
44 socket.set_option(serial::stop_bits{serial::stop_bits::two});
45 socket.set_option(serial::flow_control{serial::flow_control::none});
47 std::array<uint8_t, 8192> recvBuffer;
50 std::cout <<
"Listening on serial port " << argv[1] <<
"...\n";
52 auto lastPrint = std::chrono::steady_clock::now();
58 const auto bytesRead = socket.read_some(boost::asio::buffer(recvBuffer));
60 decoder.
addNewData(recvBuffer.data(), bytesRead);
63 if((std::chrono::steady_clock::now() - lastPrint) >
64 std::chrono::seconds{1})
67 if(nav.position.is_initialized())
69 std::cout <<
"Position: \n" 70 << std::fixed << std::setprecision(9)
71 <<
" lat: " << nav.
position->latitude_deg <<
" deg\n" 72 <<
" lon: " << nav.position->longitude_deg <<
" deg\n" 73 << std::setprecision(2)
74 <<
" alt: " << nav.position->altitude_m <<
" m\n";
75 lastPrint = std::chrono::steady_clock::now();
81 catch(
const std::runtime_error& e)
83 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 ...
int main(int argc, char **argv)
Parser of a STDBIN IXblue message. This is the entry point of the library. Usage of this class is as ...