From 46c9893607ecd32f8d9e2e6b7c3afa38bfc7e324 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 13 Aug 2026 22:16:05 +0200 Subject: erster import --- src/main.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/main.cpp (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..e699c6a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +#include "minmea.h" + +using namespace std; + +// constexpr auto PORT = "/dev/cuaU0"; +constexpr auto PORT = "/dev/ugen0.14"; +constexpr auto BAUDRATE = 9600; + +namespace { + +auto +read_line(asio::serial_port& com) +{ + for ( string result;; ) { + char chr = 0; + + asio::read(com, asio::buffer(&chr, 1)); + switch ( chr ) { + case '\r': + break; + case '\n': + return result; + default: + result += chr; + } + } +} + +string +get_gps_timestamp(const minmea_time& time, const minmea_date& date) +{ + tm tm_utc{}; + ::minmea_getdatetime(&tm_utc, &date, &time); + + auto timestamp = ::timegm(&tm_utc); + + tm tm_local{}; + (void) ::localtime_r(×tamp, &tm_local); + + static const auto buffer_size = 100; + array buffer{}; + + (void) ::strftime(buffer.data(), buffer.size(), "%Y-%m-%d %H:%M:%S", &tm_local); + + return buffer.data(); +} + +} // namespace + +int +main() +{ + try { + asio::io_context io_context{}; + asio::serial_port com(io_context, PORT); + com.set_option(asio::serial_port::baud_rate(BAUDRATE)); + + for ( ;; ) { + const auto line = read_line(com); + const auto nmea_id = ::minmea_sentence_id(line.c_str(), true); + + if ( nmea_id != MINMEA_SENTENCE_RMC ) { + continue; + } + + minmea_sentence_rmc frame{}; + if ( !::minmea_parse_rmc(&frame, line.c_str()) || !frame.valid ) { + continue; + } + + auto timestamp = get_gps_timestamp(frame.time, frame.date); + + cout << timestamp << ": $RMC floating point degree coordinates and speed: (" + << ::minmea_tocoord(&frame.latitude) << "," << ::minmea_tocoord(&frame.longitude) << ") " + << ::minmea_tofloat(&frame.speed) << '\n'; + } + } + catch ( exception& e ) { + cerr << "io-error: " << e.what() << '\n'; + } +} -- cgit v1.3