From 8c1d2d1f062ffa0eb7de635e1d5b7240a98f66c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Thu, 4 Jan 2024 15:55:55 +0100 Subject: [PATCH] Bugfixes LibreCAL automatic calibration dialog --- .../Calibration/LibreCAL/caldevice.cpp | 54 ++++++++++++++++++- .../Calibration/LibreCAL/caldevice.h | 4 ++ .../Calibration/LibreCAL/librecaldialog.cpp | 7 +-- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.cpp b/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.cpp index eddfa76f..642b6f39 100644 --- a/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.cpp @@ -3,20 +3,59 @@ #include #include - +#include using namespace std; +static QString getLocalDateTimeWithUtcOffset() +{ + QDateTime currentDateTime = QDateTime::currentDateTime(); + int year = currentDateTime.date().year(); + int month = currentDateTime.date().month(); + int day = currentDateTime.date().day(); + int hour = currentDateTime.time().hour(); + int minute = currentDateTime.time().minute(); + int second = currentDateTime.time().second(); + int utcOffset = currentDateTime.offsetFromUtc() / 3600; + int utcOffsetMinute = (currentDateTime.offsetFromUtc() % 3600) / 60; + QString dateTimeString = QString("%1/%2/%3 %4:%5:%6 UTC%7%8:%9") + .arg(year, 4, 10, QChar('0')) + .arg(month, 2, 10, QChar('0')) + .arg(day, 2, 10, QChar('0')) + .arg(hour, 2, 10, QChar('0')) + .arg(minute, 2, 10, QChar('0')) + .arg(second, 2, 10, QChar('0')) + .arg(utcOffset > 0 ? "+" : "-") // Add a plus sign for positive offsets + .arg(qAbs(utcOffset), 2, 10, QChar('0')) + .arg(utcOffsetMinute, 2, 10, QChar('0')); + return dateTimeString; +} + CalDevice::CalDevice(QString serial) : usb(new USBDevice(serial)) { // Check device identification auto id = usb->Query("*IDN?"); - if(!id.startsWith("LibreCAL_")) { + if(!id.startsWith("LibreCAL,")) { delete usb; throw std::runtime_error("Invalid response to *IDN?: "+id.toStdString()); } firmware = usb->Query(":FIRMWARE?"); + QList fw_version = firmware.split("."); + if (fw_version.size() == 3) { + int firmware_major = fw_version[0].toInt(); + int firmware_minor = fw_version[1].toInt(); + this->firmware_major_minor = firmware_major + ((float)firmware_minor/10.0f); + if(this->firmware_major_minor >= 0.2) + { + /* Set Date Time UTC */ + QString LocalDateTimeWithUtcOffset = getLocalDateTimeWithUtcOffset(); + QString ret = usb->Query(":DATE_TIME "+LocalDateTimeWithUtcOffset); + } + } else { + // fw_version invalid + this->firmware_major_minor = 0.0; + } QString ports = usb->Query(":PORTS?"); bool okay; numPorts = ports.toInt(&okay); @@ -119,6 +158,17 @@ bool CalDevice::enterBootloader() return usb->Cmd(":BOOTloader"); } +QString CalDevice::getDateTimeUTC() +{ + if(this->firmware_major_minor >= 0.2) + { + return usb->Query(":DATE_TIME?"); + }else + { + return ""; // Not available + } +} + void CalDevice::loadCoefficientSets(QStringList names) { coeffSets.clear(); diff --git a/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.h b/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.h index d9f23312..587651df 100644 --- a/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.h +++ b/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/caldevice.h @@ -44,6 +44,8 @@ class CalDevice : public QObject QString getFirmware() const; unsigned int getNumPorts() const; + QString getDateTimeUTC(); + bool enterBootloader(); class CoefficientSet { @@ -97,6 +99,8 @@ class CalDevice : public QObject QString firmware; int numPorts; + float firmware_major_minor; + std::vector coeffSets; }; diff --git a/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/librecaldialog.cpp b/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/librecaldialog.cpp index 620e3edf..91f3b0a9 100644 --- a/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/librecaldialog.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Calibration/LibreCAL/librecaldialog.cpp @@ -33,11 +33,12 @@ LibreCALDialog::LibreCALDialog(Calibration *cal) : device = new CalDevice(text); } catch (exception &e) { device = nullptr; + InformationBox::ShowError("Failed to connect", e.what(), this); } if(device) { createPortAssignmentUI(); connect(device, &CalDevice::updateCoefficientsPercent, ui->progressCoeff, &QProgressBar::setValue); - connect(device, &CalDevice::updateCoefficientsDone, [=](bool success){ + connect(device, &CalDevice::updateCoefficientsDone, this, [=](bool success){ busy = false; if(success) { ui->progressCoeff->setValue(100); @@ -48,7 +49,7 @@ LibreCALDialog::LibreCALDialog(Calibration *cal) : ui->lCoefficientStatus->setText("Failed to load coefficients"); } updateCalibrationStartStatus(); - }); + }, Qt::QueuedConnection); ui->cbCoefficients->clear(); ui->cbCoefficients->addItem("Select..."); @@ -389,7 +390,7 @@ void LibreCALDialog::startCalibration() } setTerminationOnAllUsedPorts(CalDevice::Standard(CalDevice::Standard::Type::None)); auto m = throughMeasurements[throughIndex]; - device->setStandard(m->getPort1(), CalDevice::Standard(m->getPort2())); + device->setStandard(portAssignment[m->getPort1()-1], CalDevice::Standard(portAssignment[m->getPort2()-1])); emit cal->startMeasurements({m}); } break;