diff --git a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.cpp b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.cpp index 38717f5..a7e88a5 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.cpp @@ -44,6 +44,9 @@ LibreVNATCPDriver::LibreVNATCPDriver() ssdpSockets.push_back(socket); } + connect(&ssdpTimer, &QTimer::timeout,this, &LibreVNATCPDriver::SSDRequest); + ssdpTimer.start(1000); + specificSettings.push_back(Savable::SettingDescription(&captureRawReceiverValues, "LibreVNATCPDriver.captureRawReceiverValues", false)); specificSettings.push_back(Savable::SettingDescription(&harmonicMixing, "LibreVNATCPDriver.harmonicMixing", false)); specificSettings.push_back(Savable::SettingDescription(&SASignalID, "LibreVNATCPDriver.signalID", true)); @@ -60,26 +63,6 @@ QString LibreVNATCPDriver::getDriverName() std::set LibreVNATCPDriver::GetAvailableDevices() { - QByteArray data; - data.append("M-SEARCH * HTTP/1.1\r\n" - "HOST: 239.255.255.250:1900\r\n" - "MAN: \"ssdp:discover\"\r\n" - "MX: 1\r\n" - "ST: "); - data.append(service_name.toUtf8()); - data.append("\r\n" - "\r\n"); - - // just delete everything instead of keeping old entries (they will answer again if they are still available) - detectedDevices.clear(); -// pruneDetectedDevices(); - for(auto s : ssdpSockets) { - s->writeDatagram(data.data(), SSDPaddress, SSDPport); - } - - // need delay here while still processing events - SynSleep::sleep(100); - std::set serials; for(auto d : detectedDevices) { serials.insert(d.serial); @@ -171,6 +154,25 @@ void LibreVNATCPDriver::registerTypes() qDebug() << "Registering meta type: " << qRegisterMetaType(); } +void LibreVNATCPDriver::SSDRequest() +{ + QByteArray data; + data.append("M-SEARCH * HTTP/1.1\r\n" + "HOST: 239.255.255.250:1900\r\n" + "MAN: \"ssdp:discover\"\r\n" + "MX: 1\r\n" + "ST: "); + data.append(service_name.toUtf8()); + data.append("\r\n" + "\r\n"); + + pruneDetectedDevices(); + + for(auto s : ssdpSockets) { + s->writeDatagram(data.data(), SSDPaddress, SSDPport); + } +} + void LibreVNATCPDriver::SSDPreceived(QUdpSocket *sock) { while(sock->hasPendingDatagrams()) { @@ -183,7 +185,7 @@ void LibreVNATCPDriver::SSDPreceived(QUdpSocket *sock) QString ssdp_string = QString(buf); auto lines = ssdp_string.split("\r\n"); - QString location, st, serial, max_age; + QString location, st, serial, max_age = "2"; if(lines[0] != "HTTP/1.1 200 OK") { continue; diff --git a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.h b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.h index fbc822b..599673d 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.h +++ b/Software/PC_Application/LibreVNA-GUI/Device/LibreVNA/librevnatcpdriver.h @@ -53,6 +53,7 @@ public: } private slots: + void SSDRequest(); void SSDPreceived(QUdpSocket *sock); void ReceivedData(); void ReceivedLog(); @@ -101,6 +102,7 @@ private: QQueue transmissionQueue; bool startNextTransmission(); QTimer transmissionTimer; + QTimer ssdpTimer; bool transmissionActive; std::thread *m_receiveThread; diff --git a/Software/PC_Application/LibreVNA-GUI/Util/util.h b/Software/PC_Application/LibreVNA-GUI/Util/util.h index 47c6bb7..60b280f 100644 --- a/Software/PC_Application/LibreVNA-GUI/Util/util.h +++ b/Software/PC_Application/LibreVNA-GUI/Util/util.h @@ -11,42 +11,6 @@ #include #include -class SynSleep: public QObject { - Q_OBJECT -public: - SynSleep(){ - needRunning=1; - } - -public slots: - void sleep(){ - if (needRunning==1) - loop.exec(); - } - - void reset(){ - needRunning=1; - } - - virtual void finish(){ - needRunning=0; - loop.exit(); - } - - static void sleep(int ms) { - QTimer tim; - tim.setSingleShot(true); - auto ss = SynSleep(); - connect(&tim, &QTimer::timeout, &ss, &SynSleep::finish); - tim.start(ms); - ss.sleep(); - } - -private: - QEventLoop loop; - int needRunning; -}; - namespace Util { template T Scale(T value, T from_low, T from_high, T to_low, T to_high, bool log_from = false, bool log_to = false) { T normalized; diff --git a/Software/PC_Application/LibreVNA-GUI/appwindow.cpp b/Software/PC_Application/LibreVNA-GUI/appwindow.cpp index 91aeafd..5527d07 100644 --- a/Software/PC_Application/LibreVNA-GUI/appwindow.cpp +++ b/Software/PC_Application/LibreVNA-GUI/appwindow.cpp @@ -1019,7 +1019,7 @@ void AppWindow::SetupSCPI() void AppWindow::StartTCPServer(int port) { server = new TCPServer(port); - connect(server, &TCPServer::received, &scpi, &SCPI::input, Qt::QueuedConnection); + connect(server, &TCPServer::received, &scpi, &SCPI::input); connect(&scpi, &SCPI::output, server, &TCPServer::send); } diff --git a/Software/PC_Application/LibreVNA-GUI/scpi.cpp b/Software/PC_Application/LibreVNA-GUI/scpi.cpp index 397e41a..5fae1b4 100644 --- a/Software/PC_Application/LibreVNA-GUI/scpi.cpp +++ b/Software/PC_Application/LibreVNA-GUI/scpi.cpp @@ -3,7 +3,9 @@ #include SCPI::SCPI() : - SCPINode("") + SCPINode(""), + semQueue(QSemaphore(1)), + semProcessing(QSemaphore(1)) { WAIexecuting = false; OPCsetBitScheduled = false; @@ -178,21 +180,30 @@ QString SCPI::getResultName(SCPI::Result r) void SCPI::input(QString line) { + semQueue.acquire(); cmdQueue.append(line); - if(!processing) { + semQueue.release(); + if(semProcessing.available()) { process(); } } void SCPI::process() { - processing = true; - while(!WAIexecuting && !cmdQueue.isEmpty()) { + semProcessing.acquire(); + semQueue.acquire(); + auto queueBuf = cmdQueue; + semQueue.release(); + while(!WAIexecuting && !queueBuf.isEmpty()) { + semQueue.acquire(); auto cmd = cmdQueue.front(); cmdQueue.pop_front(); + queueBuf = cmdQueue; + semQueue.release(); auto cmds = cmd.split(";"); SCPINode *lastNode = this; for(auto cmd : cmds) { + qDebug() << "Handling cmd " << cmd; if(cmd.size() > 0) { if(cmd[0] == ':' || cmd[0] == '*') { // reset to root node @@ -216,9 +227,10 @@ void SCPI::process() emit output(response); } } + qDebug() << "handling done"; } } - processing = false; + semProcessing.release(); } void SCPI::someOperationCompleted() diff --git a/Software/PC_Application/LibreVNA-GUI/scpi.h b/Software/PC_Application/LibreVNA-GUI/scpi.h index e206689..42e122a 100644 --- a/Software/PC_Application/LibreVNA-GUI/scpi.h +++ b/Software/PC_Application/LibreVNA-GUI/scpi.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -122,6 +123,8 @@ private: QList cmdQueue; bool processing; + QSemaphore semQueue; + QSemaphore semProcessing; }; #endif // SCPI_H