2014-11-26 21:30:25 +00:00
|
|
|
#include "GCodeSender.hpp"
|
|
|
|
#include <iostream>
|
2014-12-27 21:57:20 +00:00
|
|
|
#include <istream>
|
|
|
|
#include <string>
|
2018-07-24 15:41:57 +00:00
|
|
|
#include <thread>
|
2014-12-27 21:57:20 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2015-11-02 19:34:36 +00:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2014-12-27 21:57:20 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-11-26 21:30:25 +00:00
|
|
|
|
2018-02-12 19:44:06 +00:00
|
|
|
#if defined(__APPLE__) || defined(__OpenBSD__)
|
2016-01-03 16:35:59 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
2018-02-12 19:44:06 +00:00
|
|
|
#ifdef __APPLE__
|
2014-12-31 18:10:46 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <IOKit/serial/ioss.h>
|
|
|
|
#endif
|
2018-02-12 19:44:06 +00:00
|
|
|
#ifdef __linux__
|
2014-12-31 18:10:46 +00:00
|
|
|
#include <sys/ioctl.h>
|
2018-02-12 19:44:06 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include "/usr/include/asm-generic/ioctls.h"
|
|
|
|
|
|
|
|
/* The following definitions are kindly borrowed from:
|
|
|
|
/usr/include/asm-generic/termbits.h
|
|
|
|
Unfortunately we cannot just include that one because
|
|
|
|
it would redefine the "struct termios" already defined
|
|
|
|
the <termios.h> already included by Boost.ASIO. */
|
|
|
|
#define K_NCCS 19
|
|
|
|
struct termios2 {
|
|
|
|
tcflag_t c_iflag;
|
|
|
|
tcflag_t c_oflag;
|
|
|
|
tcflag_t c_cflag;
|
|
|
|
tcflag_t c_lflag;
|
|
|
|
cc_t c_line;
|
|
|
|
cc_t c_cc[K_NCCS];
|
|
|
|
speed_t c_ispeed;
|
|
|
|
speed_t c_ospeed;
|
|
|
|
};
|
|
|
|
#define BOTHER CBAUDEX
|
|
|
|
|
2014-12-31 18:10:46 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-26 10:49:19 +00:00
|
|
|
//#define DEBUG_SERIAL
|
|
|
|
#ifdef DEBUG_SERIAL
|
2018-06-04 15:47:01 +00:00
|
|
|
#include <cstdlib>
|
2015-12-26 10:49:19 +00:00
|
|
|
#include <fstream>
|
|
|
|
std::fstream fs;
|
|
|
|
#endif
|
|
|
|
|
2016-03-13 17:27:02 +00:00
|
|
|
#define KEEP_SENT 20
|
|
|
|
|
2014-11-26 21:30:25 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-12-31 18:10:46 +00:00
|
|
|
GCodeSender::GCodeSender()
|
2015-11-03 22:08:16 +00:00
|
|
|
: io(), serial(io), can_send(false), sent(0), open(false), error(false),
|
|
|
|
connected(false), queue_paused(false)
|
2018-06-04 15:47:01 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_SERIAL
|
|
|
|
std::srand(std::time(nullptr));
|
|
|
|
#endif
|
|
|
|
}
|
2014-12-31 18:10:46 +00:00
|
|
|
|
|
|
|
GCodeSender::~GCodeSender()
|
2014-11-26 21:30:25 +00:00
|
|
|
{
|
2014-12-31 18:10:46 +00:00
|
|
|
this->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GCodeSender::connect(std::string devname, unsigned int baud_rate)
|
|
|
|
{
|
2015-11-19 12:17:52 +00:00
|
|
|
this->disconnect();
|
|
|
|
|
2015-11-03 22:08:16 +00:00
|
|
|
this->set_error_status(false);
|
2014-12-31 18:10:46 +00:00
|
|
|
try {
|
|
|
|
this->serial.open(devname);
|
2018-02-12 19:44:06 +00:00
|
|
|
|
|
|
|
this->serial.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::odd));
|
|
|
|
this->serial.set_option(boost::asio::serial_port_base::character_size(boost::asio::serial_port_base::character_size(8)));
|
|
|
|
this->serial.set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none));
|
|
|
|
this->serial.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
|
|
|
|
this->set_baud_rate(baud_rate);
|
|
|
|
|
|
|
|
this->serial.close();
|
|
|
|
this->serial.open(devname);
|
|
|
|
this->serial.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
|
|
|
|
|
|
|
|
// set baud rate again because set_option overwrote it
|
|
|
|
this->set_baud_rate(baud_rate);
|
|
|
|
this->open = true;
|
|
|
|
this->reset();
|
2017-08-18 19:09:53 +00:00
|
|
|
} catch (boost::system::system_error &) {
|
2015-11-03 22:08:16 +00:00
|
|
|
this->set_error_status(true);
|
2014-12-31 18:10:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-24 12:07:38 +00:00
|
|
|
// a reset firmware expect line numbers to start again from 1
|
|
|
|
this->sent = 0;
|
|
|
|
this->last_sent.clear();
|
|
|
|
|
2015-12-26 10:49:19 +00:00
|
|
|
/* Initialize debugger */
|
|
|
|
#ifdef DEBUG_SERIAL
|
|
|
|
fs.open("serial.txt", std::fstream::out | std::fstream::trunc);
|
|
|
|
#endif
|
|
|
|
|
2014-12-27 21:57:20 +00:00
|
|
|
// this gives some work to the io_service before it is started
|
|
|
|
// (post() runs the supplied function in its thread)
|
|
|
|
this->io.post(boost::bind(&GCodeSender::do_read, this));
|
|
|
|
|
|
|
|
// start reading in the background thread
|
2017-08-18 19:09:53 +00:00
|
|
|
boost::thread t(boost::bind(&boost::asio::io_service::run, &this->io));
|
2014-12-27 21:57:20 +00:00
|
|
|
this->background_thread.swap(t);
|
2014-12-31 18:10:46 +00:00
|
|
|
|
2018-02-12 19:44:06 +00:00
|
|
|
// always send a M105 to check for connection because firmware might be silent on connect
|
|
|
|
//FIXME Vojtech: This is being sent too early, leading to line number synchronization issues,
|
|
|
|
// from which the GCodeSender never recovers.
|
|
|
|
// this->send("M105", true);
|
|
|
|
|
2014-12-31 18:10:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::set_baud_rate(unsigned int baud_rate)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// This does not support speeds > 115200
|
2017-08-18 19:09:53 +00:00
|
|
|
this->serial.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
|
|
|
|
} catch (boost::system::system_error &) {
|
2014-12-31 18:10:46 +00:00
|
|
|
boost::asio::serial_port::native_handle_type handle = this->serial.native_handle();
|
|
|
|
|
|
|
|
#if __APPLE__
|
|
|
|
termios ios;
|
|
|
|
::tcgetattr(handle, &ios);
|
|
|
|
::cfsetspeed(&ios, baud_rate);
|
|
|
|
speed_t newSpeed = baud_rate;
|
|
|
|
ioctl(handle, IOSSIOSPEED, &newSpeed);
|
|
|
|
::tcsetattr(handle, TCSANOW, &ios);
|
2016-01-03 16:35:59 +00:00
|
|
|
#elif __linux
|
2018-02-12 19:44:06 +00:00
|
|
|
termios2 ios;
|
|
|
|
if (ioctl(handle, TCGETS2, &ios))
|
|
|
|
printf("Error in TCGETS2: %s\n", strerror(errno));
|
|
|
|
ios.c_ispeed = ios.c_ospeed = baud_rate;
|
|
|
|
ios.c_cflag &= ~CBAUD;
|
|
|
|
ios.c_cflag |= BOTHER | CLOCAL | CREAD;
|
|
|
|
ios.c_cc[VMIN] = 1; // Minimum of characters to read, prevents eof errors when 0 bytes are read
|
|
|
|
ios.c_cc[VTIME] = 1;
|
|
|
|
if (ioctl(handle, TCSETS2, &ios))
|
|
|
|
printf("Error in TCSETS2: %s\n", strerror(errno));
|
|
|
|
|
2016-01-03 16:35:59 +00:00
|
|
|
#elif __OpenBSD__
|
|
|
|
struct termios ios;
|
|
|
|
::tcgetattr(handle, &ios);
|
|
|
|
::cfsetspeed(&ios, baud_rate);
|
|
|
|
if (::tcsetattr(handle, TCSAFLUSH, &ios) != 0)
|
|
|
|
printf("Failed to set baud rate: %s\n", strerror(errno));
|
2014-12-31 18:10:46 +00:00
|
|
|
#else
|
2020-09-14 14:27:55 +00:00
|
|
|
//throw Slic3r::InvalidArgument("OS does not currently support custom bauds");
|
2014-12-31 18:10:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-27 21:57:20 +00:00
|
|
|
GCodeSender::disconnect()
|
|
|
|
{
|
|
|
|
if (!this->open) return;
|
|
|
|
this->open = false;
|
|
|
|
this->connected = false;
|
|
|
|
this->io.post(boost::bind(&GCodeSender::do_close, this));
|
|
|
|
this->background_thread.join();
|
|
|
|
this->io.reset();
|
2015-12-17 23:56:56 +00:00
|
|
|
/*
|
2014-12-27 21:57:20 +00:00
|
|
|
if (this->error_status()) {
|
|
|
|
throw(boost::system::system_error(boost::system::error_code(),
|
|
|
|
"Error while closing the device"));
|
|
|
|
}
|
2015-12-17 23:56:56 +00:00
|
|
|
*/
|
2015-12-26 10:49:19 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SERIAL
|
2018-02-12 19:44:06 +00:00
|
|
|
fs << "DISCONNECTED" << std::endl << std::flush;
|
2015-12-26 10:49:19 +00:00
|
|
|
fs.close();
|
|
|
|
#endif
|
2014-12-27 21:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GCodeSender::is_connected() const
|
2014-11-26 21:30:25 +00:00
|
|
|
{
|
2014-12-27 21:57:20 +00:00
|
|
|
return this->connected;
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:34:36 +00:00
|
|
|
bool
|
|
|
|
GCodeSender::wait_connected(unsigned int timeout) const
|
|
|
|
{
|
|
|
|
using namespace boost::posix_time;
|
|
|
|
ptime t0 = second_clock::local_time() + seconds(timeout);
|
|
|
|
while (!this->connected) {
|
|
|
|
if (second_clock::local_time() > t0) return false;
|
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-27 21:57:20 +00:00
|
|
|
size_t
|
|
|
|
GCodeSender::queue_size() const
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
|
|
|
return this->queue.size();
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 22:25:55 +00:00
|
|
|
void
|
|
|
|
GCodeSender::pause_queue()
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
|
|
|
this->queue_paused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::resume_queue()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
|
|
|
this->queue_paused = false;
|
|
|
|
}
|
|
|
|
this->send();
|
|
|
|
}
|
|
|
|
|
2015-11-02 00:18:05 +00:00
|
|
|
void
|
2015-11-08 09:10:54 +00:00
|
|
|
GCodeSender::purge_queue(bool priority)
|
2015-11-02 00:18:05 +00:00
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
2015-11-08 09:10:54 +00:00
|
|
|
if (priority) {
|
|
|
|
// clear priority queue
|
2016-03-13 17:27:02 +00:00
|
|
|
std::list<std::string> empty;
|
2015-11-08 09:10:54 +00:00
|
|
|
std::swap(this->priqueue, empty);
|
|
|
|
} else {
|
2015-11-02 00:18:05 +00:00
|
|
|
// clear queue
|
2016-03-13 17:27:02 +00:00
|
|
|
std::queue<std::string> empty;
|
2015-11-02 00:18:05 +00:00
|
|
|
std::swap(this->queue, empty);
|
2015-11-08 09:10:54 +00:00
|
|
|
this->queue_paused = false;
|
2015-11-02 00:18:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 22:18:23 +00:00
|
|
|
// purge log and return its contents
|
|
|
|
std::vector<std::string>
|
|
|
|
GCodeSender::purge_log()
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->log_mutex);
|
|
|
|
std::vector<std::string> retval;
|
|
|
|
retval.reserve(this->log.size());
|
|
|
|
while (!this->log.empty()) {
|
|
|
|
retval.push_back(this->log.front());
|
|
|
|
this->log.pop();
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
GCodeSender::getT() const
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->log_mutex);
|
|
|
|
return this->T;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
GCodeSender::getB() const
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->log_mutex);
|
|
|
|
return this->B;
|
|
|
|
}
|
|
|
|
|
2014-11-26 21:30:25 +00:00
|
|
|
void
|
2014-12-27 21:57:20 +00:00
|
|
|
GCodeSender::do_close()
|
2014-11-26 21:30:25 +00:00
|
|
|
{
|
2015-11-03 22:08:16 +00:00
|
|
|
this->set_error_status(false);
|
2014-12-27 21:57:20 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
this->serial.cancel(ec);
|
|
|
|
if (ec) this->set_error_status(true);
|
|
|
|
this->serial.close(ec);
|
|
|
|
if (ec) this->set_error_status(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::set_error_status(bool e)
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->error_mutex);
|
|
|
|
this->error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GCodeSender::error_status() const
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->error_mutex);
|
|
|
|
return this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::do_read()
|
|
|
|
{
|
|
|
|
// read one line
|
2017-08-18 19:09:53 +00:00
|
|
|
boost::asio::async_read_until(
|
2014-12-27 21:57:20 +00:00
|
|
|
this->serial,
|
|
|
|
this->read_buffer,
|
|
|
|
'\n',
|
|
|
|
boost::bind(
|
|
|
|
&GCodeSender::on_read,
|
|
|
|
this,
|
2017-08-18 19:09:53 +00:00
|
|
|
boost::asio::placeholders::error,
|
|
|
|
boost::asio::placeholders::bytes_transferred
|
2014-12-27 21:57:20 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::on_read(const boost::system::error_code& error,
|
|
|
|
size_t bytes_transferred)
|
|
|
|
{
|
2015-11-03 22:08:16 +00:00
|
|
|
this->set_error_status(false);
|
2014-12-27 21:57:20 +00:00
|
|
|
if (error) {
|
2018-02-12 19:44:06 +00:00
|
|
|
#ifdef __APPLE__
|
2015-11-19 12:17:52 +00:00
|
|
|
if (error.value() == 45) {
|
|
|
|
// OS X bug: http://osdir.com/ml/lib.boost.asio.user/2008-08/msg00004.html
|
|
|
|
this->do_read();
|
2018-02-12 19:44:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// printf("ERROR: [%d] %s\n", error.value(), error.message().c_str());
|
|
|
|
// error can be true even because the serial port was closed.
|
|
|
|
// In this case it is not a real error, so ignore.
|
|
|
|
if (this->open) {
|
|
|
|
this->do_close();
|
|
|
|
this->set_error_status(true);
|
2014-12-27 21:57:20 +00:00
|
|
|
}
|
|
|
|
return;
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-04 22:18:23 +00:00
|
|
|
std::istream is(&this->read_buffer);
|
|
|
|
std::string line;
|
|
|
|
std::getline(is, line);
|
2015-11-19 12:17:52 +00:00
|
|
|
if (!line.empty()) {
|
2015-12-26 10:49:19 +00:00
|
|
|
#ifdef DEBUG_SERIAL
|
2016-03-13 17:27:02 +00:00
|
|
|
fs << "<< " << line << std::endl << std::flush;
|
2015-12-26 10:49:19 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-19 12:17:52 +00:00
|
|
|
// note that line might contain \r at its end
|
|
|
|
// parse incoming line
|
|
|
|
if (!this->connected
|
|
|
|
&& (boost::starts_with(line, "start")
|
|
|
|
|| boost::starts_with(line, "Grbl ")
|
|
|
|
|| boost::starts_with(line, "ok")
|
|
|
|
|| boost::contains(line, "T:"))) {
|
|
|
|
this->connected = true;
|
2015-01-04 17:17:15 +00:00
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
|
|
|
this->can_send = true;
|
|
|
|
}
|
2014-12-27 21:57:20 +00:00
|
|
|
this->send();
|
2015-11-19 12:17:52 +00:00
|
|
|
} else if (boost::starts_with(line, "ok")) {
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
|
|
|
this->can_send = true;
|
|
|
|
}
|
|
|
|
this->send();
|
|
|
|
} else if (boost::istarts_with(line, "resend") // Marlin uses "Resend: "
|
|
|
|
|| boost::istarts_with(line, "rs")) {
|
|
|
|
// extract the first number from line
|
|
|
|
boost::algorithm::trim_left_if(line, !boost::algorithm::is_digit());
|
|
|
|
size_t toresend = boost::lexical_cast<size_t>(line.substr(0, line.find_first_not_of("0123456789")));
|
2018-06-04 15:47:01 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SERIAL
|
|
|
|
fs << "!! line num out of sync: toresend = " << toresend << ", sent = " << sent << ", last_sent.size = " << last_sent.size() << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (toresend > this->sent - this->last_sent.size() && toresend <= this->sent) {
|
2015-11-19 12:17:52 +00:00
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
2016-03-13 17:27:02 +00:00
|
|
|
|
2018-06-04 15:47:01 +00:00
|
|
|
const auto lines_to_resend = this->sent - toresend + 1;
|
|
|
|
#ifdef DEBUG_SERIAL
|
|
|
|
fs << "!! resending " << lines_to_resend << " lines" << std::endl;
|
|
|
|
#endif
|
2016-03-13 17:27:02 +00:00
|
|
|
// move the unsent lines to priqueue
|
|
|
|
this->priqueue.insert(
|
|
|
|
this->priqueue.begin(), // insert at the beginning
|
2018-06-04 15:47:01 +00:00
|
|
|
this->last_sent.begin() + this->last_sent.size() - lines_to_resend,
|
2016-03-13 17:27:02 +00:00
|
|
|
this->last_sent.end()
|
|
|
|
);
|
|
|
|
|
|
|
|
// we can empty last_sent because it's not useful anymore
|
|
|
|
this->last_sent.clear();
|
|
|
|
|
|
|
|
// start resending with the requested line number
|
|
|
|
this->sent = toresend - 1;
|
2015-11-19 12:17:52 +00:00
|
|
|
this->can_send = true;
|
|
|
|
}
|
|
|
|
this->send();
|
|
|
|
} else {
|
2020-06-16 11:13:51 +00:00
|
|
|
printf("Cannot resend %zu (oldest we have is %zu)\n", toresend, this->sent - this->last_sent.size());
|
2015-11-19 12:17:52 +00:00
|
|
|
}
|
|
|
|
} else if (boost::starts_with(line, "wait")) {
|
|
|
|
// ignore
|
2014-12-27 21:57:20 +00:00
|
|
|
} else {
|
2015-11-19 12:17:52 +00:00
|
|
|
// push any other line into the log
|
|
|
|
boost::lock_guard<boost::mutex> l(this->log_mutex);
|
|
|
|
this->log.push(line);
|
2014-12-27 21:57:20 +00:00
|
|
|
}
|
2015-01-04 22:18:23 +00:00
|
|
|
|
2015-11-19 12:17:52 +00:00
|
|
|
// parse temperature info
|
|
|
|
{
|
|
|
|
size_t pos = line.find("T:");
|
2015-01-04 22:18:23 +00:00
|
|
|
if (pos != std::string::npos && line.size() > pos + 2) {
|
2015-11-19 12:17:52 +00:00
|
|
|
// we got temperature info
|
|
|
|
boost::lock_guard<boost::mutex> l(this->log_mutex);
|
|
|
|
this->T = line.substr(pos+2, line.find_first_not_of("0123456789.", pos+2) - (pos+2));
|
|
|
|
|
|
|
|
pos = line.find("B:");
|
|
|
|
if (pos != std::string::npos && line.size() > pos + 2) {
|
|
|
|
// we got bed temperature info
|
|
|
|
this->B = line.substr(pos+2, line.find_first_not_of("0123456789.", pos+2) - (pos+2));
|
|
|
|
}
|
2015-01-04 22:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
2014-12-27 21:57:20 +00:00
|
|
|
this->do_read();
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-04 17:17:15 +00:00
|
|
|
GCodeSender::send(const std::vector<std::string> &lines, bool priority)
|
2014-11-26 21:30:25 +00:00
|
|
|
{
|
2014-12-27 21:57:20 +00:00
|
|
|
// append lines to queue
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
2015-01-04 17:17:15 +00:00
|
|
|
for (std::vector<std::string>::const_iterator line = lines.begin(); line != lines.end(); ++line) {
|
|
|
|
if (priority) {
|
2016-03-13 17:27:02 +00:00
|
|
|
this->priqueue.push_back(*line);
|
2015-01-04 17:17:15 +00:00
|
|
|
} else {
|
|
|
|
this->queue.push(*line);
|
|
|
|
}
|
|
|
|
}
|
2014-12-27 21:57:20 +00:00
|
|
|
}
|
|
|
|
this->send();
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-04 17:17:15 +00:00
|
|
|
GCodeSender::send(const std::string &line, bool priority)
|
2014-11-26 21:30:25 +00:00
|
|
|
{
|
2014-12-27 21:57:20 +00:00
|
|
|
// append line to queue
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
2015-01-04 17:17:15 +00:00
|
|
|
if (priority) {
|
2016-03-13 17:27:02 +00:00
|
|
|
this->priqueue.push_back(line);
|
2015-01-04 17:17:15 +00:00
|
|
|
} else {
|
|
|
|
this->queue.push(line);
|
|
|
|
}
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
2014-12-27 21:57:20 +00:00
|
|
|
this->send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::send()
|
2015-11-19 12:17:52 +00:00
|
|
|
{
|
|
|
|
this->io.post(boost::bind(&GCodeSender::do_send, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::do_send()
|
2014-12-27 21:57:20 +00:00
|
|
|
{
|
2015-01-04 17:17:15 +00:00
|
|
|
boost::lock_guard<boost::mutex> l(this->queue_mutex);
|
|
|
|
|
2014-12-27 21:57:20 +00:00
|
|
|
// printer is not connected or we're still waiting for the previous ack
|
|
|
|
if (!this->can_send) return;
|
|
|
|
|
2016-03-13 17:27:02 +00:00
|
|
|
std::string line;
|
2015-01-04 17:17:15 +00:00
|
|
|
while (!this->priqueue.empty() || (!this->queue.empty() && !this->queue_paused)) {
|
|
|
|
if (!this->priqueue.empty()) {
|
|
|
|
line = this->priqueue.front();
|
2016-03-13 17:27:02 +00:00
|
|
|
this->priqueue.pop_front();
|
2015-01-04 17:17:15 +00:00
|
|
|
} else {
|
|
|
|
line = this->queue.front();
|
|
|
|
this->queue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip comments
|
2015-12-06 10:17:58 +00:00
|
|
|
size_t comment_pos = line.find_first_of(';');
|
|
|
|
if (comment_pos != std::string::npos)
|
|
|
|
line.erase(comment_pos, std::string::npos);
|
2015-01-04 17:17:15 +00:00
|
|
|
boost::algorithm::trim(line);
|
|
|
|
|
|
|
|
// if line is not empty, send it
|
2016-03-13 17:27:02 +00:00
|
|
|
if (!line.empty()) break;
|
2015-01-04 17:17:15 +00:00
|
|
|
// if line is empty, process next item in queue
|
|
|
|
}
|
2016-03-13 17:27:02 +00:00
|
|
|
if (line.empty()) return;
|
|
|
|
|
2015-11-04 13:00:37 +00:00
|
|
|
// compute full line
|
2018-02-12 19:44:06 +00:00
|
|
|
++ this->sent;
|
2018-06-04 15:47:01 +00:00
|
|
|
#ifndef DEBUG_SERIAL
|
|
|
|
const auto line_num = this->sent;
|
|
|
|
#else
|
|
|
|
// In DEBUG_SERIAL mode, test line re-synchronization by sending bad line number 1/4 of the time
|
|
|
|
const auto line_num = std::rand() < RAND_MAX/4 ? 0 : this->sent;
|
|
|
|
#endif
|
|
|
|
std::string full_line = "N" + boost::lexical_cast<std::string>(line_num) + " " + line;
|
2015-11-04 13:00:37 +00:00
|
|
|
|
2014-12-27 21:57:20 +00:00
|
|
|
// calculate checksum
|
|
|
|
int cs = 0;
|
2015-11-04 13:00:37 +00:00
|
|
|
for (std::string::const_iterator it = full_line.begin(); it != full_line.end(); ++it)
|
2014-12-27 21:57:20 +00:00
|
|
|
cs = cs ^ *it;
|
|
|
|
|
2015-11-04 13:00:37 +00:00
|
|
|
// write line to device
|
2015-11-19 12:17:52 +00:00
|
|
|
full_line += "*";
|
|
|
|
full_line += boost::lexical_cast<std::string>(cs);
|
|
|
|
full_line += "\n";
|
2015-12-26 10:49:19 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SERIAL
|
2016-03-13 17:27:02 +00:00
|
|
|
fs << ">> " << full_line << std::flush;
|
2015-12-26 10:49:19 +00:00
|
|
|
#endif
|
2016-03-13 17:27:02 +00:00
|
|
|
|
|
|
|
this->last_sent.push_back(line);
|
|
|
|
this->can_send = false;
|
|
|
|
|
2018-06-04 15:47:01 +00:00
|
|
|
while (this->last_sent.size() > KEEP_SENT) {
|
|
|
|
this->last_sent.pop_front();
|
|
|
|
}
|
2016-03-13 17:27:02 +00:00
|
|
|
|
2017-08-18 19:09:53 +00:00
|
|
|
// we can't supply boost::asio::buffer(full_line) to async_write() because full_line is on the
|
2016-03-13 17:27:02 +00:00
|
|
|
// stack and the buffer would lose its underlying storage causing memory corruption
|
|
|
|
std::ostream os(&this->write_buffer);
|
|
|
|
os << full_line;
|
2017-08-18 19:09:53 +00:00
|
|
|
boost::asio::async_write(this->serial, this->write_buffer, boost::bind(&GCodeSender::on_write, this, boost::asio::placeholders::error,
|
2016-03-13 17:27:02 +00:00
|
|
|
boost::asio::placeholders::bytes_transferred));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::on_write(const boost::system::error_code& error,
|
|
|
|
size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
this->set_error_status(false);
|
|
|
|
if (error) {
|
|
|
|
if (this->open) {
|
|
|
|
this->do_close();
|
|
|
|
this->set_error_status(true);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->do_send();
|
2014-11-26 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 12:17:52 +00:00
|
|
|
void
|
|
|
|
GCodeSender::set_DTR(bool on)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__SYMBIAN32__)
|
2017-08-18 19:09:53 +00:00
|
|
|
boost::asio::serial_port_service::native_handle_type handle = this->serial.native_handle();
|
2015-11-19 12:17:52 +00:00
|
|
|
if (on)
|
|
|
|
EscapeCommFunction(handle, SETDTR);
|
|
|
|
else
|
|
|
|
EscapeCommFunction(handle, CLRDTR);
|
|
|
|
#else
|
|
|
|
int fd = this->serial.native_handle();
|
|
|
|
int status;
|
|
|
|
ioctl(fd, TIOCMGET, &status);
|
|
|
|
if (on)
|
|
|
|
status |= TIOCM_DTR;
|
|
|
|
else
|
|
|
|
status &= ~TIOCM_DTR;
|
|
|
|
ioctl(fd, TIOCMSET, &status);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GCodeSender::reset()
|
|
|
|
{
|
2018-07-24 15:41:57 +00:00
|
|
|
set_DTR(false);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
set_DTR(true);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
set_DTR(false);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
2015-11-19 12:17:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 19:09:53 +00:00
|
|
|
} // namespace Slic3r
|