serial_listener.cpp
Go to the documentation of this file.
1 #include <array>
2 #include <chrono>
3 #include <iomanip>
4 #include <iostream>
5 #include <limits>
6 
7 #include <boost/asio.hpp>
8 
10 
11 int main(int argc, char** argv)
12 {
13  if(argc < 3)
14  {
15  std::cerr << "USAGE: serial_listener <port> <baudrate>\n\n"
16  << " Note: The serial setting should be Odd Parity and 2 stop bits.\n";
17  return 1;
18  }
19 
20  const int baudrate = std::atoi(argv[2]);
21  if(baudrate <= 0)
22  {
23  std::cerr << "Bad baudrate (=" << baudrate << "\n";
24  return 1;
25  }
26 
27  boost::asio::io_service io;
28  boost::asio::serial_port socket{io};
29 
30  try
31  {
32  socket.open(argv[1]);
33  }
34  catch(const boost::system::system_error& e)
35  {
36  std::cerr << "Failed to open the serial port: " << e.what();
37  return 1;
38  }
39 
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});
46 
47  std::array<uint8_t, 8192> recvBuffer;
49 
50  std::cout << "Listening on serial port " << argv[1] << "...\n";
51 
52  auto lastPrint = std::chrono::steady_clock::now();
53 
54  try
55  {
56  while(true)
57  {
58  const auto bytesRead = socket.read_some(boost::asio::buffer(recvBuffer));
59 
60  decoder.addNewData(recvBuffer.data(), bytesRead);
61  while(decoder.parseNextFrame())
62  {
63  if((std::chrono::steady_clock::now() - lastPrint) >
64  std::chrono::seconds{1})
65  {
66  const auto nav = decoder.getLastNavData();
67  if(nav.position.is_initialized())
68  {
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();
76  }
77  }
78  }
79  }
80  }
81  catch(const std::runtime_error& e)
82  {
83  std::cerr << "Parser error: " << e.what() << '\n';
84  return 1;
85  }
86 }
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
Definition: stdbin.h:77
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 ...


ixblue_stdbin_decoder
Author(s): Adrien BARRAL , Laure LEBROTON
autogenerated on Sat Jan 9 2021 03:13:21