25 #include "GrblDeviceBase.hpp"
27 #include <ccut/Regex.hpp>
28 #include <ccut/utils.hpp>
29 #include <logger/Logger.hpp>
31 #include "GrblParser.hpp"
34 using namespace std::chrono;
35 using namespace logger;
37 static const std::string s_loggerCat{
"smc:grbldevice"};
38 static const std::string s_crnl(
"\r\n");
40 const milliseconds GrblDeviceBase::defaultSendTimeout(1000);
48 while (takeLine(line))
50 debug(s_loggerCat) <<
"<~- " << line;
57 std::function<
bool(
const std::string &)> msg,
58 const std::chrono::milliseconds &timeout)
60 const steady_clock::time_point deadline = steady_clock::now() + timeout;
62 debug(s_loggerCat) <<
"--> " << cmd;
68 bool msgConfirmed =
false;
69 while (steady_clock::now() <= deadline)
71 steady_clock::time_point now = steady_clock::now();
72 const milliseconds remaining = (now < deadline) ?
73 duration_cast<milliseconds>(deadline - now) :
79 while (this->takeLine(line))
81 debug(s_loggerCat) <<
"<-- " << line;
84 case GrblParser::LineType::OkReply:
86 if (msg && !msgConfirmed)
87 return grbl::CustomErrorCode::NoPayload;
90 case GrblParser::LineType::ErrorReply:
95 error(s_loggerCat) <<
"Cannot parse error code: " << line;
96 return grbl::CustomErrorCode::InternalError;
101 if (
bool(msg) && msg(line))
106 catch (
const grbl::ErrorCode &rc)
110 catch (std::exception &ex)
113 <<
"exception in client callback: " << ex.what();
119 return grbl::CustomErrorCode::Timeout;
123 const std::string &cmd,
124 std::function<
bool(
const std::string &)> msg,
125 const std::chrono::milliseconds &timeout)
127 const steady_clock::time_point deadline = steady_clock::now() + timeout;
129 debug(s_loggerCat) <<
"->> " << cmd;
130 if (cmd.length() > 0)
133 return grbl::ErrorCode{0};
135 while (steady_clock::now() <= deadline)
137 steady_clock::time_point now = steady_clock::now();
138 const milliseconds remaining = (now < deadline) ?
139 duration_cast<milliseconds>(deadline - now) :
140 milliseconds::zero();
145 while (this->takeLine(line))
147 debug(s_loggerCat) <<
"<<- " << line;
153 catch (
const grbl::ErrorCode &rc)
157 catch (std::exception &ex)
160 <<
"exception in client callback: " << ex.what();
164 return grbl::CustomErrorCode::Timeout;
169 return m_buffer.takeLine(line);
virtual grbl::ErrorCode sendRealtime(const std::string &cmd, std::function< bool(const std::string &)> msg={}, const std::chrono::milliseconds &timeout=defaultSendTimeout)
send a realtime-command wait for reply
virtual bool takeLine(std::string &line)
consume line from buffer
virtual grbl::ErrorCode send(const std::string &cmd, std::function< bool(const std::string &)> msg={}, const std::chrono::milliseconds &timeout=defaultSendTimeout)
send a command wait for reply
void fetch(const std::chrono::milliseconds &timeout)
wait for data and process it
static LineType getLineType(const std::string &line)
detect line type
static bool parseErrorReply(const std::string &line, grbl::ErrorCode &errorCode)
parse an error message