diff --git a/src/core/temperature.cpp b/src/core/temperature.cpp --- a/src/core/temperature.cpp +++ b/src/core/temperature.cpp @@ -92,46 +92,35 @@ void Temperature::decodeTemp(const QByteArray &msg) { - int bloc = msg.indexOf(QStringLiteral("B:")); - - float firstTargetTemperature = 0; - float secondTargetTemperature = 0; - - QRegularExpression tempRegEx(QStringLiteral("(T:(?\\d+\\.?\\d*))")); - QRegularExpression targetTempRegEx(QStringLiteral("(\\/)(?\\d*)(.+)")); + //Capture after T: until next space + static QRegularExpression tempRegEx(QStringLiteral("T:(?\\d+\\.?\\d*)")); QRegularExpressionMatch tempCheck = tempRegEx.match(QString::fromLatin1(msg)); + //Find T:## /## and store the second set of numbers + static QRegularExpression targetTempRegEx(QStringLiteral("T:[^\\/]*\\/(?\\d+\\.?\\d*)")); QRegularExpressionMatch targetTempCheck = targetTempRegEx.match(QString::fromLatin1(msg)); if (tempCheck.hasMatch()) { setExtruderTemperature(tempCheck.captured(QStringLiteral("extruder")).toFloat()); } if (targetTempCheck.hasMatch()) { - firstTargetTemperature = targetTempCheck.captured(QStringLiteral("extruderTarget")).toFloat(); + setExtruderTargetTemperature(targetTempCheck.captured(QStringLiteral("extruderTarget")).toFloat()); } - if (bloc != -1) { - QRegularExpression bedRegEx(QStringLiteral("(B:(?\\d+\\.?\\d*))")); + if ( msg.indexOf(QStringLiteral("B:")) != -1) { + //Capture after B: until next space + static QRegularExpression bedRegEx(QStringLiteral("B:(?\\d+\\.?\\d*)")); QRegularExpressionMatch bedCheck = bedRegEx.match(QString::fromLatin1(msg)); - QRegularExpression targetBedRegEx(QStringLiteral("B:(.+)(\\/)(?\\d+)")); + //Find B:## /## and store the second set of numbers + static QRegularExpression targetBedRegEx(QStringLiteral("B:[^\\/]*\\/(?\\d+\\.?\\d*)")); QRegularExpressionMatch targetBedCheck = targetBedRegEx.match(QString::fromLatin1(msg)); if (bedCheck.hasMatch()) { setBedTemperature(bedCheck.captured(QStringLiteral("bed")).toFloat()); } if (targetBedCheck.hasMatch()) { - secondTargetTemperature = targetBedCheck.captured(QStringLiteral("bedTarget")).toFloat(); + setBedTargetTemperature(targetBedCheck.captured(QStringLiteral("bedTarget")).toFloat()); } } - //Currently the first value after / is stored in firstTargetTemperature and the second / in secondTargetTemperature - //Because of this we need to check what came first in the return and place the values - //The regex for temperature needs to look at the whole T: or B: block to correctly decode targets - if (bloc < msg.indexOf(QStringLiteral("T:")) && bloc != -1) { - setExtruderTargetTemperature(secondTargetTemperature); - setBedTargetTemperature(firstTargetTemperature); - } else { - setExtruderTargetTemperature(firstTargetTemperature); - setBedTargetTemperature(secondTargetTemperature); - } } diff --git a/unittests/temperaturetests.h b/unittests/temperaturetests.h --- a/unittests/temperaturetests.h +++ b/unittests/temperaturetests.h @@ -33,6 +33,7 @@ void setBedTargetTemperature(); void testDecodeAprinter(); void testDecodeMarlin(); + void testDecodeMarlinCreality(); void testDecodeRepetier(); void testDecodeSmoothie(); void testDecodeSprinter(); diff --git a/unittests/temperaturetests.cpp b/unittests/temperaturetests.cpp --- a/unittests/temperaturetests.cpp +++ b/unittests/temperaturetests.cpp @@ -87,6 +87,15 @@ QVERIFY(temperature->bedTargetTemperature() == 50); } +void TemperatureTests::testDecodeMarlinCreality() +{ + temperature->decodeTemp(QByteArray("ok T:48.8 /215.0 B:57.5 /70.0 T0:48.8 /0.0 @:0 B@:0")); + QVERIFY(temperature->extruderTemperature() == float(48.8)); + QVERIFY(temperature->extruderTargetTemperature() == 215); + QVERIFY(temperature->bedTemperature() == float(57.5)); + QVERIFY(temperature->bedTargetTemperature() == 70); +} + void TemperatureTests::testDecodeRepetier() { temperature->decodeTemp(QByteArray("T:25.47 /230 B:69.42 /80 B@:255 @:0"));