Skip to content

Commit

Permalink
Merge pull request #27 from mihtjel/Development
Browse files Browse the repository at this point in the history
0.1.0
  • Loading branch information
mihtjel authored Oct 2, 2019
2 parents b862c14 + 5f4dd68 commit 51bdcc2
Show file tree
Hide file tree
Showing 9 changed files with 555 additions and 225 deletions.
292 changes: 242 additions & 50 deletions NanoVNASaver/Calibration.py

Large diffs are not rendered by default.

302 changes: 181 additions & 121 deletions NanoVNASaver/Chart.py

Large diffs are not rendered by default.

51 changes: 36 additions & 15 deletions NanoVNASaver/Marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def __init__(self, name, initialColor, frequency=""):

line = QtWidgets.QFrame()
line.setFrameShape(QtWidgets.QFrame.VLine)
#line.setFrameShadow(QtWidgets.QFrame.Sunken)

left_form = QtWidgets.QFormLayout()
right_form = QtWidgets.QFormLayout()
Expand Down Expand Up @@ -169,28 +168,39 @@ def updateLabels(self, s11data: List[Datapoint], s21data: List[Datapoint]):
from NanoVNASaver.NanoVNASaver import NanoVNASaver
if self.location != -1:
im50, re50, vswr = NanoVNASaver.vswr(s11data[self.location])
rp = (re50 ** 2 + im50 ** 2) / re50
xp = (re50 ** 2 + im50 ** 2) / im50
re50 = round(re50, 4 - max(0, math.floor(math.log10(abs(re50)))))
rp = round(rp, 4 - max(0, math.floor(math.log10(abs(rp)))))
im50 = round(im50, 4 - max(0, math.floor(math.log10(abs(im50)))))
xp = round(xp, 4 - max(0, math.floor(math.log10(abs(xp)))))
if re50 > 0:
rp = (re50 ** 2 + im50 ** 2) / re50
rp = round(rp, 4 - max(0, math.floor(math.log10(abs(rp)))))
rpstr = str(rp) + " \N{OHM SIGN}"

re50 = round(re50, 4 - max(0, math.floor(math.log10(abs(re50)))))
else:
rpstr = "- \N{OHM SIGN}"
re50 = 0

if im50 != 0:
xp = (re50 ** 2 + im50 ** 2) / im50
xp = round(xp, 4 - max(0, math.floor(math.log10(abs(xp)))))
if xp < 0:
xpstr = NanoVNASaver.capacitanceEquivalent(xp, s11data[self.location].freq)
else:
xpstr = NanoVNASaver.inductanceEquivalent(xp, s11data[self.location].freq)
else:
xpstr = "-"

if im50 != 0:
im50 = round(im50, 4 - max(0, math.floor(math.log10(abs(im50)))))

if im50 < 0:
im50str = " -j" + str(-1 * im50)
else:
im50str = " +j" + str(im50)
im50str += " \N{OHM SIGN}"

if xp < 0:
xpstr = NanoVNASaver.capacitanceEquivalent(xp, s11data[self.location].freq)
else:
xpstr = NanoVNASaver.inductanceEquivalent(xp, s11data[self.location].freq)

self.frequency_label.setText(NanoVNASaver.formatFrequency(s11data[self.location].freq))
self.impedance_label.setText(str(re50) + im50str)
self.parallel_r_label.setText(str(rp) + " \N{OHM SIGN}")
self.parallel_r_label.setText(rpstr)
self.parallel_x_label.setText(xpstr)
#self.returnloss_label.setText(str(round(20 * math.log10((vswr - 1) / (vswr + 1)), 3)) + " dB")
self.returnloss_label.setText(str(round(NanoVNASaver.gain(s11data[self.location]), 3)) + " dB")
capacitance = NanoVNASaver.capacitanceEquivalent(im50, s11data[self.location].freq)
inductance = NanoVNASaver.inductanceEquivalent(im50, s11data[self.location].freq)
Expand All @@ -200,7 +210,18 @@ def updateLabels(self, s11data: List[Datapoint], s21data: List[Datapoint]):
if vswr < 0:
vswr = "-"
self.vswr_label.setText(str(vswr))
self.quality_factor_label.setText(str(round(NanoVNASaver.qualifyFactor(s11data[self.location]), 1)))
q = NanoVNASaver.qualifyFactor(s11data[self.location])
if q > 10000 or q < 0:
q_str = "\N{INFINITY}"
elif q > 1000:
q_str = str(round(q, 0))
elif q > 100:
q_str = str(round(q, 1))
elif q > 10:
q_str = str(round(q, 2))
else:
q_str = str(round(q, 3))
self.quality_factor_label.setText(q_str)
self.s11_phase_label.setText(
str(round(PhaseChart.angle(s11data[self.location]), 2)) + "\N{DEGREE SIGN}")
if len(s21data) == len(s11data):
Expand Down
44 changes: 30 additions & 14 deletions NanoVNASaver/NanoVNASaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import math
import sys
import threading
from time import sleep, strftime, gmtime
from time import sleep, strftime, localtime
from typing import List, Tuple

import numpy as np
Expand Down Expand Up @@ -49,7 +49,12 @@ class NanoVNASaver(QtWidgets.QWidget):

def __init__(self):
super().__init__()
self.setWindowIcon(QtGui.QIcon("icon_48x48.png"))
if getattr(sys, 'frozen', False):
logger.debug("Running from pyinstaller bundle")
self.icon = QtGui.QIcon(sys._MEIPASS + "/icon_48x48.png")
else:
self.icon = QtGui.QIcon("icon_48x48.png")
self.setWindowIcon(self.icon)

self.settings = QtCore.QSettings(QtCore.QSettings.IniFormat,
QtCore.QSettings.UserScope,
Expand Down Expand Up @@ -386,6 +391,7 @@ def __init__(self):

self.fileWindow = QtWidgets.QWidget()
self.fileWindow.setWindowTitle("Files")
self.fileWindow.setWindowIcon(self.icon)
shortcut = QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self.fileWindow, self.fileWindow.hide)
file_window_layout = QtWidgets.QVBoxLayout()
self.fileWindow.setLayout(file_window_layout)
Expand Down Expand Up @@ -595,13 +601,17 @@ def startSerial(self):
logger.info(self.readFirmware())

frequencies = self.readValues("frequencies")
logger.info("Read starting frequency %s and end frequency %s", frequencies[0], frequencies[100])
if int(frequencies[0]) == int(frequencies[100]) and (self.sweepStartInput.text() == "" or self.sweepEndInput.text() == ""):
self.sweepStartInput.setText(frequencies[0])
self.sweepEndInput.setText(str(int(frequencies[100]) + 100000))
elif self.sweepStartInput.text() == "" or self.sweepEndInput.text() == "":
self.sweepStartInput.setText(frequencies[0])
self.sweepEndInput.setText(frequencies[100])
if frequencies:
logger.info("Read starting frequency %s and end frequency %s", frequencies[0], frequencies[100])
if int(frequencies[0]) == int(frequencies[100]) and (self.sweepStartInput.text() == "" or self.sweepEndInput.text() == ""):
self.sweepStartInput.setText(frequencies[0])
self.sweepEndInput.setText(str(int(frequencies[100]) + 100000))
elif self.sweepStartInput.text() == "" or self.sweepEndInput.text() == "":
self.sweepStartInput.setText(frequencies[0])
self.sweepEndInput.setText(frequencies[100])
else:
logger.warning("No frequencies read")
return

logger.debug("Starting initial sweep")
self.sweep()
Expand Down Expand Up @@ -696,6 +706,8 @@ def readValues(self, value):
values = result.split("\r\n")
except serial.SerialException as exc:
logger.exception("Exception while reading %s: %s", value, exc)
self.serialLock.release()
return

self.serialLock.release()
return values[1:102]
Expand All @@ -713,7 +725,7 @@ def saveData(self, data, data12, source=None):
if source is not None:
self.sweepSource = source
else:
self.sweepSource = strftime("%Y-%m-%d %H:%M:%S", gmtime())
self.sweepSource = strftime("%Y-%m-%d %H:%M:%S", localtime())

def dataUpdated(self):
if self.dataLock.acquire(blocking=True):
Expand Down Expand Up @@ -789,10 +801,10 @@ def vswr(data: Datapoint):
def qualifyFactor(data: Datapoint):
im50, re50, _ = NanoVNASaver.vswr(data)
if re50 != 0:
Q = im50 / re50
Q = abs(im50 / re50)
else:
Q = 0
return abs(Q)
Q = -1
return Q

@staticmethod
def capacitanceEquivalent(im50, freq) -> str:
Expand All @@ -812,7 +824,7 @@ def capacitanceEquivalent(im50, freq) -> str:
def inductanceEquivalent(im50, freq) -> str:
if freq == 0:
return "- nH"
inductance = im50 * 1000000000/ (freq * 2 * math.pi)
inductance = im50 * 1000000000 / (freq * 2 * math.pi)
if abs(inductance) > 10000:
return str(round(inductance / 1000, 2)) + " μH"
elif abs(inductance) > 1000:
Expand Down Expand Up @@ -1066,6 +1078,7 @@ def __init__(self, app: NanoVNASaver):

self.app = app
self.setWindowTitle("Display settings")
self.setWindowIcon(self.app.icon)

shortcut = QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self, self.hide)

Expand Down Expand Up @@ -1433,6 +1446,7 @@ def __init__(self, app: NanoVNASaver):
self.distance_axis = []

self.setWindowTitle("TDR")
self.setWindowIcon(self.app.icon)

shortcut = QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self, self.hide)

Expand Down Expand Up @@ -1544,6 +1558,7 @@ def __init__(self, app: NanoVNASaver):

self.app = app
self.setWindowTitle("Sweep settings")
self.setWindowIcon(self.app.icon)

shortcut = QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self, self.hide)

Expand Down Expand Up @@ -1584,6 +1599,7 @@ def __init__(self, app):

self.app: NanoVNASaver = app
self.setWindowTitle("Manage bands")
self.setWindowIcon(self.app.icon)

shortcut = QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self, self.hide)

Expand Down
6 changes: 6 additions & 0 deletions NanoVNASaver/SweepWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ def readData(self, data):
done = True
returndata = []
tmpdata = self.app.readValues(data)
if not tmpdata:
logger.warning("Read no values")
raise NanoVNAValueException("Failed reading data: Returned no values.")
logger.debug("Read %d values", len(tmpdata))
for d in tmpdata:
a, b = d.split(" ")
Expand Down Expand Up @@ -334,6 +337,9 @@ def readFreq(self):
done = True
returnfreq = []
tmpfreq = self.app.readValues("frequencies")
if not tmpfreq:
logger.warning("Read no frequencies")
raise NanoVNAValueException("Failed reading frequencies: Returned no values.")
for f in tmpfreq:
if not f.isdigit():
logger.warning("Got a non-digit frequency: %s", f)
Expand Down
3 changes: 2 additions & 1 deletion NanoVNASaver/Touchstone.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class Touchstone:
s11data: List[Datapoint] = []
s21data: List[Datapoint] = []

comments = []
filename = ""

def __init__(self, filename):
Expand All @@ -51,6 +51,7 @@ def load(self):
line = line.strip()
if line.startswith("!"):
logger.info(line)
self.comments.append(line)
continue
if line.startswith("#") and not parsed_header:
pattern = "^# (.?HZ) (S )?RI( R 50)?$"
Expand Down
4 changes: 4 additions & 0 deletions NanoVNASaver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from PyQt5 import QtWidgets, QtCore

from .NanoVNASaver import NanoVNASaver
from .about import debug


def main():
Expand All @@ -44,6 +45,9 @@ def main():
print("You must enter a file name when using -D")
return

if debug:
console_log_level = logging.DEBUG

logger = logging.getLogger("NanoVNASaver")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
Expand Down
4 changes: 2 additions & 2 deletions NanoVNASaver/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

version = '0.0.12'

version = '0.1.0'
debug = False
Loading

0 comments on commit 51bdcc2

Please sign in to comment.