Skip to content

Fix: Throttle serial connection retries #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions BabbleApp/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def run(self):
):
port = self.config.capture_source
self.current_capture_source = port
self.start_serial_connection(port)
if self.start_serial_connection(port):
time.sleep(0.3)
continue
elif ViveTracker.is_device_vive_tracker(self.config.capture_source):
if self.cv2_camera is not None:
self.cv2_camera.release()
Expand Down Expand Up @@ -312,31 +314,29 @@ def start_serial_connection(self, port):
if self.serial_connection is not None and self.serial_connection.is_open:
# Do nothing. The connection is already open on this port.
if self.serial_connection.port == port:
return
return False
# Otherwise, close the connection before trying to reopen.
self.serial_connection.close()
com_ports = [tuple(p) for p in list(serial.tools.list_ports.comports())]
# Do not try connecting if no such port i.e. device was unplugged.
if not any(p for p in com_ports if port in p):
return
return True
try:
rate = 115200 if sys.platform == "darwin" else 3000000 # Higher baud rate not working on macOS
conn = serial.Serial(baudrate=rate, port=port, xonxoff=False, dsrdtr=False, rtscts=False)
# Set explicit buffer size for serial. This function is Windows only!
if os_type == 'Windows':
conn.set_buffer_size(rx_size=BUFFER_SIZE, tx_size=BUFFER_SIZE)

print(
f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRConnected")} {port}{Fore.RESET}'
)
print(f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRConnected")} {port}{Fore.RESET}')
self.serial_connection = conn
self.camera_status = CameraState.CONNECTED
return False
except Exception as e:
print(
f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRFailiure")} {port}{Fore.RESET}'
)
print(f'{Fore.CYAN}[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.ETVRFailiure")} {port}{Fore.RESET}')
print(e)
self.camera_status = CameraState.DISCONNECTED
return True

def clamp_max_res(self, image: MatLike) -> MatLike:
shape = image.shape
Expand Down