-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoConnection.cpp
More file actions
76 lines (65 loc) · 2.47 KB
/
ArduinoConnection.cpp
File metadata and controls
76 lines (65 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "ArduinoConnection.h"
#include <chrono>
ArduinoConnection::ArduinoConnection(std::string port, int baudrate, int timeout)
: arduino(port) // Open the serial port
{
// Set serial parameters
if (baudrate==115200)
arduino.SetBaudRate(LibSerial::BaudRate::BAUD_115200); // Correct baud rate enumeration
arduino.SetCharacterSize(LibSerial::CharacterSize::CHAR_SIZE_8); // Correct character size
arduino.SetParity(LibSerial::Parity::PARITY_NONE); // Correct parity
arduino.SetStopBits(LibSerial::StopBits::STOP_BITS_1); // Correct stop bits
arduino.SetFlowControl(LibSerial::FlowControl::FLOW_CONTROL_NONE); // Correct flow control
}
bool ArduinoConnection::is_open(){
// Check if the port is open
if (arduino.IsOpen())
return true;
else
return false;
}
void ArduinoConnection::write(const std::vector<double>& values){
std::string data_string = "";
for(int i=0; i<values.size(); i++) {
if (i!=0)
data_string += ",";
data_string += std::to_string(values[i]);
}
// std::cout << data_string << std::endl;
arduino << data_string << std::endl;
}
void ArduinoConnection::write(const std::string data_string){
arduino << data_string << std::endl;
// std::cout << data_string << std::endl;
const std::string expected_response = "Received."; // Define the expected response
std::string response;
bool received = false;
// Start the timeout timer
auto start = std::chrono::steady_clock::now();
const auto timeout = std::chrono::seconds(1); // Set timeout duration
while (!received) {
// Check for timeout
if (std::chrono::steady_clock::now() - start > timeout) {
std::cerr << "Timeout waiting for response." << std::endl;
break;
}
response = read(); // Read response from Arduino
if (response == expected_response) { // Check if response matches the expected value
received = true;
}
}
if (!received) {
std::cerr << "Failed to receive acknowledgment from Arduino." << std::endl;
}
}
std::string ArduinoConnection::read(){
std::string response;
if (arduino.IsDataAvailable()) { // Ensure data is available before reading
std::getline(arduino, response);
response.erase(response.find_last_not_of(" \n\r\t") + 1); // Trim whitespace
}
return response;
}
ArduinoConnection::~ArduinoConnection(){
arduino.Close();
}