#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'; } }