diff --git a/Documentation/UserManual/SCPI_Examples/libreVNA.py b/Documentation/UserManual/SCPI_Examples/libreVNA.py index 2365b98..b06334e 100644 --- a/Documentation/UserManual/SCPI_Examples/libreVNA.py +++ b/Documentation/UserManual/SCPI_Examples/libreVNA.py @@ -1,10 +1,12 @@ import socket from asyncio import IncompleteReadError # only import the exception class +import time class SocketStreamReader: def __init__(self, sock: socket.socket): self._sock = sock self._recv_buffer = bytearray() + self.timeout = 1.0 def read(self, num_bytes: int = -1) -> bytes: raise NotImplementedError @@ -32,10 +34,13 @@ class SocketStreamReader: bytes_read = self._recv_into(memoryview(buf)) assert bytes_read == len(buf) + timeout = time.time() + self.timeout while True: idx = buf.find(separator, start) if idx != -1: break + elif time.time() > timeout: + raise Exception("Timed out waiting for response from GUI") start = len(self._recv_buffer) bytes_read = self._recv_into(memoryview(chunk)) @@ -53,7 +58,10 @@ class SocketStreamReader: self._recv_buffer = self._recv_buffer[bytes_read:] if bytes_read == len(view): return bytes_read - bytes_read += self._sock.recv_into(view[bytes_read:]) + try: + bytes_read += self._sock.recv_into(view[bytes_read:], 0, socket.MSG_DONTWAIT) + except: + pass return bytes_read class libreVNA: diff --git a/Software/PC_Application/LibreVNA-GUI/Calibration/calibration.cpp b/Software/PC_Application/LibreVNA-GUI/Calibration/calibration.cpp index 523c3b8..2388dba 100644 --- a/Software/PC_Application/LibreVNA-GUI/Calibration/calibration.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Calibration/calibration.cpp @@ -293,6 +293,7 @@ Calibration::Calibration() } return SCPI::getResultName(SCPI::Result::True); })); + add(&kit); } QString Calibration::TypeToString(Calibration::Type type) diff --git a/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.cpp b/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.cpp index ac8dc51..f10ab3d 100644 --- a/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.cpp @@ -16,12 +16,33 @@ using json = nlohmann::json; using namespace std; Calkit::Calkit() + : SCPINode("KIT") { - // set default values for(auto e : descr) { e.var.setValue(e.def); } + + add(new SCPICommand("SAVE", [=](QStringList params) -> QString { + if(params.size() != 1 ) { + // no filename given or no calibration active + return SCPI::getResultName(SCPI::Result::Error); + } + toFile(params[0]); + return SCPI::getResultName(SCPI::Result::Empty); + }, nullptr)); + add(new SCPICommand("LOAD", nullptr, [=](QStringList params) -> QString { + if(params.size() != 1) { + // no filename given or no calibration active + return SCPI::getResultName(SCPI::Result::False); + } + try { + *this = fromFile(params[0]); + return SCPI::getResultName(SCPI::Result::True); + } catch (runtime_error &e) { + return SCPI::getResultName(SCPI::Result::False); + } + })); } void Calkit::toFile(QString filename) diff --git a/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.h b/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.h index 6f52588..0bea69b 100644 --- a/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.h +++ b/Software/PC_Application/LibreVNA-GUI/Calibration/calkit.h @@ -5,6 +5,7 @@ #include "Util/qpointervariant.h" #include "calstandard.h" #include "savable.h" +#include "scpi.h" #include "LibreCAL/caldevice.h" @@ -12,7 +13,7 @@ #include #include -class Calkit : public Savable +class Calkit : public Savable, public SCPINode { friend class CalkitDialog; friend class LibreCALDialog; diff --git a/Software/PC_Application/LibreVNA-GUI/Device/device.cpp b/Software/PC_Application/LibreVNA-GUI/Device/device.cpp index e707c79..b7790a9 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/device.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/device.cpp @@ -510,6 +510,7 @@ void Device::ReceivedData() dataBuffer->removeBytes(handled_len); switch(packet.type) { case Protocol::PacketType::VNADatapoint: +// qDebug() << "Got point" << packet.VNAdatapoint->pointNum; emit DatapointReceived(this, packet.VNAdatapoint); break; case Protocol::PacketType::ManualStatusV1: diff --git a/Software/PC_Application/LibreVNA-GUI/Device/virtualdevice.cpp b/Software/PC_Application/LibreVNA-GUI/Device/virtualdevice.cpp index 5f57fb5..03ac944 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/virtualdevice.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/virtualdevice.cpp @@ -1,4 +1,4 @@ -#include "virtualdevice.h" +#include "virtualdevice.h" #include "preferences.h" #include "CustomWidgets/informationbox.h" diff --git a/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp b/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp index bbca69e..32b468b 100644 --- a/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp +++ b/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp @@ -64,6 +64,9 @@ VNA::VNA(AppWindow *window, QString name) calMeasuring = false; calWaitFirst = false; calDialog.reset(); + // A modal QProgressDialog calls processEvents() in setValue(). Needs to use a queued connection to update the progress + // value from within the NewDatapoint slot to prevent possible re-entrancy. + connect(this, &VNA::calibrationMeasurementPercentage, &calDialog, &QProgressDialog::setValue, Qt::QueuedConnection); changingSettings = false; settings.sweepType = SweepType::Frequency; settings.zerospan = false; @@ -837,11 +840,12 @@ void VNA::NewDatapoint(VirtualDevice::VNAMeasurement m) if(m_avg.pointNum == settings.npoints - 1) { calMeasuring = false; cal.measurementsComplete(); + calDialog.reset(); } } } int percentage = (((average.currentSweep() - 1) * 100) + (m_avg.pointNum + 1) * 100 / settings.npoints) / averages; - calDialog.setValue(percentage); + emit calibrationMeasurementPercentage(percentage); } cal.correctMeasurement(m_avg); @@ -879,6 +883,7 @@ void VNA::NewDatapoint(VirtualDevice::VNAMeasurement m) UpdateAverageCount(); markerModel->updateMarkers(); } + static unsigned int lastPoint = 0; if(m_avg.pointNum > 0 && m_avg.pointNum != lastPoint + 1) { qWarning() << "Got point" << m_avg.pointNum << "but last received point was" << lastPoint << "("<<(m_avg.pointNum-lastPoint-1)<<"missed points)"; diff --git a/Software/PC_Application/LibreVNA-GUI/VNA/vna.h b/Software/PC_Application/LibreVNA-GUI/VNA/vna.h index 6fbba29..12b2b5f 100644 --- a/Software/PC_Application/LibreVNA-GUI/VNA/vna.h +++ b/Software/PC_Application/LibreVNA-GUI/VNA/vna.h @@ -191,6 +191,8 @@ signals: void sweepStopped(); void sweepStarted(); + + void calibrationMeasurementPercentage(int percent); }; #endif // VNA_H diff --git a/Software/PC_Application/LibreVNA-GUI/appwindow.cpp b/Software/PC_Application/LibreVNA-GUI/appwindow.cpp index d236ab4..e25c353 100644 --- a/Software/PC_Application/LibreVNA-GUI/appwindow.cpp +++ b/Software/PC_Application/LibreVNA-GUI/appwindow.cpp @@ -546,7 +546,7 @@ void AppWindow::SetupSCPI() if(active) { switch(active->getType()) { case Mode::Type::VNA: return "VNA"; - case Mode::Type::SG: return "SG"; + case Mode::Type::SG: return "GEN"; case Mode::Type::SA: return "SA"; case Mode::Type::Last: return SCPI::getResultName(SCPI::Result::Error); }