Ejemplo n.º 1
0
    def open(self):
        '''Open the serial port.
        
        When the serial port is instantiated, it will try to open automatically.
        '''
        self.close()

        device = usb.get_usb_device(self.portstr)
        if not device:
            raise SerialException("Device not present {}".format(self.portstr))

        if not usb.has_usb_permission(device):
            usb.request_usb_permission(device)
            return

        connection = usb.get_usb_manager().openDevice(device)
        if not connection:
            raise SerialException("Failed to open device!")

        self._device = device
        self._connection = connection

        raw_descriptors = self._connection.getRawDescriptors()
        self._bcd_device = raw_descriptors[12] + raw_descriptors[13] * 256

        for i in range(self._device.getInterfaceCount()):
            if i == 0:
                self._interface = self._device.getInterface(i)
            if not self._connection.claimInterface(
                    self._device.getInterface(i), True):
                raise SerialException(
                    "Could not claim interface {}.".format(i))

        self._index = self._interface.getId() + 1

        for i in range(self._interface.getEndpointCount()):
            ep = self._interface.getEndpoint(i)
            if (
                (ep.getDirection() == usb.UsbConstants.USB_DIR_IN) \
                and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_INT)
            ):
                self._control_endpoint = ep
            elif (
                (ep.getDirection() == usb.UsbConstants.USB_DIR_IN) \
                and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK)
            ):
                self._read_endpoint = ep
            elif (
                (ep.getDirection() == usb.UsbConstants.USB_DIR_OUT) \
                and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK)
            ):
                self._write_endpoint = ep

        # Check that all endpoints are good
        if None in [self._write_endpoint, self._read_endpoint]:
            raise SerialException("Could not establish all endpoints!")

        self.is_open = True
        self._reconfigure_port()
Ejemplo n.º 2
0
    def open(self):
        """Open the serial port.

        When the serial port is instantiated, it will try to open automatically.
        """
        self.close()

        device = usb.get_usb_device(self.portstr)
        if not device:
            raise SerialException("Device not present {}".format(self.portstr))

        if not usb.has_usb_permission(device):
            usb.request_usb_permission(device)
            return

        connection = usb.get_usb_manager().openDevice(device)
        if not connection:
            raise SerialException("Failed to open device!")

        self._device = device
        self._connection = connection

        if self._device.getInterfaceCount() == 1:
            # Device might be castrated ACM device. Try single interface logic.
            self._open_single_interface()
        else:
            # Try default interface logic.
            self._open_interface()

        # Check that all endpoints are good
        if None in [
                self._control_endpoint, self._write_endpoint,
                self._read_endpoint
        ]:
            raise SerialException("Could not establish all endpoints!")

        self.is_open = True
        self._set_dtr_rts(self._dtr_state, self._rts_state)
        self._reconfigure_port()
Ejemplo n.º 3
0
 def open(self):
     '''Open the serial port.
     
     When the serial port is instantiated, it will try to open automatically.
     '''
     self.close()
     
     device = usb.get_usb_device(self.portstr)
     if not device:
         raise SerialException("Device not present {}".format(self.portstr))
     
     if not usb.has_usb_permission(device):
         usb.request_usb_permission(device)
         return
         
     connection = usb.get_usb_manager().openDevice(device)
     if not connection:
         raise SerialException("Failed to open device!")
         
     self._device = device
     self._connection = connection
     
     for i in range(self._device.getInterfaceCount()):
         if not self._connection.claimInterface(
             self._device.getInterface(i),
             True
         ):
             raise SerialException("Could not claim interface {}.".format(i))
     
     self._interface = self._device.getInterface(
         self._device.getInterfaceCount() - 1
     )
     
     for i in range(self._interface.getEndpointCount()):
         ep = self._interface.getEndpoint(i)
         if (
             (ep.getDirection() == usb.UsbConstants.USB_DIR_IN) \
             and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_INT)
         ):
             self._control_endpoint = ep
         elif (
             (ep.getDirection() == usb.UsbConstants.USB_DIR_IN) \
             and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK)
         ):
             self._read_endpoint = ep
         elif (
             (ep.getDirection() == usb.UsbConstants.USB_DIR_OUT) \
             and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK)
         ):
             self._write_endpoint = ep  
             
     # Check that all endpoints are good
     if None in [self._write_endpoint, self._read_endpoint]:
         raise SerialException("Could not establish all endpoints!")
     
     # Enable UART
     self._ctrl_transfer_out(self.CP210X_IFC_ENABLE, self.UART_ENABLE)
     # Set Baud Div
     self._ctrl_transfer_out(
         self.CP210X_SET_BAUDDIV,
         self.BAUD_RATE_GEN_FREQ / self.DEFAULT_BAUDRATE
     )
     
     self.is_open = True
     
     # Set DTR and RTS
     self.dtr = True
     self.rts = True
     
     self._reconfigure_port()
Ejemplo n.º 4
0
    def open(self):
        '''Open the serial port.
        
        When the serial port is instantiated, it will try to open automatically.
        '''
        self.close()

        device = usb.get_usb_device(self.portstr)
        if not device:
            raise SerialException("Device not present {}".format(self.portstr))

        if not usb.has_usb_permission(device):
            usb.request_usb_permission(device)
            return

        connection = usb.get_usb_manager().openDevice(device)
        if not connection:
            raise SerialException("Failed to open device!")

        self._device = device
        self._connection = connection

        for i in range(self._device.getInterfaceCount()):
            if not self._connection.claimInterface(
                    self._device.getInterface(i), True):
                raise SerialException(
                    "Could not claim interface {}.".format(i))

        self._interface = self._device.getInterface(
            self._device.getInterfaceCount() - 1)

        for i in range(self._interface.getEndpointCount()):
            ep = self._interface.getEndpoint(i)
            if (
                (ep.getDirection() == usb.UsbConstants.USB_DIR_IN) \
                and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_INT)
            ):
                self._control_endpoint = ep
            elif (
                (ep.getDirection() == usb.UsbConstants.USB_DIR_IN) \
                and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK)
            ):
                self._read_endpoint = ep
            elif (
                (ep.getDirection() == usb.UsbConstants.USB_DIR_OUT) \
                and (ep.getType() == usb.UsbConstants.USB_ENDPOINT_XFER_BULK)
            ):
                self._write_endpoint = ep

        # Check that all endpoints are good
        if None in [
                self._control_endpoint, self._read_endpoint,
                self._write_endpoint
        ]:
            raise SerialException("Could not establish all endpoints!")

        if self._device.getDeviceClass() == usb.UsbConstants.USB_CLASS_COMM:
            self._device_type = self.DEVICE_TYPE_0
        else:
            raw_descriptors = self._connection.getRawDescriptors()
            max_packet_size_0 = raw_descriptors[7]
            if max_packet_size_0 == 64:
                self._device_type = self.DEVICE_TYPE_HX
            elif (
                self._device.getDeviceClass() == \
                usb.UsbConstants.USB_CLASS_PER_INTERFACE
                or self._device.getDeviceClass() == \
                usb.UsbConstants.USB_CLASS_VENDOR_SPEC
            ):
                self._device_type = self.DEVICE_TYPE_1
            else:
                # Unknown device sub type, assume DEVICE_TYPE_HX.
                self._device_type = self.DEVICE_TYPE_HX

        self._init_device()
        self.is_open = True
        self._set_dtr_rts(self._dtr_state, self._rts_state)
        self._reconfigure_port()