def __init__(self, port): """ Connect to the Arduino board and create a logger to track communication. Args: Port: The stored port to attempt to connect to. Raises: SerialException: Unable to connect to the board. """ self._logger = Logger(name="arduino_log") self._connection = None self._establish_connection(port)
def __init__(self, port): """ Connect to the Arduino board and create a logger to track communication. Args: Port: The stored port to attempt to connect to. Raises: SerialException: Unable to connect to the board. """ self._logger = Logger(name = "arduino_log") self._connection = None self._establish_connection(port)
class BoardCommunicator(object): """ Class actually responsible for the serial communication with the board. Needs to keep a running connection with the board, and reconnect if necessary. Reconnection will reboot the arduino, so homing is required. Attributes: _connection: The serial connection with the Arduino board. _logger: An Arduino-communication specific logger. Logs messages to and from the Arduino board. """ def __init__(self, port): """ Connect to the Arduino board and create a logger to track communication. Args: Port: The stored port to attempt to connect to. Raises: SerialException: Unable to connect to the board. """ self._logger = Logger(name = "arduino_log") self._connection = None self._establish_connection(port) def send(self, message): """ Send a message to the Arduino board. Args: message: the string to be sent to the Arduino over the serial connection Returns: An integer specifying the success or error of sending the message to the Arduino board. Raises: SerialException: the connection to the Arduino board has been closed """ self._logger.log("Sent to arduino: '%s'" %message) if self._connection.isOpen(): self._connection.flushInput() self._connection.flushOutput() self._connection.write(message) else: raise SerialException("Connection to arduino board closed") #blocking waiting for feedback while(self._connection.inWaiting() == 0): pass sleep(1) #allows for the entire message to be transmitted result = int(self._connection.read(self._connection.inWaiting()).rstrip()) self._logger.log("Received from arduino: '%d'" %result) return result def _establish_connection(self, port): """ Helper method to be used by the BoardCommunicator only to connect to the arduino board. Tells the arduino to home upon connection. """ cxn = Serial() cxn.baudrate = 115200 self._logger.log("Connecting to arduino board at %s" %port) cxn.port = port #Attempt to connect at the previously stored port. try: cxn.open() except (SerialException, OSError): cxn.close() self._logger.log("Failed connection to stored port %s" %port) if cxn.isOpen(): sleep(3) while cxn.inWaiting() > 0: cxn.read() #Send the handshake message cxn.write("connection") sleep(3) msg = cxn.read(cxn.inWaiting()).strip() self._logger.log("Handshake string received from arduino: %r" %msg) if msg == "main": self._logger.log("Main Arduino control unit found at port %s" %port) self._connection = cxn if self.send("h") == 0: self._logger.log("Homing of camera successful") return else: self._logger.log("Homing failed upon connection") raise SerialException("Homing Error, please check machine") else: self._logger.log("Connection at port %s was not the Arduino" + " control unit") cxn.close() #If the stored port fails, search available ports, trying the handshake #at each one else: self._logger.log("Searching available ports for the Arduino " "control unit") cxn.close() for searched_port in list_ports.comports(): cxn.port = searched_port[0] try: cxn.open() except (SerialException, OSError): self._logger.log("Failed connection to searched port %s" %searched_port[0]) if cxn.isOpen(): sleep(3) while cxn.inWaiting() > 0: cxn.read() cxn.write("connection") sleep(3) msg = cxn.read(cxn.inWaiting()).strip() self._logger.log("Handshake string received from arduino: %r" %msg) if msg == "main": self._logger.log("Main Arduino control unit found at port %s" %searched_port[0]) self._connection = cxn utils.update_config_dict("CameraCommunicator", dict(arduino_port = searched_port[0])) if self.send("h") == 0: self._logger.log("Homing of camera successful") return else: self._logger.log("Homing failed upon connection") raise SerialException("Homing Error, please check machine") else: self._logger.log(("Connection at port %s was not the Arduino" + " control unit") %searched_port[0]) cxn.close() if self._connection is None: self._logger.log("Did not connect to the Arduino Board after " + "searching all ports") raise SerialException("Did not connect to the Arduino Board")
class BoardCommunicator(object): """ Class actually responsible for the serial communication with the board. Needs to keep a running connection with the board, and reconnect if necessary. Reconnection will reboot the arduino, so homing is required. Attributes: _connection: The serial connection with the Arduino board. _logger: An Arduino-communication specific logger. Logs messages to and from the Arduino board. """ def __init__(self, port): """ Connect to the Arduino board and create a logger to track communication. Args: Port: The stored port to attempt to connect to. Raises: SerialException: Unable to connect to the board. """ self._logger = Logger(name="arduino_log") self._connection = None self._establish_connection(port) def send(self, message): """ Send a message to the Arduino board. Args: message: the string to be sent to the Arduino over the serial connection Returns: An integer specifying the success or error of sending the message to the Arduino board. Raises: SerialException: the connection to the Arduino board has been closed """ self._logger.log("Sent to arduino: '%s'" % message) if self._connection.isOpen(): self._connection.flushInput() self._connection.flushOutput() self._connection.write(message) else: raise SerialException("Connection to arduino board closed") #blocking waiting for feedback while (self._connection.inWaiting() == 0): pass sleep(1) #allows for the entire message to be transmitted result = int( self._connection.read(self._connection.inWaiting()).rstrip()) self._logger.log("Received from arduino: '%d'" % result) return result def _establish_connection(self, port): """ Helper method to be used by the BoardCommunicator only to connect to the arduino board. Tells the arduino to home upon connection. """ cxn = Serial() cxn.baudrate = 115200 self._logger.log("Connecting to arduino board at %s" % port) cxn.port = port #Attempt to connect at the previously stored port. try: cxn.open() except (SerialException, OSError): cxn.close() self._logger.log("Failed connection to stored port %s" % port) if cxn.isOpen(): sleep(3) while cxn.inWaiting() > 0: cxn.read() #Send the handshake message cxn.write("connection") sleep(3) msg = cxn.read(cxn.inWaiting()).strip() self._logger.log("Handshake string received from arduino: %r" % msg) if msg == "main": self._logger.log("Main Arduino control unit found at port %s" % port) self._connection = cxn if self.send("h") == 0: self._logger.log("Homing of camera successful") return else: self._logger.log("Homing failed upon connection") raise SerialException("Homing Error, please check machine") else: self._logger.log("Connection at port %s was not the Arduino" + " control unit") cxn.close() #If the stored port fails, search available ports, trying the handshake #at each one else: self._logger.log("Searching available ports for the Arduino " "control unit") cxn.close() for searched_port in list_ports.comports(): cxn.port = searched_port[0] try: cxn.open() except (SerialException, OSError): self._logger.log("Failed connection to searched port %s" % searched_port[0]) if cxn.isOpen(): sleep(3) while cxn.inWaiting() > 0: cxn.read() cxn.write("connection") sleep(3) msg = cxn.read(cxn.inWaiting()).strip() self._logger.log( "Handshake string received from arduino: %r" % msg) if msg == "main": self._logger.log( "Main Arduino control unit found at port %s" % searched_port[0]) self._connection = cxn utils.update_config_dict( "CameraCommunicator", dict(arduino_port=searched_port[0])) if self.send("h") == 0: self._logger.log("Homing of camera successful") return else: self._logger.log("Homing failed upon connection") raise SerialException( "Homing Error, please check machine") else: self._logger.log( ("Connection at port %s was not the Arduino" + " control unit") % searched_port[0]) cxn.close() if self._connection is None: self._logger.log("Did not connect to the Arduino Board after " + "searching all ports") raise SerialException("Did not connect to the Arduino Board")