Save/load calkit SCPI command + re-entrancy bugfix during calibration

This commit is contained in:
Jan Käberich 2022-11-14 00:08:45 +01:00
parent 9b7f457aa5
commit b03c8b3958
9 changed files with 45 additions and 6 deletions

View File

@ -1,10 +1,12 @@
import socket import socket
from asyncio import IncompleteReadError # only import the exception class from asyncio import IncompleteReadError # only import the exception class
import time
class SocketStreamReader: class SocketStreamReader:
def __init__(self, sock: socket.socket): def __init__(self, sock: socket.socket):
self._sock = sock self._sock = sock
self._recv_buffer = bytearray() self._recv_buffer = bytearray()
self.timeout = 1.0
def read(self, num_bytes: int = -1) -> bytes: def read(self, num_bytes: int = -1) -> bytes:
raise NotImplementedError raise NotImplementedError
@ -32,10 +34,13 @@ class SocketStreamReader:
bytes_read = self._recv_into(memoryview(buf)) bytes_read = self._recv_into(memoryview(buf))
assert bytes_read == len(buf) assert bytes_read == len(buf)
timeout = time.time() + self.timeout
while True: while True:
idx = buf.find(separator, start) idx = buf.find(separator, start)
if idx != -1: if idx != -1:
break break
elif time.time() > timeout:
raise Exception("Timed out waiting for response from GUI")
start = len(self._recv_buffer) start = len(self._recv_buffer)
bytes_read = self._recv_into(memoryview(chunk)) bytes_read = self._recv_into(memoryview(chunk))
@ -53,7 +58,10 @@ class SocketStreamReader:
self._recv_buffer = self._recv_buffer[bytes_read:] self._recv_buffer = self._recv_buffer[bytes_read:]
if bytes_read == len(view): if bytes_read == len(view):
return bytes_read 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 return bytes_read
class libreVNA: class libreVNA:

View File

@ -293,6 +293,7 @@ Calibration::Calibration()
} }
return SCPI::getResultName(SCPI::Result::True); return SCPI::getResultName(SCPI::Result::True);
})); }));
add(&kit);
} }
QString Calibration::TypeToString(Calibration::Type type) QString Calibration::TypeToString(Calibration::Type type)

View File

@ -16,12 +16,33 @@ using json = nlohmann::json;
using namespace std; using namespace std;
Calkit::Calkit() Calkit::Calkit()
: SCPINode("KIT")
{ {
// set default values // set default values
for(auto e : descr) { for(auto e : descr) {
e.var.setValue(e.def); 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) void Calkit::toFile(QString filename)

View File

@ -5,6 +5,7 @@
#include "Util/qpointervariant.h" #include "Util/qpointervariant.h"
#include "calstandard.h" #include "calstandard.h"
#include "savable.h" #include "savable.h"
#include "scpi.h"
#include "LibreCAL/caldevice.h" #include "LibreCAL/caldevice.h"
@ -12,7 +13,7 @@
#include <complex> #include <complex>
#include <QDir> #include <QDir>
class Calkit : public Savable class Calkit : public Savable, public SCPINode
{ {
friend class CalkitDialog; friend class CalkitDialog;
friend class LibreCALDialog; friend class LibreCALDialog;

View File

@ -510,6 +510,7 @@ void Device::ReceivedData()
dataBuffer->removeBytes(handled_len); dataBuffer->removeBytes(handled_len);
switch(packet.type) { switch(packet.type) {
case Protocol::PacketType::VNADatapoint: case Protocol::PacketType::VNADatapoint:
// qDebug() << "Got point" << packet.VNAdatapoint->pointNum;
emit DatapointReceived(this, packet.VNAdatapoint); emit DatapointReceived(this, packet.VNAdatapoint);
break; break;
case Protocol::PacketType::ManualStatusV1: case Protocol::PacketType::ManualStatusV1:

View File

@ -1,4 +1,4 @@
#include "virtualdevice.h" #include "virtualdevice.h"
#include "preferences.h" #include "preferences.h"
#include "CustomWidgets/informationbox.h" #include "CustomWidgets/informationbox.h"

View File

@ -64,6 +64,9 @@ VNA::VNA(AppWindow *window, QString name)
calMeasuring = false; calMeasuring = false;
calWaitFirst = false; calWaitFirst = false;
calDialog.reset(); 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; changingSettings = false;
settings.sweepType = SweepType::Frequency; settings.sweepType = SweepType::Frequency;
settings.zerospan = false; settings.zerospan = false;
@ -837,11 +840,12 @@ void VNA::NewDatapoint(VirtualDevice::VNAMeasurement m)
if(m_avg.pointNum == settings.npoints - 1) { if(m_avg.pointNum == settings.npoints - 1) {
calMeasuring = false; calMeasuring = false;
cal.measurementsComplete(); cal.measurementsComplete();
calDialog.reset();
} }
} }
} }
int percentage = (((average.currentSweep() - 1) * 100) + (m_avg.pointNum + 1) * 100 / settings.npoints) / averages; int percentage = (((average.currentSweep() - 1) * 100) + (m_avg.pointNum + 1) * 100 / settings.npoints) / averages;
calDialog.setValue(percentage); emit calibrationMeasurementPercentage(percentage);
} }
cal.correctMeasurement(m_avg); cal.correctMeasurement(m_avg);
@ -879,6 +883,7 @@ void VNA::NewDatapoint(VirtualDevice::VNAMeasurement m)
UpdateAverageCount(); UpdateAverageCount();
markerModel->updateMarkers(); markerModel->updateMarkers();
} }
static unsigned int lastPoint = 0; static unsigned int lastPoint = 0;
if(m_avg.pointNum > 0 && m_avg.pointNum != lastPoint + 1) { 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)"; qWarning() << "Got point" << m_avg.pointNum << "but last received point was" << lastPoint << "("<<(m_avg.pointNum-lastPoint-1)<<"missed points)";

View File

@ -191,6 +191,8 @@ signals:
void sweepStopped(); void sweepStopped();
void sweepStarted(); void sweepStarted();
void calibrationMeasurementPercentage(int percent);
}; };
#endif // VNA_H #endif // VNA_H

View File

@ -546,7 +546,7 @@ void AppWindow::SetupSCPI()
if(active) { if(active) {
switch(active->getType()) { switch(active->getType()) {
case Mode::Type::VNA: return "VNA"; 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::SA: return "SA";
case Mode::Type::Last: return SCPI::getResultName(SCPI::Result::Error); case Mode::Type::Last: return SCPI::getResultName(SCPI::Result::Error);
} }