Skip to content

Commit

Permalink
- Error handling for serial port problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mihtjel committed Oct 1, 2019
1 parent b9f9cd0 commit 1397aef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
22 changes: 11 additions & 11 deletions NanoVNASaver/Calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,41 +802,41 @@ def loadCalibration(self, filename):
lines = file.readlines()
parsed_header = False

for l in lines:
l = l.strip()
if l.startswith("!"):
note = l[2:]
for line in lines:
line = line.strip()
if line.startswith("!"):
note = line[2:]
self.notes.append(note)
continue
if l.startswith("#") and not parsed_header:
if line.startswith("#") and not parsed_header:
# Check that this is a valid header
if l == "# Hz ShortR ShortI OpenR OpenI LoadR LoadI ThroughR ThroughI IsolationR IsolationI":
if line == "# Hz ShortR ShortI OpenR OpenI LoadR LoadI ThroughR ThroughI IsolationR IsolationI":
parsed_header = True
continue
else:
# This is some other comment line
continue
if not parsed_header:
logger.warning("Warning: Read line without having read header: %s", l)
logger.warning("Warning: Read line without having read header: %s", line)
continue

try:
if l.count(" ") == 6:
freq, shortr, shorti, openr, openi, loadr, loadi = l.split(" ")
if line.count(" ") == 6:
freq, shortr, shorti, openr, openi, loadr, loadi = line.split(" ")
self.s11short.append(Datapoint(int(freq), float(shortr), float(shorti)))
self.s11open.append(Datapoint(int(freq), float(openr), float(openi)))
self.s11load.append(Datapoint(int(freq), float(loadr), float(loadi)))

else:
freq, shortr, shorti, openr, openi, loadr, loadi, throughr, throughi, isolationr, isolationi = l.split(" ")
freq, shortr, shorti, openr, openi, loadr, loadi, throughr, throughi, isolationr, isolationi = line.split(" ")
self.s11short.append(Datapoint(int(freq), float(shortr), float(shorti)))
self.s11open.append(Datapoint(int(freq), float(openr), float(openi)))
self.s11load.append(Datapoint(int(freq), float(loadr), float(loadi)))
self.s21through.append(Datapoint(int(freq), float(throughr), float(throughi)))
self.s21isolation.append(Datapoint(int(freq), float(isolationr), float(isolationi)))

except ValueError as e:
logger.exception("Error parsing calibration data \"%s\": %s", l, e)
logger.exception("Error parsing calibration data \"%s\": %s", line, e)
file.close()
except Exception as e:
logger.exception("Failed loading calibration data: %s", e)
1 change: 0 additions & 1 deletion NanoVNASaver/Chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,6 @@ def drawChart(self, qp: QtGui.QPainter):
qp.drawText(3, 35, str(self.maxQ))

def drawValues(self, qp: QtGui.QPainter):
from NanoVNASaver.NanoVNASaver import NanoVNASaver
if len(self.data) == 0 and len(self.reference) == 0:
return
if self.span == 0:
Expand Down
20 changes: 13 additions & 7 deletions NanoVNASaver/NanoVNASaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,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 +700,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 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

0 comments on commit 1397aef

Please sign in to comment.