Skip to content
Snippets Groups Projects
Commit d1f27c8c authored by Erik Strand's avatar Erik Strand
Browse files

Log data to a file

parent 997eedda
No related branches found
No related tags found
No related merge requests found
// Use of libserialport based on https://gist.github.com/Nixes/78e401234e66aa131547d7b78135271c // Use of libserialport based on https://gist.github.com/Nixes/78e401234e66aa131547d7b78135271c
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream> #include <iostream>
#include <libserialport.h> #include <libserialport.h>
...@@ -30,6 +34,8 @@ int main() { ...@@ -30,6 +34,8 @@ int main() {
constexpr uint32_t byte_buffer_size = 512; constexpr uint32_t byte_buffer_size = 512;
char byte_buffer[byte_buffer_size]; char byte_buffer[byte_buffer_size];
std::ofstream output_file = std::ofstream("data.csv");
struct sp_port *port; struct sp_port *port;
std::cout << "Opening port " << desired_port << '\n'; std::cout << "Opening port " << desired_port << '\n';
sp_return error = sp_get_port_by_name(desired_port, &port); sp_return error = sp_get_port_by_name(desired_port, &port);
...@@ -48,7 +54,16 @@ int main() { ...@@ -48,7 +54,16 @@ int main() {
//if (byte_buffer[i] == '1') { //if (byte_buffer[i] == '1') {
// button_pressed = true; // 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'; //std::cout << '\n';
/* /*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment