def disconnect(self): """Closes the serial connection. Raises: error.BadSerialConnection if unable to close the connection. """ # Fortunately, close() doesn't throw an exception if already closed. self.logger.debug('SerialData.disconnect called for {}', self.name) try: self.ser.close() except Exception as err: raise error.BadSerialConnection( msg="SerialData.disconnect failed for {}; underlying error: {}" .format(self.name, err)) if self.is_connected: raise error.BadSerialConnection( msg="SerialData.disconnect failed for {}".format(self.name))
def connect(self): """If disconnected, then connect to the serial port. Raises: error.BadSerialConnection if unable to open the connection. """ if self.is_connected: self.logger.debug('Connection already open to {}', self.name) return self.logger.debug('SerialData.connect called for {}', self.name) try: # Note: we must not call open when it is already open, else an exception is thrown of # the same type thrown when open fails to actually open the device. self.ser.open() if not self.is_connected: raise error.BadSerialConnection( msg="Serial connection {} is not open".format(self.name)) except serial.serialutil.SerialException as err: raise error.BadSerialConnection(msg=err) self.logger.debug('Serial connection established to {}', self.name)
def _connect(self): """ Sets up serial connection """ self.logger.debug('Making serial connection for mount at {}'.format( self._port)) try: self.serial.connect() except Exception: raise error.BadSerialConnection( 'Cannot create serial connect for mount at port {}'.format( self._port)) self.logger.debug('Mount connected via serial')
def verify_connected(self): """Throw an exception if not connected.""" if not self.is_connected: raise error.BadSerialConnection( msg='Not connected to dome at port {}'.format(self._port))