AIS: maneuver indicator is now an enumeration.
[marnav.git] / examples / simple_seatalk_nmea_converter.cpp
blob3c7323e72e342d954c1749eebc996b44dc099474
1 #include <map>
2 #include <boost/asio.hpp>
3 #include <marnav/nmea/nmea.hpp>
4 #include <marnav/nmea/dpt.hpp>
5 #include <marnav/seatalk/seatalk.hpp>
6 #include <marnav/seatalk/message_00.hpp>
7 #include <marnav/io/default_seatalk_reader.hpp>
8 #include <marnav/io/default_seatalk_serial.hpp>
10 using namespace marnav;
12 namespace marnav_example
14 static std::string conv_depth_below_transducer(const seatalk::message & msg)
16 using namespace marnav::seatalk;
18 const auto & m = message_cast<message_00>(msg);
20 nmea::dpt dpt;
21 dpt.set_depth_meter(m.get_depth_meters());
22 dpt.set_transducer_offset(0.0);
23 return nmea::to_string(dpt);
27 int main(int, char **)
29 using namespace marnav_example;
30 using namespace marnav::io;
32 // mapping of conversion functions
33 static const std::map<seatalk::message_id,
34 std::function<std::string(const seatalk::message &)>> CONV
35 = {
36 {seatalk::message_id::depth_below_transducer, conv_depth_below_transducer},
39 // open output port
40 boost::asio::io_service io;
41 boost::asio::serial_port serial{io, "/dev/ttyUSB1"};
42 serial.set_option(boost::asio::serial_port_base::baud_rate(4800));
44 // open input port and reader
45 default_seatalk_reader reader{make_default_seatalk_serial("/dev/ttyUSB0")};
46 seatalk::raw data;
48 while (reader.read_message(data)) {
49 // receive message
50 auto msg = seatalk::make_message(data);
51 if (!msg)
52 continue;
54 // convert message to NMEA sentence, if known
55 std::string nmea;
56 auto func = CONV.find(msg->type());
57 if (func != CONV.end())
58 nmea = func->second(*msg);
60 // send NMEA sentence
61 if (nmea.size() > 0) {
62 nmea += "\r\n";
63 boost::asio::write(serial, boost::asio::buffer(nmea.c_str(), nmea.size()));
67 return 0;