def reconnect(self):
     """
     Simulates a board reconnection. 
     :return: 
     """
     self.disconnect()
     time.sleep(1)
     if self.sim is not None:
         if not self.sim.is_connected():
             raise ConnectionFailed(
                 'The simulated connection status is False.')
         else:
             logger.debug('Simulating a board connect')
             self.connected = True
     else:
         raise ConnectionFailed('The simulator is not running yet.')
コード例 #2
0
 def flush_input(self):
     """
     Flushes the serial input. 
     
     :return: 
     """
     try:
         self.port.flushInput()
     except serial.SerialException as e:
         raise ConnectionFailed(e)
コード例 #3
0
 def disconnect(self):
     """
     Closes the COM port
     :return: 
     """
     if self.is_connected():
         try:
             self.port.close()
         except serial.SerialException as e:
             raise ConnectionFailed(e)
コード例 #4
0
    def find_com_port(self):
        """
        Finds all of the appropriate COM port based on the set PID and VID. Will automatically select the first match.
        
        TODO: Need to handle multiple connections. It might not be uncommon to have multiple Arduinos or FTDI USB to 
        Serial devices connected to a computer at once. 
        
        :return: str - port name
        """

        # Filter on just the PIDs and VIDs that match, if provided
        if self.pid is not None and self.vid is not None:
            # If the port name is auto, find the control board by searching through the PID and VID
            port_names = [
                port.device for port in lp.comports()
                if port.pid == self.pid and port.vid == self.vid
            ]
        else:
            # If PID or VID is not supplied, list all the com ports
            port_names = [port.device for port in lp.comports()]

        # If Auto, select the first port in the port_names list
        if self.port_name.lower(
        ) == 'auto' and self.pid is not None and self.vid is not None:
            if not any(port_names):
                raise ConnectionFailed(
                    'No valid COM ports found for the control board. Is it connected?'
                )
            elif len(port_names) > 1:
                # TODO: Handle multiple port_names
                port_name = port_names[0]
            else:
                port_name = port_names[0]
        else:
            # This is the hard-set COM port setting.
            if self.port_name in port_names:
                port_name = self.port_name
            else:
                raise ConnectionFailed('Invalid COM port selected.')

        return port_name
コード例 #5
0
 def is_connected(self):
     """
     Returns True is the serial port is valid and open.
     :return: bool - Serial port connected
     """
     try:
         if self.port is None:
             return False
         else:
             return self.port.isOpen()
     except serial.SerialException as e:
         raise ConnectionFailed(e)
コード例 #6
0
 def pulse_dtr(self, assert_time=50e-3):
     """
     Pulses the DTR pin for a given assertion time in seconds.
     :param assert_time: float - seconds to hold the assertion. (Default = 50ms)
     :return: 
     """
     try:
         self.port.dtr = 1
         time.sleep(assert_time)
         self.port.dtr = 0
     except serial.SerialException as e:
         raise ConnectionFailed(e)
コード例 #7
0
 def write_line(self, data_out):
     """
     Writes a line of data to the serial output. Flushes the output before continuing. 
     
     :param data_out: 
     :return: 
     """
     try:
         self.port.write(str.encode(data_out + '\r\n'))
         self.port.flushOutput()
     except serial.SerialTimeoutException as e:
         raise ConnectionTimeout(e)
     except serial.SerialException as e:
         raise ConnectionFailed(e)
コード例 #8
0
 def connect(self):
     """
     Attempts to attach/connect to a serial port. 
     :return: 
     """
     try:
         port_name = self.find_com_port()
         self.port = serial.Serial(port=port_name,
                                   baudrate=self.BAUD_RATE,
                                   timeout=self.timeout)
     except serial.SerialTimeoutException as e:
         raise ConnectionTimeout(e)
     except serial.SerialException as e:
         time.sleep(1)
         raise ConnectionFailed(e)
    def update(self):
        """
        Updates the simulator with output data. Receives a response packet with input data.
        :return: 
        """
        if not self.is_connected():
            raise ConnectionFailed('The simulator connection status is False.')

        # Check that there is a simulator frame connected
        if self.sim is not None:
            # Write outputs
            self.sim.set_pwms(self.getPwmValues())
            self.sim.set_leds(self.getLedValues())

            # Update screen
            self.sim.update_indicators()
            # CPU Saver - This is the only bound on how fast the simulator updates.
            time.sleep(20e-3)

            # Get inputs
            self.putAnalogvalues(self.sim.get_analogs())
            self.putSwitchvalues(self.sim.get_switches())
        else:
            raise ConnectionFailed('The simulator is not running yet.')
コード例 #10
0
 def read_line(self):
     """
     Reads a line of data from the serial input.
     
     :return: utf-8 data
     """
     try:
         data = self.port.readline().decode('utf-8')
         if not any(data):
             raise ConnectionTimeout('No data was read in time.')
         return data
     except serial.SerialTimeoutException as e:
         raise ConnectionTimeout(e)
     except serial.SerialException as e:
         raise ConnectionFailed(e)
     except UnicodeDecodeError as e:
         raise DataIntegrityError(e)