Communicator banner + single char commands

This commit is contained in:
2023-04-27 18:10:48 +03:00
parent 99cbad2c28
commit 32bda9d55b
3 changed files with 67 additions and 10 deletions
+36 -2
View File
@@ -52,13 +52,20 @@ void grbl::tcp_transport::close() {
is_connected = false;
send_queue = std::queue<std::string>{};
if (listener != nullptr)
if (listener != nullptr) {
listener->on_disconnected(this);
listener = nullptr;
}
fd = -1;
}
}
void grbl::tcp_transport::send(std::string line) {
if (is_connected) {
if (line.at(0) == (char) 0xff)
send_queue.push(line);
else
send_queue.push(line + "\r");
}
}
@@ -93,8 +100,14 @@ void grbl::tcp_transport::worker() {
auto is_eol = buffer[i] == '\r' || buffer[i] == '\n';
if (is_eol) {
if (!is_empty_line(received)) {
// starts with? https://stackoverflow.com/questions/1878001/how-do-i-check-if-a-c-stdstring-starts-with-a-certain-string-and-convert-a
if (received.rfind("Grbl", 0) == 0) {
listener->on_banner(received, this);
} else {
listener->on_line_received(received, this);
}
}
received.clear();
} else {
received.push_back(static_cast<char>(buffer[i]));
@@ -114,7 +127,7 @@ grbl::tcp_transport::~tcp_transport() {
live_check_thread.join();
}
bool grbl::tcp_transport::is_empty_line(std::string line) {
bool grbl::tcp_transport::is_empty_line(const std::string &line) {
for (auto &c: line) {
if (c != '\r' && c != '\n' && c != ' ') {
return false;
@@ -122,3 +135,24 @@ bool grbl::tcp_transport::is_empty_line(std::string line) {
}
return true;
}
void grbl::tcp_transport::send_single_char_command(uint8_t data) const {
if (is_connected)
write(fd, &data, 1);
}
void grbl::tcp_transport::request_realtime_report() {
send_single_char_command(0x80);
}
void grbl::tcp_transport::request_cycle_start() {
send_single_char_command(0x81);
}
void grbl::tcp_transport::request_feed_hold() {
send_single_char_command(0x82);
}
void grbl::tcp_transport::parser_state_report() {
send_single_char_command(0x83);
}
+12 -1
View File
@@ -12,6 +12,7 @@ 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;
virtual void on_banner(std::string version, transport* ) = 0;
};
struct transport {
@@ -28,6 +29,14 @@ struct tcp_transport : public transport {
void close() override;
void send(std::string line) override;
void request_realtime_report();
void request_cycle_start();
void request_feed_hold();
void parser_state_report();
private:
void worker();
@@ -40,7 +49,9 @@ private:
std::thread live_check_thread;
std::queue<std::string> send_queue;
volatile bool should_quit = false;
bool is_empty_line(std::string line);
static bool is_empty_line(const std::string& line);
void send_single_char_command(uint8_t data) const;
};
}
+13 -1
View File
@@ -616,7 +616,9 @@ private:
struct grbl_listener : public grbl::transport_callbacks {
void on_connected(grbl::transport *t) override {
std::cout << "Listener: connected!" << std::endl;
t->send("$$");
// telnet handshake so that we get the banner. banner won't be coming otherwise
// t->send("\xff\xfd\x18\xff\xfd\x20\xff\xfd\x23\xff\xfd\x27");
t->send("\xff\xfd\x18");
}
void on_disconnected(grbl::transport *t) override {
@@ -626,6 +628,11 @@ struct grbl_listener : public grbl::transport_callbacks {
void on_line_received(std::string line, grbl::transport *t) override {
std::cout << "Listener: -> " << line << std::endl;
}
void on_banner(std::string version, grbl::transport *t) override {
std::cout << "Banner: " << version << std::endl;
t->send("$$");
}
};
int main(int argc, char **argv) {
@@ -641,6 +648,11 @@ int main(int argc, char **argv) {
exit(result);
}
transport.request_realtime_report();
transport.request_cycle_start();
transport.request_feed_hold();
transport.parser_state_report();
try {
// grbl::program pgm;