Skip to content

Commit 294ad27

Browse files
committed
Added custom PozyxExceptions. Deprecated trace shows and quit.
1 parent e331038 commit 294ad27

1 file changed

Lines changed: 16 additions & 25 deletions

File tree

pypozyx/pozyx_serial.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ def is_correct_pyserial_version():
105105
return True
106106
return False
107107

108+
109+
class PozyxException(IOError):
110+
"""Base class for Pozyx related exceptions"""
111+
112+
113+
class PozyxConnectionError(PozyxException):
114+
"""Bad connection to Pozyx gives an exception"""
115+
108116
# @}
109117

110118

@@ -125,7 +133,7 @@ class PozyxSerial(PozyxLib):
125133
timeout: timeout for the serial port communication in seconds. Default is 0.1s or 100ms.
126134
print_output: boolean for printing the serial exchanges, mainly for debugging purposes
127135
debug_trace: boolean for printing the trace on bad serial init (DEPRECATED)
128-
show_trace: boolean for printing the trace on bad serial init (replaces debug_trace)
136+
show_trace: boolean for printing the trace on bad serial init (DEPRECATED)
129137
suppress_warnings: boolean for suppressing warnings in the Pozyx use, usage not recommended
130138
131139
Example usage:
@@ -150,11 +158,10 @@ def __init__(self, port, baudrate=115200, timeout=0.1, write_timeout=0.1,
150158
suppress_warnings=False):
151159
"""Initializes the PozyxSerial object. See above for details."""
152160
self.print_output = print_output
153-
if debug_trace is True:
161+
if debug_trace is True or show_trace is True:
154162
if not suppress_warnings:
155-
warn(
156-
"Using debug_trace is on its way out, please use show_trace in the future", DeprecationWarning)
157-
self.show_trace = debug_trace or show_trace
163+
warn("debug_trace or show_trace are on their way out, exceptions of the type PozyxException are now raised.",
164+
DeprecationWarning)
158165
self.suppress_warnings = suppress_warnings
159166

160167
self.connectToPozyx(port, baudrate, timeout, write_timeout)
@@ -179,34 +186,18 @@ def connectToPozyx(self, port, baudrate, timeout, write_timeout):
179186
self.ser = Serial(port=port, baudrate=baudrate, timeout=timeout,
180187
writeTimeout=write_timeout)
181188
except SerialException as exc:
182-
print("Wrong or busy serial port, SerialException:", str(exc))
183-
self.printTrace()
184-
quit()
189+
raise PozyxConnectionError("Wrong or busy serial port, SerialException: {}".format(str(exc)))
185190
except Exception as exc:
186-
print("Couldn't connect to Pozyx, unknown exception:", str(exc))
187-
self.printTrace()
188-
quit()
191+
raise PozyxConnectionError("Couldn't connect to Pozyx, {}: {}".format(exc.__class__.__name__, str(exc)))
189192

190193
def validatePozyx(self):
191194
"""Validates whether the connected device is indeed a Pozyx device"""
192195
whoami = SingleRegister()
193196
if self.getWhoAmI(whoami) != POZYX_SUCCESS:
194-
195-
print("Connected to device, but couldn't read serial data. Is it a Pozyx?")
196-
self.printTrace()
197-
quit()
197+
raise PozyxConnectionError("Connected to device, but couldn't read serial data. Is it a Pozyx?")
198198

199199
if whoami.value != 0x43:
200-
print("POZYX_WHO_AM_I returned 0x%0.2x, something is wrong with Pozyx." %
201-
whoami.value)
202-
quit()
203-
204-
def printTrace(self):
205-
"""Prints the trace, handy for debugging. Enabled by show_trace flag"""
206-
if self.show_trace:
207-
import traceback
208-
import sys
209-
traceback.print_tb(sys.exc_info()[2])
200+
raise PozyxConnectionError("POZYX_WHO_AM_I returned 0x%0.2x, something is wrong with Pozyx." % whoami.value)
210201

211202
# @}
212203

0 commit comments

Comments
 (0)