Index: src/core/ifirmware.h =================================================================== --- src/core/ifirmware.h +++ src/core/ifirmware.h @@ -73,7 +73,7 @@ * @param command: Command command to translate * @return firmware specific translated command */ - virtual QByteArray translate(const QString &command); + virtual QByteArray translate(QString command); /** * @brief AtCore Parent of the firmware plugin Index: src/core/ifirmware.cpp =================================================================== --- src/core/ifirmware.cpp +++ src/core/ifirmware.cpp @@ -70,7 +70,7 @@ } } -QByteArray IFirmware::translate(const QString &command) +QByteArray IFirmware::translate(QString command) { - return command.toLocal8Bit(); + return command.append(QStringLiteral("\r\n")).toLocal8Bit(); } Index: src/core/seriallayer.h =================================================================== --- src/core/seriallayer.h +++ src/core/seriallayer.h @@ -120,13 +120,6 @@ */ void push(); - /** - * @brief Check if is a command available - * - * @return bool - */ - bool commandAvailable() const; - /** * @brief Return a QStringList of valids serial baud rates * Index: src/core/seriallayer.cpp =================================================================== --- src/core/seriallayer.cpp +++ src/core/seriallayer.cpp @@ -31,9 +31,6 @@ namespace { -QByteArray _return = QByteArray("\r"); -QByteArray _newLine = QByteArray("\n"); -QByteArray _newLineReturn = QByteArray("\n\r"); QStringList _validBaudRates = { QStringLiteral("9600"), QStringLiteral("14400"), @@ -59,7 +56,6 @@ bool _serialOpened; //!< @param _serialOpened: is serial port opened QSerialPort::SerialPortError _lastError; //!< @param _lastError: the last reported error QByteArray _rawData; //!< @param _rawData: the raw serial data - QVector _rByteCommands; //!< @param _rByteCommand: received Messages QVector _sByteCommands; //!< @param _sByteCommand: sent Messages }; @@ -77,52 +73,51 @@ void SerialLayer::readAllData() { - d->_rawData.append(readAll()); - - //Remove any \r in the string, then split by \n. - //This removes any trailing \r or \n from the commands - // Proper line endings are added when the command is pushed. - d->_rawData = d->_rawData.replace(_return, QByteArray()); - - QList tempList = d->_rawData.split(_newLine.at(0)); - for (auto i = tempList.begin(); i != tempList.end(); ++i) { - // Get finished line to _byteCommands - if (i < tempList.end() - 1) { - d->_rByteCommands.append(*i); - emit receivedCommand(*i); - } else { - d->_rawData.clear(); - d->_rawData.append(*i); + // Some firmwares can return \n, \r, \n\r or \r\n + auto data = QString::fromLatin1(d->_rawData + readAll()); + d->_rawData.clear(); + // Regex to find end of feedback + static const auto finalRegex = QRegExp(QStringLiteral("(\\n|\\r)")); + // Check if we have a complete messsage in the end of our tring + bool hasFinalMessageComplete = data.lastIndexOf(finalRegex) == (data.length() - 1); + // Split all commands + QStringList tempList = data.split(finalRegex); + // If final command is not done, we will wait for it + if(!hasFinalMessageComplete) { + d->_rawData = tempList.takeLast().toLocal8Bit(); + } + for (const auto& command : tempList) { + // Remove empty strings if there is any + if(!command.isEmpty()) { + emit receivedCommand(command.toLocal8Bit()); } } } void SerialLayer::pushCommand(const QByteArray &comm, const QByteArray &term) +{ + pushCommand(comm + term); +} + +void SerialLayer::pushCommand(const QByteArray &comm) { if (!isOpen()) { qCDebug(SERIAL_LAYER) << "Serial not connected !"; return; } - QByteArray tmp = comm + term; - write(tmp); - emit pushedCommand(tmp); - -} -void SerialLayer::pushCommand(const QByteArray &comm) -{ - pushCommand(comm, _newLineReturn); + write(comm); + emit pushedCommand(comm); } void SerialLayer::add(const QByteArray &comm, const QByteArray &term) { - QByteArray tmp = comm + term; - d->_sByteCommands.append(tmp); + add(comm + term); } void SerialLayer::add(const QByteArray &comm) { - add(comm, _newLineReturn); + d->_sByteCommands.append(comm); } void SerialLayer::push() @@ -138,11 +133,6 @@ d->_sByteCommands.clear(); } -bool SerialLayer::commandAvailable() const -{ - return !d->_rByteCommands.isEmpty(); -} - QStringList SerialLayer::validBaudRates() const { return _validBaudRates; Index: src/plugins/grblplugin.h =================================================================== --- src/plugins/grblplugin.h +++ src/plugins/grblplugin.h @@ -55,16 +55,16 @@ */ bool isSdSupported() const override; - /** - * @brief Translate common commands to firmware specific command. - * @param command: command to translate - * @return firmware specific translated command - */ - QByteArray translate(const QString &command) override; - /** * @brief Grbl does not return anything on command execution * @param lastMessage: last message from printer */ void validateCommand(const QString &lastMessage) override; + + /** + * @brief Translate common commands to firmware specific command. + * @param command: command to translate + * @return firmware specific translated command + */ + QByteArray translate(QString command) override; }; Index: src/plugins/grblplugin.cpp =================================================================== --- src/plugins/grblplugin.cpp +++ src/plugins/grblplugin.cpp @@ -51,27 +51,7 @@ } } -QByteArray GrblPlugin::translate(const QString &command) +QByteArray GrblPlugin::translate(QString command) { - QString temp = command; - //Match all G and M commands followed by one or more digits up to and include the space, - //if thats followed by a letter capture any non G or M starting text - //else just grab the digits that follow. - //ex: G28 X Y M1 would capture "G28 X Y" and "M1" - static const auto regEx = QRegularExpression(QStringLiteral("[GM]\\d+.(?(?=\\D)[^GM]+|\\d+)?")); - - QRegularExpressionMatch secondCommand = regEx.match(temp, 1); - - if (secondCommand.hasMatch()) { - QRegularExpressionMatchIterator commandMatch = regEx.globalMatch(temp); - temp.clear(); - while (commandMatch.hasNext()) { - QRegularExpressionMatch t = commandMatch.next(); - temp.append(t.captured().simplified()); - if (commandMatch.hasNext()) { - temp.append(QStringLiteral("\r\n")); - } - } - } - return temp.toLocal8Bit(); + return command.append(QStringLiteral("\n")).toLocal8Bit(); } Index: src/plugins/teacupplugin.h =================================================================== --- src/plugins/teacupplugin.h +++ src/plugins/teacupplugin.h @@ -60,5 +60,5 @@ * @param command: command to translate * @return firmware specific translated command */ - QByteArray translate(const QString &command) override; + QByteArray translate(QString command) override; }; Index: src/plugins/teacupplugin.cpp =================================================================== --- src/plugins/teacupplugin.cpp +++ src/plugins/teacupplugin.cpp @@ -45,7 +45,7 @@ qCDebug(TEACUP_PLUGIN) << name() << " plugin loaded!"; } -QByteArray TeacupPlugin::translate(const QString &command) +QByteArray TeacupPlugin::translate(QString command) { QString temp = command; if (command.contains(QStringLiteral("M109"))) {