From a98158634c9ecddfde2463ad163c00ff4cdc2e67 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Thu, 21 Mar 2019 17:48:45 +0000 Subject: [PATCH] Close the camera if the stage cannot be opened --- openflexure_microscope/api/app.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 6e67129d..e7bddaa3 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -7,6 +7,7 @@ from flask import ( Flask, render_template) from flask.logging import default_handler +from serial import SerialException from flask_cors import CORS @@ -71,15 +72,19 @@ def attach_microscope(): api_camera = StreamingCamera(config=api_microscope.rc.read()) logging.debug("Creating stage object...") - api_stage = Stage("/dev/ttyUSB0") + try: + api_stage = Stage("/dev/ttyUSB0") + except SerialException as e: + api_camera.close() + raise e + else: + logging.debug("Attaching devices to microscope...") + api_microscope.attach( + api_camera, + api_stage + ) - logging.debug("Attaching devices to microscope...") - api_microscope.attach( - api_camera, - api_stage - ) - - logging.debug("Microscope successfully attached!") + logging.debug("Microscope successfully attached!") ##### WEBAPP ROUTES ######