2020-05-18 18:31:27 +00:00
|
|
|
# NanoVNASaver
|
2020-06-25 17:48:03 +00:00
|
|
|
#
|
2020-05-18 18:31:27 +00:00
|
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
2020-06-25 17:52:30 +00:00
|
|
|
# Copyright (C) 2019, 2020 Rune B. Broberg
|
2021-06-30 05:21:14 +00:00
|
|
|
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
|
2020-05-16 15:10:07 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import logging
|
2020-07-15 12:47:06 +00:00
|
|
|
from collections import OrderedDict
|
2020-05-16 15:10:07 +00:00
|
|
|
from time import sleep
|
2020-07-07 14:48:43 +00:00
|
|
|
from typing import List, Iterator
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-04 10:08:54 +00:00
|
|
|
from PyQt5 import QtGui
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-25 18:10:24 +00:00
|
|
|
from NanoVNASaver.Version import Version
|
2020-07-04 10:08:54 +00:00
|
|
|
from NanoVNASaver.Hardware.Serial import Interface, drain_serial
|
2020-06-25 17:48:03 +00:00
|
|
|
|
2020-05-16 15:10:07 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-07-15 12:47:06 +00:00
|
|
|
DISLORD_BW = OrderedDict((
|
2022-01-17 00:15:25 +00:00
|
|
|
(10, 363),
|
|
|
|
(33, 117),
|
|
|
|
(50, 78),
|
|
|
|
(100, 39),
|
|
|
|
(200, 19),
|
|
|
|
(250, 15),
|
|
|
|
(333, 11),
|
|
|
|
(500, 7),
|
|
|
|
(1000, 3),
|
|
|
|
(2000, 1),
|
|
|
|
(4000, 0),
|
2020-07-15 12:47:06 +00:00
|
|
|
))
|
2020-07-24 19:11:34 +00:00
|
|
|
WAIT = 0.05
|
2020-07-15 12:47:06 +00:00
|
|
|
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-14 09:30:28 +00:00
|
|
|
def _max_retries(bandwidth: int, datapoints: int) -> int:
|
2020-08-03 15:31:00 +00:00
|
|
|
return round(20 + 20 * (datapoints / 101) +
|
2020-07-24 19:11:34 +00:00
|
|
|
(1000 / bandwidth) ** 1.30 * (datapoints / 101))
|
|
|
|
|
2020-07-14 09:30:28 +00:00
|
|
|
|
2020-05-16 15:10:07 +00:00
|
|
|
class VNA:
|
|
|
|
name = "VNA"
|
2020-07-30 07:28:57 +00:00
|
|
|
valid_datapoints = (101, 51, 11)
|
2020-07-24 19:11:34 +00:00
|
|
|
wait = 0.05
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-04 10:08:54 +00:00
|
|
|
def __init__(self, iface: Interface):
|
|
|
|
self.serial = iface
|
2020-07-04 12:26:20 +00:00
|
|
|
self.version = Version("0.0.0")
|
2020-06-10 10:28:51 +00:00
|
|
|
self.features = set()
|
2020-07-28 15:34:16 +00:00
|
|
|
self.validateInput = False
|
2020-07-07 10:33:32 +00:00
|
|
|
self.datapoints = self.valid_datapoints[0]
|
2020-07-14 09:30:28 +00:00
|
|
|
self.bandwidth = 1000
|
2020-07-15 12:47:06 +00:00
|
|
|
self.bw_method = "ttrftech"
|
2021-04-03 14:48:48 +00:00
|
|
|
self.sweep_max_freq_Hz = None
|
2021-04-06 09:05:13 +00:00
|
|
|
# [((min_freq, max_freq), [description]]. Order by increasing
|
|
|
|
# frequency. Put default output power first.
|
|
|
|
self.txPowerRanges = []
|
2020-07-07 14:48:43 +00:00
|
|
|
if self.connected():
|
2020-07-08 13:45:33 +00:00
|
|
|
self.version = self.readVersion()
|
2020-07-07 14:48:43 +00:00
|
|
|
self.read_features()
|
2020-07-15 12:47:06 +00:00
|
|
|
logger.debug("Features: %s", self.features)
|
2020-07-14 09:30:28 +00:00
|
|
|
# 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])
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-24 19:11:34 +00:00
|
|
|
def connect(self):
|
|
|
|
logger.info("connect %s", self.serial)
|
|
|
|
with self.serial.lock:
|
|
|
|
self.serial.open()
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
logger.info("disconnect %s", self.serial)
|
|
|
|
with self.serial.lock:
|
|
|
|
self.serial.close()
|
|
|
|
|
|
|
|
def reconnect(self):
|
|
|
|
self.disconnect()
|
|
|
|
sleep(WAIT)
|
|
|
|
self.connect()
|
|
|
|
sleep(WAIT)
|
|
|
|
|
|
|
|
def exec_command(self, command: str, wait: float = WAIT) -> Iterator[str]:
|
2020-07-08 13:45:33 +00:00
|
|
|
logger.debug("exec_command(%s)", command)
|
2020-07-07 14:48:43 +00:00
|
|
|
with self.serial.lock:
|
|
|
|
drain_serial(self.serial)
|
|
|
|
self.serial.write(f"{command}\r".encode('ascii'))
|
|
|
|
sleep(wait)
|
|
|
|
retries = 0
|
2020-07-14 09:30:28 +00:00
|
|
|
max_retries = _max_retries(self.bandwidth, self.datapoints)
|
|
|
|
logger.debug("Max retries: %s", max_retries)
|
2020-07-07 14:48:43 +00:00
|
|
|
while True:
|
|
|
|
line = self.serial.readline()
|
|
|
|
line = line.decode("ascii").strip()
|
|
|
|
if not line:
|
|
|
|
retries += 1
|
2020-07-14 09:30:28 +00:00
|
|
|
if retries > max_retries:
|
2020-07-07 14:48:43 +00:00
|
|
|
raise IOError("too many retries")
|
2020-07-12 22:09:00 +00:00
|
|
|
sleep(wait)
|
2020-07-07 14:48:43 +00:00
|
|
|
continue
|
|
|
|
if line == command: # suppress echo
|
|
|
|
continue
|
|
|
|
if line.startswith("ch>"):
|
2020-07-12 22:09:00 +00:00
|
|
|
logger.debug("Needed retries: %s", retries)
|
2020-07-07 14:48:43 +00:00
|
|
|
break
|
|
|
|
yield line
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-07 14:48:43 +00:00
|
|
|
def read_features(self):
|
2020-07-14 09:30:28 +00:00
|
|
|
result = " ".join(self.exec_command("help")).split()
|
2020-07-07 14:48:43 +00:00
|
|
|
logger.debug("result:\n%s", result)
|
|
|
|
if "capture" in result:
|
2020-06-10 10:28:51 +00:00
|
|
|
self.features.add("Screenshots")
|
2020-07-14 09:30:28 +00:00
|
|
|
if "bandwidth" in result:
|
|
|
|
self.features.add("Bandwidth")
|
2020-07-15 12:47:06 +00:00
|
|
|
result = " ".join(list(self.exec_command("bandwidth")))
|
|
|
|
if "Hz)" in result:
|
|
|
|
self.bw_method = "dislord"
|
2020-07-07 10:33:32 +00:00
|
|
|
if len(self.valid_datapoints) > 1:
|
2020-06-17 17:36:06 +00:00
|
|
|
self.features.add("Customizable data points")
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-14 09:30:28 +00:00
|
|
|
def get_bandwidths(self) -> List[int]:
|
|
|
|
logger.debug("get bandwidths")
|
2020-07-15 12:47:06 +00:00
|
|
|
if self.bw_method == "dislord":
|
|
|
|
return list(DISLORD_BW.keys())
|
|
|
|
result = " ".join(list(self.exec_command("bandwidth")))
|
2020-07-14 09:30:28 +00:00
|
|
|
try:
|
|
|
|
result = result.split(" {")[1].strip("}")
|
|
|
|
return sorted([int(i) for i in result.split("|")])
|
|
|
|
except IndexError:
|
2020-07-15 12:47:06 +00:00
|
|
|
return [1000, ]
|
2020-07-14 09:30:28 +00:00
|
|
|
|
2020-07-15 12:47:06 +00:00
|
|
|
def set_bandwidth(self, bandwidth: int):
|
2022-03-17 16:54:52 +00:00
|
|
|
bw_val = DISLORD_BW[bandwidth] \
|
|
|
|
if self.bw_method == "dislord" else bandwidth
|
2020-07-15 12:47:06 +00:00
|
|
|
result = " ".join(self.exec_command(f"bandwidth {bw_val}"))
|
|
|
|
if self.bw_method == "ttrftech" and result:
|
|
|
|
raise IOError(f"set_bandwith({bandwidth}: {result}")
|
|
|
|
self.bandwidth = bandwidth
|
2020-07-14 09:30:28 +00:00
|
|
|
|
2020-06-14 07:19:28 +00:00
|
|
|
def readFrequencies(self) -> List[int]:
|
2020-07-04 18:37:37 +00:00
|
|
|
return [int(f) for f in self.readValues("frequencies")]
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def resetSweep(self, start: int, stop: int):
|
|
|
|
pass
|
|
|
|
|
2020-12-23 15:15:08 +00:00
|
|
|
def _get_running_frequencies(self):
|
|
|
|
'''
|
|
|
|
If possible, read frequencies already runnung
|
|
|
|
if not return default values
|
|
|
|
Overwrite in specific HW
|
|
|
|
'''
|
|
|
|
return 27000000, 30000000
|
|
|
|
|
2020-07-05 11:00:03 +00:00
|
|
|
def connected(self) -> bool:
|
|
|
|
return self.serial.is_open
|
|
|
|
|
2020-05-16 15:10:07 +00:00
|
|
|
def getFeatures(self) -> List[str]:
|
|
|
|
return self.features
|
|
|
|
|
|
|
|
def getCalibration(self) -> str:
|
2020-07-15 12:47:06 +00:00
|
|
|
return " ".join(list(self.exec_command("cal")))
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def getScreenshot(self) -> QtGui.QPixmap:
|
|
|
|
return QtGui.QPixmap()
|
|
|
|
|
|
|
|
def flushSerialBuffers(self):
|
2020-07-09 10:46:26 +00:00
|
|
|
if not self.connected():
|
|
|
|
return
|
2020-07-04 10:08:54 +00:00
|
|
|
with self.serial.lock:
|
2020-07-01 17:46:35 +00:00
|
|
|
self.serial.write("\r\n\r\n".encode("ascii"))
|
2020-05-16 15:10:07 +00:00
|
|
|
sleep(0.1)
|
|
|
|
self.serial.reset_input_buffer()
|
|
|
|
self.serial.reset_output_buffer()
|
|
|
|
sleep(0.1)
|
|
|
|
|
|
|
|
def readFirmware(self) -> str:
|
2020-07-07 14:48:43 +00:00
|
|
|
result = "\n".join(list(self.exec_command("info")))
|
|
|
|
logger.debug("result:\n%s", result)
|
|
|
|
return result
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def readValues(self, value) -> List[str]:
|
|
|
|
logger.debug("VNA reading %s", value)
|
2020-07-07 14:48:43 +00:00
|
|
|
result = list(self.exec_command(value))
|
|
|
|
logger.debug("VNA done reading %s (%d values)",
|
|
|
|
value, len(result))
|
|
|
|
return result
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-07-08 13:45:33 +00:00
|
|
|
def readVersion(self) -> 'Version':
|
|
|
|
result = list(self.exec_command("version"))
|
|
|
|
logger.debug("result:\n%s", result)
|
|
|
|
return Version(result[0])
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def setSweep(self, start, stop):
|
2020-07-07 14:48:43 +00:00
|
|
|
list(self.exec_command(f"sweep {start} {stop} {self.datapoints}"))
|
2021-04-06 09:05:13 +00:00
|
|
|
|
|
|
|
def setTXPower(self, freq_range, power_desc):
|
|
|
|
raise NotImplementedError()
|