Skip to content

Commit 9d3704c

Browse files
authored
Merge pull request #112 from SnuffSocket/fix-throtser
Fix: Throttle serial connection retries
2 parents f229c74 + 08b6102 commit 9d3704c

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

BabbleApp/camera.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def run(self):
111111
):
112112
port = self.config.capture_source
113113
self.current_capture_source = port
114-
self.start_serial_connection(port)
114+
if self.start_serial_connection(port):
115+
time.sleep(0.3)
116+
continue
115117
elif ViveTracker.is_device_vive_tracker(self.config.capture_source):
116118
if self.cv2_camera is not None:
117119
self.cv2_camera.release()
@@ -318,31 +320,29 @@ def start_serial_connection(self, port):
318320
if self.serial_connection is not None and self.serial_connection.is_open:
319321
# Do nothing. The connection is already open on this port.
320322
if self.serial_connection.port == port:
321-
return
323+
return False
322324
# Otherwise, close the connection before trying to reopen.
323325
self.serial_connection.close()
324326
com_ports = [tuple(p) for p in list(serial.tools.list_ports.comports())]
325327
# Do not try connecting if no such port i.e. device was unplugged.
326328
if not any(p for p in com_ports if port in p):
327-
return
329+
return True
328330
try:
329331
rate = 115200 if sys.platform == "darwin" else 3000000 # Higher baud rate not working on macOS
330332
conn = serial.Serial(baudrate=rate, port=port, xonxoff=False, dsrdtr=False, rtscts=False)
331333
# Set explicit buffer size for serial. This function is Windows only!
332334
if os_type == 'Windows':
333335
conn.set_buffer_size(rx_size=BUFFER_SIZE, tx_size=BUFFER_SIZE)
334336

335-
print(
336-
f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRConnected")} {port}{Fore.RESET}'
337-
)
337+
print(f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRConnected")} {port}{Fore.RESET}')
338338
self.serial_connection = conn
339339
self.camera_status = CameraState.CONNECTED
340+
return False
340341
except Exception as e:
341-
print(
342-
f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRFailiure")} {port}{Fore.RESET}'
343-
)
342+
print(f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRFailiure")} {port}{Fore.RESET}')
344343
print(e)
345344
self.camera_status = CameraState.DISCONNECTED
345+
return True
346346

347347
def clamp_max_res(self, image: MatLike) -> MatLike:
348348
shape = image.shape

0 commit comments

Comments
 (0)