24 #ifndef MGRBLDEVICEBASE_CPP__
25 #define MGRBLDEVICEBASE_CPP__
29 #include <logger/Logger.hpp>
31 #include "GrblParser.hpp"
32 #include "MGrblDeviceBase.hpp"
36 template<
typename DeviceBase>
37 grbl::ErrorCode MGrblDeviceBase<DeviceBase>::send(
38 const std::string &cmd,
39 std::function<
bool(
const std::string &)> msg,
40 const std::chrono::milliseconds &timeout)
42 static const std::string s_loggerCat{
"smc:mgrbldevice"};
44 const std::chrono::steady_clock::time_point deadline =
45 std::chrono::steady_clock::now() + timeout;
46 logger::debug(s_loggerCat) <<
"--> " << cmd;
50 this->write(cmd +
"\r\n");
51 const char sender = cmd[0];
53 bool msgConfirmed =
false;
56 std::chrono::steady_clock::time_point now =
57 std::chrono::steady_clock::now();
58 const std::chrono::milliseconds remaining = (now < deadline) ?
59 std::chrono::duration_cast<std::chrono::milliseconds>(deadline -
61 std::chrono::milliseconds::zero();
63 this->wait(remaining);
66 while (this->takeLine(line))
68 logger::debug(s_loggerCat) <<
"<-- " << line;
69 if (line[0] == sender)
71 if (line.compare(1, 2,
"ok") == 0)
73 if (msg && !msgConfirmed)
74 return grbl::CustomErrorCode::NoPayload;
77 else if (line.compare(1, 6,
"error:") == 0)
79 grbl::ErrorCode errorCode;
82 logger::error(s_loggerCat)
83 <<
"Cannot parse error code: " << line;
84 return grbl::CustomErrorCode::InternalError;
89 if (
bool(msg) && msg(line))
92 this->dataSignal(line);
94 catch (grbl::ErrorCode &rc)
98 catch (std::exception &ex)
100 logger::error(s_loggerCat)
101 <<
"exception in client callback: " << ex.what();
104 }
while (std::chrono::steady_clock::now() <= deadline);
105 return grbl::CustomErrorCode::Timeout;
static bool parseErrorReply(const std::string &line, grbl::ErrorCode &errorCode)
parse an error message