First TCP communicator
This commit is contained in:
+1
-1
@@ -23,5 +23,5 @@ set(CMAKE_CXX_STANDARD 17)
|
|||||||
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
|
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
|
||||||
|
|
||||||
|
|
||||||
add_executable(sender main.cpp grbl.h grbl.cpp grbl_test.cpp)
|
add_executable(sender main.cpp grbl.h grbl.cpp grbl_test.cpp grbl_communication.h grbl_communication.cpp)
|
||||||
target_link_libraries(sender nanogui gtest gtest_main)
|
target_link_libraries(sender nanogui gtest gtest_main)
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
#include "grbl_communication.h"
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
grbl::tcp_transport::tcp_transport(std::string address, uint16_t p)
|
||||||
|
: ip{std::move(address)}, port{p} {
|
||||||
|
live_check_thread = std::thread(&grbl::tcp_transport::worker, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void grbl::tcp_transport::open(grbl::transport_callbacks& cb) {
|
||||||
|
if (is_connected) {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
listener = &cb;
|
||||||
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (fd == -1) {
|
||||||
|
std::cerr << "Error creating socket" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in serv_addr{};
|
||||||
|
serv_addr.sin_family = AF_INET;
|
||||||
|
serv_addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
if (inet_pton(AF_INET, ip.c_str(), &serv_addr.sin_addr) <= 0) {
|
||||||
|
std::cerr << "Invalid address/ Address not supported" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto status = connect(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
|
||||||
|
if (status < 0) {
|
||||||
|
std::cerr << "Connection failed";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
is_connected = true;
|
||||||
|
listener->on_connected(this);
|
||||||
|
|
||||||
|
// set non-blocking please, after we connected
|
||||||
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||||
|
}
|
||||||
|
|
||||||
|
void grbl::tcp_transport::close() {
|
||||||
|
if (fd >= 0) {
|
||||||
|
::close(fd);
|
||||||
|
|
||||||
|
is_connected = false;
|
||||||
|
send_queue = std::queue<std::string>{};
|
||||||
|
|
||||||
|
if (listener != nullptr)
|
||||||
|
listener->on_disconnected(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void grbl::tcp_transport::send(std::string line) {
|
||||||
|
if (is_connected) {
|
||||||
|
send_queue.push(line + "\r");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void grbl::tcp_transport::worker() {
|
||||||
|
std::string received;
|
||||||
|
uint8_t buffer[200];
|
||||||
|
|
||||||
|
while (!should_quit) {
|
||||||
|
if (is_connected) {
|
||||||
|
// anything to write?
|
||||||
|
if (!send_queue.empty()) {
|
||||||
|
auto line = send_queue.front();
|
||||||
|
auto result = write(fd, line.c_str(), line.size());
|
||||||
|
if (result == -1) {
|
||||||
|
std::cerr << "Failed while writing." << std::endl;
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
send_queue.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// anything to read?
|
||||||
|
auto read_bytes = read(fd, buffer, 200);
|
||||||
|
if (read_bytes > 0) {
|
||||||
|
for (int i = 0; i < read_bytes; i++) {
|
||||||
|
auto is_eol = buffer[i] == '\r' || buffer[i] == '\n';
|
||||||
|
if (is_eol) {
|
||||||
|
if (!is_empty_line(received)) {
|
||||||
|
listener->on_line_received(received, this);
|
||||||
|
}
|
||||||
|
received.clear();
|
||||||
|
} else {
|
||||||
|
received.push_back(static_cast<char>(buffer[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// give some time to others
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
grbl::tcp_transport::~tcp_transport() {
|
||||||
|
close();
|
||||||
|
should_quit = true;
|
||||||
|
live_check_thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool grbl::tcp_transport::is_empty_line(std::string line) {
|
||||||
|
for (auto& c: line) {
|
||||||
|
if (c != '\r' && c != '\n' && c != ' ') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
|
namespace grbl {
|
||||||
|
|
||||||
|
struct transport;
|
||||||
|
|
||||||
|
struct transport_callbacks {
|
||||||
|
virtual void on_connected(transport*) = 0;
|
||||||
|
virtual void on_disconnected(transport*) = 0;
|
||||||
|
virtual void on_line_received(std::string line, transport* ) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct transport {
|
||||||
|
virtual void open(transport_callbacks& cb) = 0;
|
||||||
|
virtual void close() = 0;
|
||||||
|
virtual void send(std::string line) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct tcp_transport : public transport {
|
||||||
|
virtual ~tcp_transport();
|
||||||
|
tcp_transport(std::string address, uint16_t port);
|
||||||
|
|
||||||
|
void open(transport_callbacks& cb) override;
|
||||||
|
void close() override;
|
||||||
|
void send(std::string line) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void worker();
|
||||||
|
|
||||||
|
std::string ip;
|
||||||
|
uint16_t port;
|
||||||
|
transport_callbacks *listener = nullptr;
|
||||||
|
int fd = -1;
|
||||||
|
volatile bool is_connected = false;
|
||||||
|
std::thread live_check_thread;
|
||||||
|
std::queue<std::string> send_queue;
|
||||||
|
volatile bool should_quit = false;
|
||||||
|
bool is_empty_line(std::string line);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -50,6 +50,7 @@
|
|||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
#include "grbl.h"
|
#include "grbl.h"
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include "grbl_communication.h"
|
||||||
|
|
||||||
using namespace nanogui;
|
using namespace nanogui;
|
||||||
|
|
||||||
@@ -611,8 +612,29 @@ private:
|
|||||||
int m_current_image;
|
int m_current_image;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct grbl_listener : public grbl::transport_callbacks {
|
||||||
|
void on_connected(grbl::transport *t) override {
|
||||||
|
std::cout << "Listener: connected!" << std::endl;
|
||||||
|
t->send("$$");
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_disconnected(grbl::transport *t) override {
|
||||||
|
std::cout << "Listener: disconnected!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_line_received(std::string line, grbl::transport *t) override {
|
||||||
|
std::cout << "Listener: -> " << line << std::endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
grbl_listener listener{};
|
||||||
|
|
||||||
|
grbl::tcp_transport transport("192.168.5.39", 23);
|
||||||
|
transport.open(listener);
|
||||||
|
|
||||||
testing::InitGoogleTest(&argc, argv);
|
testing::InitGoogleTest(&argc, argv);
|
||||||
auto result = RUN_ALL_TESTS();
|
auto result = RUN_ALL_TESTS();
|
||||||
if (result) {
|
if (result) {
|
||||||
@@ -621,9 +643,9 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
grbl::program pgm;
|
// grbl::program pgm;
|
||||||
pgm.load("./program.nc");
|
// pgm.load("./program.nc");
|
||||||
pgm.dump(std::cout);
|
// pgm.dump(std::cout);
|
||||||
|
|
||||||
nanogui::init();
|
nanogui::init();
|
||||||
|
|
||||||
|
|||||||
+1414
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user