Skip to content

Commit

Permalink
fix arduino not sending data to the COM-port
Browse files Browse the repository at this point in the history
  • Loading branch information
catink123 committed Jan 16, 2024
1 parent abe9b15 commit b5a233c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
27 changes: 22 additions & 5 deletions src/arduino_messenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ arduino_messenger::arduino_messenger(
throw open_error(error.message().c_str());
}

com.set_option(net::serial_port_base::baud_rate(baud_rate));
com.set_option(
net::serial_port_base::baud_rate(baud_rate)
);
// enable DTR (Data Terminal Ready) for reading from the COM-port to work
com.set_option(
net::serial_port_base::flow_control(
net::serial_port_base::flow_control::hardware
)
);
}

void arduino_messenger::run() {
Expand All @@ -29,7 +37,9 @@ void arduino_messenger::run() {
}

void arduino_messenger::do_read() {
net::async_read_until(com, buffer, '%',
net::streambuf::mutable_buffers_type temp_mut_buf = buffer.prepare(MAX_MESSAGE_LENGTH);

com.async_read_some(net::buffer(temp_mut_buf),
beast::bind_front_handler(
&arduino_messenger::on_read,
shared_from_this()
Expand All @@ -45,9 +55,16 @@ void arduino_messenger::on_read(
return;
}

auto buffer_data = buffer.data();
buffer.commit(bytes_transferred);

std::string message(
reinterpret_cast<const char*>(buffer.data().data()),
buffer.data().size()
);

buffer.consume(bytes_transferred);

std::string message(reinterpret_cast<const char*>(buffer_data.data()), buffer_data.size());
std::cout << "Read message: " << message << std::endl;

std::lock_guard lock(imq_mutex);
incoming_message_queue.push(json_message::parse_message(message));
Expand All @@ -57,7 +74,7 @@ void arduino_messenger::on_read(

void arduino_messenger::do_write() {
if (!outgoing_message_queue.empty()) {
outgoing_message_buffer = outgoing_message_queue.front().dump_message() + '%';
outgoing_message_buffer = outgoing_message_queue.front().dump_message();
outgoing_message_queue.pop();

std::cout << "Sending message: " << outgoing_message_buffer << std::endl;
Expand Down
3 changes: 3 additions & 0 deletions src/arduino_messenger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#include <boost/asio/error.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/asio/read.hpp>
#include "json_message.hpp"
#include <queue>
#include <thread>

class arduino_messenger : public std::enable_shared_from_this<arduino_messenger> {
static constexpr std::size_t MAX_MESSAGE_LENGTH = 100;

net::serial_port com;
net::streambuf buffer;

Expand Down
63 changes: 32 additions & 31 deletions src/arduino_program/gate_control/gate_control.ino
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
#include <ArduinoJson.h>

static const char* TYPE = "type";
static const char* PAYLOAD = "payload";
static const char* CHANGE_STATE = "change_state";
static const char* QUERY_STATE = "query_state";
static const char* QUERY_STATE_RESULT = "query_state_result";

const int capacity = 100;
StaticJsonDocument<capacity> doc;
bool state = false;

void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void change_state(bool new_state, bool persist_change = true) {
if (persist_change) {
state = new_state;
}

void flash() {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
if (state) {
if (new_state) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}

void setup() {
Serial.begin(115200);
Serial.setTimeout(50);
pinMode(13, OUTPUT);
change_state(false);
}

void loop() {
if (Serial.available() > 0) {
String msg = Serial.readStringUntil('%');
// skip the message end character
Serial.read();
String msg = Serial.readString();

deserializeJson(doc, msg);
if (doc["type"] == "change_state" && doc["payload"] == true) {
digitalWrite(13, HIGH);
state = true;
} else if (doc["type"] == "change_state" && doc["payload"] == false) {
digitalWrite(13, LOW);
state = false;
} else if (doc["type"] == "query_state") {

if (doc[TYPE] == CHANGE_STATE) {
if (doc[PAYLOAD] == true) {
change_state(true);
} else if (doc[PAYLOAD] == false) {
change_state(false);
}
} else if (doc[TYPE] == QUERY_STATE) {
DynamicJsonDocument dyn_doc(100);
dyn_doc["type"] = "query_state_result";
dyn_doc["payload"] = state;
dyn_doc[TYPE] = QUERY_STATE_RESULT;
dyn_doc[PAYLOAD] = state;

String msg;
serializeJson(dyn_doc, msg);
msg += '%';

flash();

Serial.write(msg.c_str());
} else {
Serial.write("{\"type\": \"error\", \"payload\": \"unknown_command\"}%");
Serial.write("{\"type\": \"error\", \"payload\": \"unknown_command\"}");
}
}
}

0 comments on commit b5a233c

Please sign in to comment.