From d1f27c8c4ff876a0b0dc6bf030c068a78d527e8a Mon Sep 17 00:00:00 2001 From: Erik Strand <erik.strand@cba.mit.edu> Date: Mon, 13 May 2019 03:02:52 -0400 Subject: [PATCH] Log data to a file --- main.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 0b97861..7ec5ab7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,9 @@ // Use of libserialport based on https://gist.github.com/Nixes/78e401234e66aa131547d7b78135271c +#include <chrono> +#include <ctime> +#include <fstream> +#include <iomanip> #include <iostream> #include <libserialport.h> @@ -30,6 +34,8 @@ int main() { constexpr uint32_t byte_buffer_size = 512; char byte_buffer[byte_buffer_size]; + std::ofstream output_file = std::ofstream("data.csv"); + struct sp_port *port; std::cout << "Opening port " << desired_port << '\n'; sp_return error = sp_get_port_by_name(desired_port, &port); @@ -48,7 +54,16 @@ int main() { //if (byte_buffer[i] == '1') { // button_pressed = true; //} - std::cout << byte_buffer[i]; + output_file << byte_buffer[i]; + //std::cout << byte_buffer[i]; + if (byte_buffer[i] == '\n') { + output_file << std::flush; + auto const chrono_time = std::chrono::system_clock::now(); + std::time_t time = std::chrono::system_clock::to_time_t(chrono_time); + std::tm local_time = *std::localtime(&time); + output_file << std::put_time(&local_time, "%Y/%m/%d %H:%M:%S") << ", "; + //std::cout << std::put_time(&local_time, "%Y/%m/%d %H:%M:%S") << ", "; + } } //std::cout << '\n'; /* -- GitLab