kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
Implemented bandwidth selection
rodzic
19493012b2
commit
b4ba24d42d
|
@ -28,6 +28,10 @@ from NanoVNASaver.Hardware.Serial import Interface, drain_serial
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _max_retries(bandwidth: int, datapoints: int) -> int:
|
||||
return 20 * (datapoints / 101) + round(
|
||||
(1000 / bandwidth) ** 1.2 * (datapoints / 101))
|
||||
|
||||
class VNA:
|
||||
name = "VNA"
|
||||
valid_datapoints = (101, )
|
||||
|
@ -38,9 +42,14 @@ class VNA:
|
|||
self.features = set()
|
||||
self.validateInput = True
|
||||
self.datapoints = self.valid_datapoints[0]
|
||||
self.bandwidth = 1000
|
||||
if self.connected():
|
||||
self.version = self.readVersion()
|
||||
self.read_features()
|
||||
# cannot read current bandwidth, so set to highest
|
||||
# to get initial sweep fast
|
||||
if "Bandwidth" in self.features:
|
||||
self.set_bandwidth(self.get_bandwidths()[-1])
|
||||
|
||||
def exec_command(self, command: str, wait: float = 0.05) -> Iterator[str]:
|
||||
logger.debug("exec_command(%s)", command)
|
||||
|
@ -49,12 +58,14 @@ class VNA:
|
|||
self.serial.write(f"{command}\r".encode('ascii'))
|
||||
sleep(wait)
|
||||
retries = 0
|
||||
max_retries = _max_retries(self.bandwidth, self.datapoints)
|
||||
logger.debug("Max retries: %s", max_retries)
|
||||
while True:
|
||||
line = self.serial.readline()
|
||||
line = line.decode("ascii").strip()
|
||||
if not line:
|
||||
retries += 1
|
||||
if retries > 100:
|
||||
if retries > max_retries:
|
||||
raise IOError("too many retries")
|
||||
sleep(wait)
|
||||
continue
|
||||
|
@ -66,13 +77,30 @@ class VNA:
|
|||
yield line
|
||||
|
||||
def read_features(self):
|
||||
result = "\n".join(list(self.exec_command("help")))
|
||||
result = " ".join(self.exec_command("help")).split()
|
||||
logger.debug("result:\n%s", result)
|
||||
if "capture" in result:
|
||||
self.features.add("Screenshots")
|
||||
if "bandwidth" in result:
|
||||
self.features.add("Bandwidth")
|
||||
if len(self.valid_datapoints) > 1:
|
||||
self.features.add("Customizable data points")
|
||||
|
||||
def get_bandwidths(self) -> List[int]:
|
||||
logger.debug("get bandwidths")
|
||||
try:
|
||||
result = " ".join(list(self.exec_command("bandwidth")))
|
||||
result = result.split(" {")[1].strip("}")
|
||||
return sorted([int(i) for i in result.split("|")])
|
||||
except IndexError:
|
||||
return []
|
||||
|
||||
def set_bandwidth(self, bw: int):
|
||||
result = " ".join(self.exec_command(f"bandwidth {bw}"))
|
||||
if result:
|
||||
raise IOError(f"set_bandwith({bw}: {result}")
|
||||
self.bandwidth = bw
|
||||
|
||||
def readFrequencies(self) -> List[int]:
|
||||
return [int(f) for f in self.readValues("frequencies")]
|
||||
|
||||
|
|
|
@ -88,8 +88,13 @@ class DeviceSettingsWindow(QtWidgets.QWidget):
|
|||
self.datapoints.currentIndexChanged.connect(
|
||||
self.app.sweep_control.update_step_size)
|
||||
|
||||
self.bandwidth = QtWidgets.QComboBox()
|
||||
self.bandwidth.addItem(str(self.app.vna.bandwidth))
|
||||
self.bandwidth.currentIndexChanged.connect(self.updateBandwidth)
|
||||
|
||||
form_layout = QtWidgets.QFormLayout()
|
||||
form_layout.addRow(QtWidgets.QLabel("Datapoints"), self.datapoints)
|
||||
form_layout.addRow(QtWidgets.QLabel("Bandwidth"), self.bandwidth)
|
||||
right_layout.addWidget(settings_box)
|
||||
settings_layout.addRow(form_layout)
|
||||
|
||||
|
@ -97,6 +102,10 @@ class DeviceSettingsWindow(QtWidgets.QWidget):
|
|||
self.datapoints.setCurrentIndex(
|
||||
self.datapoints.findText(str(dpoints)))
|
||||
|
||||
def _set_bandwidth_index(self, bw: int):
|
||||
self.bandwidth.setCurrentIndex(
|
||||
self.bandwidth.findText(str(bw)))
|
||||
|
||||
def show(self):
|
||||
super().show()
|
||||
self.updateFields()
|
||||
|
@ -131,6 +140,19 @@ class DeviceSettingsWindow(QtWidgets.QWidget):
|
|||
for d in sorted(self.app.vna.valid_datapoints):
|
||||
self.datapoints.addItem(str(d))
|
||||
self._set_datapoint_index(cur_dps)
|
||||
self.datapoints.setDisabled(False)
|
||||
else:
|
||||
self.datapoints.setDisabled(True)
|
||||
|
||||
if "Bandwidth" in features:
|
||||
self.bandwidth.clear()
|
||||
cur_bw = self.app.vna.bandwidth
|
||||
for d in sorted(self.app.vna.get_bandwidths()):
|
||||
self.bandwidth.addItem(str(d))
|
||||
self._set_bandwidth_index(cur_bw)
|
||||
self.bandwidth.setDisabled(False)
|
||||
else:
|
||||
self.bandwidth.setDisabled(True)
|
||||
|
||||
def updateValidation(self, validate_data: bool):
|
||||
self.app.vna.validateInput = validate_data
|
||||
|
@ -150,3 +172,9 @@ class DeviceSettingsWindow(QtWidgets.QWidget):
|
|||
return
|
||||
logger.debug("DP: %s", self.datapoints.itemText(i))
|
||||
self.app.vna.datapoints = int(self.datapoints.itemText(i))
|
||||
|
||||
def updateBandwidth(self, i):
|
||||
if i < 0 or self.app.worker.running:
|
||||
return
|
||||
logger.debug("Bandwidth: %s", self.bandwidth.itemText(i))
|
||||
self.app.vna.set_bandwidth(int(self.bandwidth.itemText(i)))
|
||||
|
|
Ładowanie…
Reference in New Issue