1 #include <marnav-io/seatalk_reader.hpp>
2 #include <marnav-io/default_seatalk_serial.hpp>
3 #include <marnav/seatalk/seatalk.hpp>
4 #include <marnav/seatalk/message_00.hpp>
7 using namespace marnav
;
9 /// Works only in a single threaded context (true for serial and seatalk_reader).
11 /// This class is implemented inline, for easier handling within this example.
12 class message_reader
: public io::seatalk_reader
15 message_reader(std::unique_ptr
<io::device
> && dev
)
16 : seatalk_reader(std::move(dev
))
20 ~message_reader() override
= default;
22 /// Reads synchronously messages from the device.
24 /// @param[out] data The received message.
25 /// @retval true Success.
26 /// @retval false End of file.
27 /// @exception std::runtime_error
28 bool read_message(seatalk::raw
& data
)
30 // reads as long as the message is not complete.
32 // the message was received, return it and reset the 'semaphore'.
33 // please note: this works only in single threaded environment,
34 // since the 'semaphore' isn't really one.
35 if (message_received
) {
37 message_received
= false;
45 /// Processes the received message. Uses the data member 'message_received'
46 /// as poor-mans semaphore to signal the receipt.
48 /// After the reception, the message will be stored temporarily.
49 void process_message(const seatalk::raw
& msg
) override
52 message_received
= true;
56 bool message_received
{false};
57 seatalk::raw message_
;
60 int main(int, char **)
62 using namespace marnav::io
;
64 // create and open the device for reading.
65 message_reader reader
{make_default_seatalk_serial("/dev/ttyUSB0")};
69 // read and process SeaTalk messages, bus synchronization is done automatically.
70 while (reader
.read_message(data
)) {
71 // data was successfully read from the SeaTalk bus, inclusive synchronization
72 // of SeaTalk messages. This means it is possible to begin to listen on the
74 auto msg
= seatalk::make_message(data
);
76 // do something with the message, for example dump the depth and ignore all
78 if (msg
->type() == seatalk::message_id::depth_below_transducer
) {
79 auto depth
= seatalk::message_cast
<seatalk::message_00
>(msg
);
80 std::cout
<< "depth below transducer: " << depth
->get_depth() << "\n";