-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathnmea_multiplexer.cpp
41 lines (35 loc) · 1.43 KB
/
nmea_multiplexer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/// This example demonstrates how to do a very basic NMEA multiplexer.
/// It does not implement any error handling and other (normally necessary)
/// stuff (configurability, error handling, etc.).
#include <marnav/nmea/nmea.hpp>
#include <marnav/nmea/checksum.hpp>
#include <marnav-io/default_nmea_reader.hpp>
#include <marnav-io/serial.hpp>
#include <vector>
int main(int, char **)
{
using namespace marnav;
using namespace marnav::io;
// prepare destinations
std::vector<std::unique_ptr<serial>> destinations;
destinations.push_back(std::make_unique<serial>("dev/ttyUSB1", serial::baud::baud_4800,
serial::databits::bit_8, serial::stopbits::bit_1, serial::parity::none));
destinations.push_back(std::make_unique<serial>("dev/ttyUSB2", serial::baud::baud_4800,
serial::databits::bit_8, serial::stopbits::bit_1, serial::parity::none));
// open source device
default_nmea_reader source{std::make_unique<serial>("/dev/ttyUSB0", serial::baud::baud_4800,
serial::databits::bit_8, serial::stopbits::bit_1, serial::parity::none)};
std::string data;
while (source.read_sentence(data)) {
try {
// this is used only to check the received data, e.g. if the checksum is correct.
auto sentence = nmea::make_sentence(data);
// send valid NMEA sentences to destinations
for (auto & destination : destinations) {
destination->write(data.c_str(), data.size());
}
} catch (nmea::checksum_error &) {
// let's just ignore them
}
}
}