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
|
2020-06-25 17:48:03 +00:00
|
|
|
# Copyright (C) 2020 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
|
|
|
|
from time import sleep
|
2020-05-18 18:31:27 +00:00
|
|
|
from typing import List
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
import serial
|
|
|
|
from PyQt5 import QtWidgets, QtGui
|
|
|
|
|
2020-06-25 17:48:03 +00:00
|
|
|
from NanoVNASaver.Settings import Version
|
2020-07-01 17:46:35 +00:00
|
|
|
from NanoVNASaver.Hardware.Serial import drain_serial
|
2020-06-25 17:48:03 +00:00
|
|
|
|
2020-05-16 15:10:07 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class VNA:
|
|
|
|
name = "VNA"
|
2020-06-17 17:36:06 +00:00
|
|
|
_datapoints = (101, )
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def __init__(self, app: QtWidgets.QWidget, serial_port: serial.Serial):
|
|
|
|
self.app = app
|
|
|
|
self.serial = serial_port
|
|
|
|
self.version: Version = Version("0.0.0")
|
2020-06-10 10:28:51 +00:00
|
|
|
self.features = set()
|
|
|
|
self.validateInput = True
|
2020-06-25 17:48:03 +00:00
|
|
|
self.datapoints = VNA._datapoints[0]
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def readFeatures(self) -> List[str]:
|
|
|
|
raw_help = self.readFromCommand("help")
|
|
|
|
logger.debug("Help command output:")
|
|
|
|
logger.debug(raw_help)
|
|
|
|
|
|
|
|
# Detect features from the help command
|
|
|
|
if "capture" in raw_help:
|
2020-06-10 10:28:51 +00:00
|
|
|
self.features.add("Screenshots")
|
2020-06-17 17:36:06 +00:00
|
|
|
if len(self._datapoints) > 1:
|
|
|
|
self.features.add("Customizable data points")
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-06-10 10:28:51 +00:00
|
|
|
return self.features
|
2020-05-16 15:10:07 +00:00
|
|
|
|
2020-06-14 07:19:28 +00:00
|
|
|
# TODO: check return types
|
|
|
|
def readFrequencies(self) -> List[int]:
|
|
|
|
return []
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def resetSweep(self, start: int, stop: int):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def isValid(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def isDFU(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getFeatures(self) -> List[str]:
|
|
|
|
return self.features
|
|
|
|
|
|
|
|
def getCalibration(self) -> str:
|
|
|
|
return "Unknown"
|
|
|
|
|
|
|
|
def getScreenshot(self) -> QtGui.QPixmap:
|
|
|
|
return QtGui.QPixmap()
|
|
|
|
|
|
|
|
def flushSerialBuffers(self):
|
2020-07-01 17:46:35 +00:00
|
|
|
with self.app.serialLock:
|
|
|
|
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-01 17:46:35 +00:00
|
|
|
try:
|
|
|
|
with self.app.serialLock:
|
|
|
|
drain_serial(self.serial)
|
2020-05-16 15:10:07 +00:00
|
|
|
self.serial.write("info\r".encode('ascii'))
|
|
|
|
result = ""
|
|
|
|
data = ""
|
|
|
|
sleep(0.01)
|
|
|
|
while data != "ch> ":
|
|
|
|
data = self.serial.readline().decode('ascii')
|
|
|
|
result += data
|
|
|
|
return result
|
2020-07-01 17:46:35 +00:00
|
|
|
except serial.SerialException as exc:
|
|
|
|
logger.exception(
|
|
|
|
"Exception while reading firmware data: %s", exc)
|
2020-06-15 11:27:00 +00:00
|
|
|
return ""
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def readFromCommand(self, command) -> str:
|
2020-07-01 17:46:35 +00:00
|
|
|
try:
|
|
|
|
with self.app.serialLock:
|
|
|
|
drain.serial(self.serial)
|
|
|
|
self.serial.write(f"{command}\r".encode('ascii'))
|
2020-05-16 15:10:07 +00:00
|
|
|
result = ""
|
|
|
|
data = ""
|
|
|
|
sleep(0.01)
|
|
|
|
while data != "ch> ":
|
|
|
|
data = self.serial.readline().decode('ascii')
|
|
|
|
result += data
|
|
|
|
return result
|
2020-07-01 17:46:35 +00:00
|
|
|
except serial.SerialException as exc:
|
|
|
|
logger.exception(
|
|
|
|
"Exception while reading %s: %s", command, exc)
|
2020-06-15 11:27:00 +00:00
|
|
|
return ""
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def readValues(self, value) -> List[str]:
|
|
|
|
logger.debug("VNA reading %s", value)
|
2020-07-01 17:46:35 +00:00
|
|
|
try:
|
|
|
|
with self.app.serialLock:
|
|
|
|
drain_serial(self.serial)
|
|
|
|
self.serial.write(f"{value}\r".encode('ascii'))
|
2020-05-16 15:10:07 +00:00
|
|
|
result = ""
|
|
|
|
data = ""
|
|
|
|
sleep(0.05)
|
|
|
|
while data != "ch> ":
|
|
|
|
data = self.serial.readline().decode('ascii')
|
|
|
|
result += data
|
2020-07-01 17:46:35 +00:00
|
|
|
values = result.split("\r\n")
|
2020-06-15 11:27:00 +00:00
|
|
|
logger.debug(
|
|
|
|
"VNA done reading %s (%d values)",
|
|
|
|
value, len(values)-2)
|
2020-05-16 15:10:07 +00:00
|
|
|
return values[1:-1]
|
2020-07-01 17:46:35 +00:00
|
|
|
except serial.SerialException as exc:
|
|
|
|
logger.exception(
|
|
|
|
"Exception while reading %s: %s", value, exc)
|
2020-06-15 11:27:00 +00:00
|
|
|
return []
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def writeSerial(self, command):
|
|
|
|
if not self.serial.is_open:
|
2020-06-15 11:27:00 +00:00
|
|
|
logger.warning("Writing without serial port being opened (%s)",
|
|
|
|
command)
|
2020-05-16 15:10:07 +00:00
|
|
|
return
|
2020-07-01 17:46:35 +00:00
|
|
|
with self.app.serialLock:
|
2020-05-16 15:10:07 +00:00
|
|
|
try:
|
2020-07-01 17:46:35 +00:00
|
|
|
self.serial.write(f"{command}\r".encode('ascii'))
|
2020-05-16 15:10:07 +00:00
|
|
|
self.serial.readline()
|
|
|
|
except serial.SerialException as exc:
|
2020-06-15 11:27:00 +00:00
|
|
|
logger.exception(
|
|
|
|
"Exception while writing to serial port (%s): %s",
|
|
|
|
command, exc)
|
2020-05-16 15:10:07 +00:00
|
|
|
|
|
|
|
def setSweep(self, start, stop):
|
2020-07-01 17:46:35 +00:00
|
|
|
self.writeSerial(f"sweep {start} {stop} {self.datapoints}")
|
2020-06-21 18:54:23 +00:00
|
|
|
|
|
|
|
|
2020-07-01 17:46:35 +00:00
|
|
|
# TODO: should be dropped and the serial part should be a connection class
|
|
|
|
# which handles unconnected devices
|
2020-05-16 15:10:07 +00:00
|
|
|
class InvalidVNA(VNA):
|
|
|
|
name = "Invalid"
|
2020-06-17 17:36:06 +00:00
|
|
|
_datapoints = (0, )
|
2020-06-21 18:54:23 +00:00
|
|
|
|
2020-05-16 15:10:07 +00:00
|
|
|
def setSweep(self, start, stop):
|
|
|
|
return
|
|
|
|
|
|
|
|
def resetSweep(self, start, stop):
|
|
|
|
return
|
|
|
|
|
|
|
|
def writeSerial(self, command):
|
|
|
|
return
|
|
|
|
|
|
|
|
def readFirmware(self):
|
|
|
|
return
|
|
|
|
|
|
|
|
def readFrequencies(self) -> List[int]:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def readValues(self, value):
|
|
|
|
return
|
|
|
|
|
|
|
|
def flushSerialBuffers(self):
|
|
|
|
return
|