Removed redundant code from hardware

pull/231/head
Holger Müller 2020-07-07 16:48:43 +02:00
rodzic 27bd5ca2b6
commit 28fb2e280f
4 zmienionych plików z 57 dodań i 159 usunięć

Wyświetl plik

@ -17,8 +17,6 @@
# 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
from typing import List
from NanoVNASaver.Hardware.Serial import Interface
from NanoVNASaver.Hardware.VNA import VNA, Version
@ -38,9 +36,8 @@ class AVNA(VNA):
return True
def resetSweep(self, start: int, stop: int):
self.writeSerial(f"sweep {start} {stop} {self.datapoints}")
self.writeSerial("resume")
list(self.exec_command(f"sweep {start} {stop} {self.datapoints}"))
list(self.exec_command("resume"))
def setSweep(self, start, stop):
self.writeSerial(f"sweep {start} {stop} {self.datapoints}")
sleep(1)
list(self.exec_command(f"sweep {start} {stop} {self.datapoints}"))

Wyświetl plik

@ -18,7 +18,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import struct
from time import sleep
from time import sleep, time
from typing import List
import serial
@ -45,20 +45,18 @@ class NanoVNA(VNA):
self.stop = 30000000
self._sweepdata = []
def isValid(self):
return True
def _capture_data(self) -> bytes:
timeout = self.serial.timeout
with self.serial.lock:
drain_serial(self.serial)
timeout = self.serial.timeout
self.serial.write("capture\r".encode('ascii'))
self.serial.timeout = 4
self.serial.readline()
self.serial.timeout = 4
image_data = self.serial.read(
self.screenwidth * self.screenheight * 2)
self.serial.timeout = timeout
self.serial.timeout = timeout
rgb_data = struct.unpack(
f">{self.screenwidth * self.screenheight}H",
image_data)
@ -87,16 +85,16 @@ class NanoVNA(VNA):
return QtGui.QPixmap()
def resetSweep(self, start: int, stop: int):
self.writeSerial(f"sweep {start} {stop} {self.datapoints}")
self.writeSerial("resume")
list(self.exec_command(f"sweep {start} {stop} {self.datapoints}"))
list(self.exec_commend("resume"))
def setSweep(self, start, stop):
self.start = start
self.stop = stop
if self.sweep_method == "sweep":
self.writeSerial(f"sweep {start} {stop} {self.datapoints}")
list(self.exec_command(f"sweep {start} {stop} {self.datapoints}"))
elif self.sweep_method == "scan":
self.writeSerial(f"scan {start} {stop} {self.datapoints}")
list(self.exec_command(f"scan {start} {stop} {self.datapoints}"))
def read_features(self):
if self.version >= Version("0.7.1"):
@ -108,7 +106,6 @@ class NanoVNA(VNA):
self.sweep_method = "scan"
super().read_features()
def readFrequencies(self) -> List[int]:
if self.sweep_method != "scan_mask":
return super().readFrequencies()
@ -123,34 +120,12 @@ class NanoVNA(VNA):
# The hardware will return all channels which we will store.
if value == "data 0":
self._sweepdata = []
try:
with self.serial.lock:
drain_serial(self.serial)
self.serial.write(
(f"scan {self.start} {self.stop}"
f' {self.datapoints} 0b110\r'
).encode("ascii"))
self.serial.readline()
logger.info("reading values")
retries = 0
while True:
line = self.serial.readline()
line = line.decode("ascii").strip()
if not line:
retries += 1
logger.info("Retry nr: %s", retries)
if retries > 10:
raise IOError("too many retries")
sleep(0.2)
continue
if line.startswith("ch>"):
break
data = line.split()
self._sweepdata.append((
f"{data[0]} {data[1]}",
f"{data[2]} {data[3]}"))
except IOError as exc:
logger.error("Error readValues: %s", exc)
for line in self.exec_command(
f"scan {self.start} {self.stop} {self.datapoints} 0b110"):
data = line.split()
self._sweepdata.append((
f"{data[0]} {data[1]}",
f"{data[2]} {data[3]}"))
if value == "data 0":
return [x[0] for x in self._sweepdata]
if value == "data 1":

Wyświetl plik

@ -25,6 +25,7 @@ logger = logging.getLogger(__name__)
def drain_serial(serial_port: serial.Serial):
"""drain up to 64k outstanding data in the serial incoming buffer"""
logger.debug("Draining: %s", serial_port)
for _ in range(512):
cnt = len(serial_port.read(128))
if not cnt:

Wyświetl plik

@ -18,7 +18,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
from time import sleep
from typing import List
from typing import List, Iterator
import serial
from PyQt5 import QtGui
@ -39,15 +39,36 @@ class VNA:
self.features = set()
self.validateInput = True
self.datapoints = self.valid_datapoints[0]
self.read_features()
if self.connected():
self.read_features()
def exec_command(self, command: str, wait: float = 0.05) -> Iterator[str]:
logger.debug("_exec_command: %s", command)
with self.serial.lock:
drain_serial(self.serial)
self.serial.write(f"{command}\r".encode('ascii'))
sleep(wait)
retries = 0
while True:
line = self.serial.readline()
line = line.decode("ascii").strip()
if not line:
retries += 1
logger.debug("Retry nr: %s", retries)
if retries > 10:
raise IOError("too many retries")
sleep(0.1)
continue
if line == command: # suppress echo
continue
if line.startswith("ch>"):
break
yield line
def read_features(self):
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:
result = "\n".join(list(self.exec_command("help")))
logger.debug("result:\n%s", result)
if "capture" in result:
self.features.add("Screenshots")
if len(self.valid_datapoints) > 1:
self.features.add("Customizable data points")
@ -58,9 +79,6 @@ class VNA:
def resetSweep(self, start: int, stop: int):
pass
def isValid(self):
return False
def connected(self) -> bool:
return self.serial.is_open
@ -68,24 +86,7 @@ class VNA:
return self.features
def getCalibration(self) -> str:
logger.debug("Reading calibration info.")
if not self.connected():
return "Not connected."
with self.serial.lock:
try:
drain_serial(self.serial)
self.serial.write("cal\r".encode('ascii'))
result = ""
data = ""
sleep(0.1)
while "ch>" not in data:
data = self.serial.readline().decode('ascii')
result += data
values = result.splitlines()
return values[1]
except serial.SerialException as exc:
logger.exception("Exception while reading calibration info: %s", exc)
return "Unknown"
return list(self.exec_command("cal"))[0]
def getScreenshot(self) -> QtGui.QPixmap:
return QtGui.QPixmap()
@ -99,95 +100,19 @@ class VNA:
sleep(0.1)
def readFirmware(self) -> str:
try:
with self.serial.lock:
drain_serial(self.serial)
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
except serial.SerialException as exc:
logger.exception(
"Exception while reading firmware data: %s", exc)
return ""
def readFromCommand(self, command) -> str:
try:
with self.serial.lock:
drain_serial(self.serial)
self.serial.write(f"{command}\r".encode('ascii'))
result = ""
data = ""
sleep(0.01)
while data != "ch> ":
data = self.serial.readline().decode('ascii')
result += data
return result
except serial.SerialException as exc:
logger.exception(
"Exception while reading %s: %s", command, exc)
return ""
result = "\n".join(list(self.exec_command("info")))
logger.debug("result:\n%s", result)
return result
def readValues(self, value) -> List[str]:
logger.debug("VNA reading %s", value)
try:
with self.serial.lock:
drain_serial(self.serial)
self.serial.write(f"{value}\r".encode('ascii'))
result = ""
data = ""
sleep(0.05)
while data != "ch> ":
data = self.serial.readline().decode('ascii')
result += data
values = result.split("\r\n")
logger.debug(
"VNA done reading %s (%d values)",
value, len(values)-2)
return values[1:-1]
except serial.SerialException as exc:
logger.exception(
"Exception while reading %s: %s", value, exc)
return []
result = list(self.exec_command(value))
logger.debug("VNA done reading %s (%d values)",
value, len(result))
return result
def readVersion(self) -> str:
logger.debug("Reading version info.")
if not self.connected():
return ""
try:
with self.serial.lock:
drain_serial(self.serial)
self.serial.write("version\r".encode('ascii'))
result = ""
data = ""
sleep(0.1)
while "ch>" not in data:
data = self.serial.readline().decode('ascii')
result += data
values = result.splitlines()
logger.debug("Found version info: %s", values[1])
return values[1]
except serial.SerialException as exc:
logger.exception("Exception while reading firmware version: %s", exc)
return ""
def writeSerial(self, command):
if not self.connected():
logger.warning("Writing without serial port being opened (%s)",
command)
return
with self.serial.lock:
try:
self.serial.write(f"{command}\r".encode('ascii'))
self.serial.readline()
except serial.SerialException as exc:
logger.exception(
"Exception while writing to serial port (%s): %s",
command, exc)
return list(self.exec_command("version"))[0]
def setSweep(self, start, stop):
self.writeSerial(f"sweep {start} {stop} {self.datapoints}")
list(self.exec_command(f"sweep {start} {stop} {self.datapoints}"))