diff --git a/main.cpp b/main.cpp index 0b97861ebbb1c2deef73ac6af83ff5c8402257ae..7ec5ab78a7e0f151590f9623b994b01cfb9ad38e 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'; /*