...
 
Commits (2)
// Use of libserialport based on https://gist.github.com/Nixes/78e401234e66aa131547d7b78135271c
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <libserialport.h>
......@@ -23,12 +27,15 @@ int main() {
list_ports();
//const char* desired_port = "/dev/cu.usbserial-AC01YB5P";
//const char* desired_port = "/dev/ttyUSB0";
const char* desired_port = "/dev/ttyACM0";
const char* desired_port = "/dev/ttyUSB0";
//const char* desired_port = "/dev/ttyACM0";
constexpr uint32_t baud_rate = 115200;
//constexpr uint32_t baud_rate = 19200;
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);
......@@ -39,7 +46,7 @@ int main() {
while (true) {
int bytes_waiting = sp_input_waiting(port);
if (bytes_waiting > 0) {
std::cout << bytes_waiting << " bytes in the buffer: ";
//std::cout << bytes_waiting << " bytes in the buffer: ";
int byte_num = 0;
byte_num = sp_nonblocking_read(port, byte_buffer, byte_buffer_size);
//bool button_pressed = false;
......@@ -47,9 +54,18 @@ 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';
//std::cout << '\n';
/*
if (button_pressed) {
std::cout << " the button is pressed!";
......@@ -87,4 +103,3 @@ int main() {
return 0;
}