summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2026-08-13 22:16:05 +0200
committerThomas Schmucker <ts@its1.de>2026-08-13 22:16:05 +0200
commit46c9893607ecd32f8d9e2e6b7c3afa38bfc7e324 (patch)
tree21b80c763a65e81a883b96761f5277cdb21f1905 /src
downloaduse-minmea-46c9893607ecd32f8d9e2e6b7c3afa38bfc7e324.tar.gz
use-minmea-46c9893607ecd32f8d9e2e6b7c3afa38bfc7e324.tar.bz2
use-minmea-46c9893607ecd32f8d9e2e6b7c3afa38bfc7e324.zip
erster import
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp86
1 files changed, 86 insertions, 0 deletions
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 @@
1#include <asio.hpp>
2#include <ctime>
3#include <iostream>
4#include <string>
5
6#include "minmea.h"
7
8using namespace std;
9
10// constexpr auto PORT = "/dev/cuaU0";
11constexpr auto PORT = "/dev/ugen0.14";
12constexpr auto BAUDRATE = 9600;
13
14namespace {
15
16auto
17read_line(asio::serial_port& com)
18{
19 for ( string result;; ) {
20 char chr = 0;
21
22 asio::read(com, asio::buffer(&chr, 1));
23 switch ( chr ) {
24 case '\r':
25 break;
26 case '\n':
27 return result;
28 default:
29 result += chr;
30 }
31 }
32}
33
34string
35get_gps_timestamp(const minmea_time& time, const minmea_date& date)
36{
37 tm tm_utc{};
38 ::minmea_getdatetime(&tm_utc, &date, &time);
39
40 auto timestamp = ::timegm(&tm_utc);
41
42 tm tm_local{};
43 (void) ::localtime_r(&timestamp, &tm_local);
44
45 static const auto buffer_size = 100;
46 array<char, buffer_size> buffer{};
47
48 (void) ::strftime(buffer.data(), buffer.size(), "%Y-%m-%d %H:%M:%S", &tm_local);
49
50 return buffer.data();
51}
52
53} // namespace
54
55int
56main()
57{
58 try {
59 asio::io_context io_context{};
60 asio::serial_port com(io_context, PORT);
61 com.set_option(asio::serial_port::baud_rate(BAUDRATE));
62
63 for ( ;; ) {
64 const auto line = read_line(com);
65 const auto nmea_id = ::minmea_sentence_id(line.c_str(), true);
66
67 if ( nmea_id != MINMEA_SENTENCE_RMC ) {
68 continue;
69 }
70
71 minmea_sentence_rmc frame{};
72 if ( !::minmea_parse_rmc(&frame, line.c_str()) || !frame.valid ) {
73 continue;
74 }
75
76 auto timestamp = get_gps_timestamp(frame.time, frame.date);
77
78 cout << timestamp << ": $RMC floating point degree coordinates and speed: ("
79 << ::minmea_tocoord(&frame.latitude) << "," << ::minmea_tocoord(&frame.longitude) << ") "
80 << ::minmea_tofloat(&frame.speed) << '\n';
81 }
82 }
83 catch ( exception& e ) {
84 cerr << "io-error: " << e.what() << '\n';
85 }
86}