diff --git a/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.cpp b/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.cpp index 9bf154a..80b9db3 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.cpp @@ -1,17 +1,28 @@ #include "ssa3000xdriver.h" #include "CustomWidgets/informationbox.h" +#include "Util/util.h" #include +#include SSA3000XDriver::SSA3000XDriver() { + diffGen = new TraceDifferenceGenerator([=](const SpectrumPoint &p){ + SAMeasurement m; + m.pointNum = p.index; + m.frequency = p.frequency; + m.measurements["PORT1"] = pow(10.0, p.dBm / 20.0); + emit SAmeasurementReceived(m); + }); searchAddresses.push_back(QHostAddress("192.168.22.2")); + connect(&traceTimer, &QTimer::timeout, this, &SSA3000XDriver::extractTracePoints); + traceTimer.setSingleShot(true); } SSA3000XDriver::~SSA3000XDriver() { - + delete diffGen; } std::set SSA3000XDriver::GetAvailableDevices() @@ -81,6 +92,8 @@ bool SSA3000XDriver::connectTo(QString serial) return false; } + connect(&dataSocket, qOverload(&QTcpSocket::error), this, &SSA3000XDriver::ConnectionLost, Qt::QueuedConnection); + // grab model information dataSocket.write("*IDN?\r\n"); dataSocket.waitForReadyRead(100); @@ -188,6 +201,9 @@ bool SSA3000XDriver::setSA(const DeviceDriver::SASettings &s, std::function cb) if(!connected) { return false; } + traceTimer.stop(); write("*RST\r\n"); if(cb) { cb(true); @@ -293,4 +315,42 @@ bool SSA3000XDriver::setExtRef(QString option_in, QString option_out) void SSA3000XDriver::write(QString s) { dataSocket.write(QString(s + "\r\n").toLocal8Bit()); + dataSocket.readAll(); } + +void SSA3000XDriver::extractTracePoints() +{ + if(!connected) { + return; + } + write(":TRAC? 1"); + auto start = QDateTime::currentDateTimeUtc(); + while(!dataSocket.canReadLine()) { + dataSocket.waitForReadyRead(100); + if(start.msecsTo(QDateTime::currentDateTimeUtc()) >= 100) { + // timed out + qWarning() << "Timed out waiting for trace data response"; + return; + } + } + QString line = QString(dataSocket.readLine()); + QStringList values = line.split(","); + // line contains a trailing comma, remove last item + values.pop_back(); + std::vector trace; + for(unsigned int i=0;inewTrace(trace); + traceTimer.start(100); +} + diff --git a/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.h b/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.h index 4c07214..895a247 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.h +++ b/Software/PC_Application/LibreVNA-GUI/Device/SSA3000X/ssa3000xdriver.h @@ -3,8 +3,11 @@ #include "../devicedriver.h" +#include "../tracedifferencegenerator.h" + #include #include +#include class SSA3000XDriver : public DeviceDriver { @@ -139,6 +142,8 @@ public: */ virtual bool setExtRef(QString option_in, QString option_out) override; +private slots: + void extractTracePoints(); private: void write(QString s); QString serial; @@ -147,6 +152,21 @@ private: bool connected; Info info; + double startFreq, stopFreq; + + class SpectrumPoint { + public: + unsigned int index; + double frequency; + double dBm; + bool operator==(const SpectrumPoint& rhs) { + return index == rhs.index && frequency == rhs.frequency && dBm == rhs.dBm; + } + }; + + QTimer traceTimer; + TraceDifferenceGenerator *diffGen; + std::vector searchAddresses; std::map detectedDevices; }; diff --git a/Software/PC_Application/LibreVNA-GUI/Device/devicedriver.h b/Software/PC_Application/LibreVNA-GUI/Device/devicedriver.h index b04c99c..f2b3d38 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/devicedriver.h +++ b/Software/PC_Application/LibreVNA-GUI/Device/devicedriver.h @@ -352,7 +352,7 @@ public: }; // S parameter measurements // Key: S parameter name, e.g. "PORT1" - // Value: measurement in mW (linear, not in dB). A value of 1.0 means 0dBm + // Value: measurement in linear voltage (linear, not in dB). A value of 1.0 means 0dBm std::map measurements; }; diff --git a/Software/PC_Application/LibreVNA-GUI/Device/tracedifferencegenerator.h b/Software/PC_Application/LibreVNA-GUI/Device/tracedifferencegenerator.h new file mode 100644 index 0000000..978f57e --- /dev/null +++ b/Software/PC_Application/LibreVNA-GUI/Device/tracedifferencegenerator.h @@ -0,0 +1,73 @@ +#ifndef TRACEDIFFERENCEGENERATOR_H +#define TRACEDIFFERENCEGENERATOR_H + +#include +#include + +#include + +template +class TraceDifferenceGenerator { +public: + TraceDifferenceGenerator(std::function changeCallback) : + last{}, + callback(changeCallback), + nextCallbackIndex(0) + {} + + void reset() { + last.clear(); + nextCallbackIndex = 0; + } + + void newTrace(const std::vector &trace) { + if(trace.size() > last.size()) { + // definitely got more points than last time. Find first point that is hasn't been transmitted and generate callbacks for it and all subsequent points + unsigned int i=nextCallbackIndex; + while(i < trace.size()) { + callback(trace[i]); + i++; + } + nextCallbackIndex = 0; + } else if(trace.size() < last.size()) { + // got less points than last time. This must be a completely new trace, generate callbacks for all points + for(auto &i : trace) { + callback(i); + } + nextCallbackIndex = 0; + } else { + // still the same amount of points. + unsigned int i = nextCallbackIndex; + unsigned int changedPoints = 0; + do { + if(i > 0) { + i--; + } else { + i = trace.size() - 1; + } + bool unchanged = last[i] == trace[i]; + if(!unchanged) { + changedPoints = (i + trace.size() - nextCallbackIndex + 1); + if(changedPoints > trace.size()) { + changedPoints -= trace.size(); + } + break; + } + } while (i != nextCallbackIndex); + i = nextCallbackIndex; + while(changedPoints--) { + callback(trace[i]); + i = (i + 1) % trace.size(); + } + nextCallbackIndex = i; + } + last = trace; + } + +private: + unsigned int nextCallbackIndex; + std::vector last; + std::function callback; +}; + +#endif // TRACEDIFFERENCEGENERATOR_H diff --git a/Software/PC_Application/LibreVNA-GUI/LibreVNA-GUI.pro b/Software/PC_Application/LibreVNA-GUI/LibreVNA-GUI.pro index b2d6b7f..04e9b17 100644 --- a/Software/PC_Application/LibreVNA-GUI/LibreVNA-GUI.pro +++ b/Software/PC_Application/LibreVNA-GUI/LibreVNA-GUI.pro @@ -37,6 +37,7 @@ HEADERS += \ Device/devicelog.h \ Device/deviceusblog.h \ Device/deviceusblogview.h \ + Device/tracedifferencegenerator.h \ Device/virtualdevice.h \ Generator/generator.h \ Generator/signalgenwidget.h \