Code example for UsbDeviceConnection

Methods: close, controlTransfer, releaseInterface

0
		if (mConnection != null) {
			// drop DTR/RTS 
			setRTS(false); 
			setDTR(false); 
			 
			mConnection.releaseInterface(intf);
			mConnection.close();
			mConnection = null;
			mDevice = null;
			ep0 = null;
			ep1 = null;
			ep2 = null;
			Log.d(TAG, "Device closed");
		} 
	} 
 
	/** 
	 * Are we connected to a pl2303 converter? 
	 * This is only the USB connection, not the serial connection. 
	 *  
	 * @return true on connection 
	 */ 
	public boolean isConnected() { 
		if (mConnection != null) return true;
		else return false; 
	} 
 
	/** 
	 * Setup basic communication parameters according to linux pl2303.c driver  
	 * @param Enum BaudRate  
	 * @param Enum DataBits 
	 * @param Enum StopBits 
	 * @param Enum Parity 
	 * @param Enum FlowControl 
	 * @throws IOException if settings not supported or connection is closed 
	 */ 
	public void setup(BaudRate R, DataBits D, StopBits S, Parity P, FlowControl F) throws IOException {
		 
		if (mConnection == null) throw new IOException("Connection closed");
 
		// Get current settings 
		mConnection.controlTransfer(GET_LINE_REQUEST_TYPE, GET_LINE_REQUEST, 0, 0, PortSetting, 7, 100);
		//mConnection.controlTransfer(VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1, null, 0, 100); 
		Log.d(TAG, "Current serial configuration:" + PortSetting[0] + "," + PortSetting[1] + "," + PortSetting[2] + "," + PortSetting[3] + "," + PortSetting[4] + "," + PortSetting[5] + "," + PortSetting[6]);
 
		// Setup Baudrate 
		int baud;
		switch (R) {
		case B0: baud = 0; break;
		case B75: baud = 75; break;
		case B150: baud = 150; break;
		case B300: baud = 300; break;
		case B600: baud = 600; break;
		case B1200: baud = 1200; break;
		case B1800: baud = 1800; break;
		case B2400: baud = 2400; break;
		case B4800: baud = 4800; break;
		case B9600: baud = 9600; break;
		case B19200: baud = 19200; break;
		case B38400: baud = 38400; break;
		case B57600: baud = 57600; break;
		case B115200: baud = 115200; break;
		case B230400: baud = 230400; break;
		case B460800: baud = 460800; break;
		case B614400: baud = 614400; break;
		case B921600: baud = 921600; break;
		case B1228800: baud = 1228800; break;
		// The following rates only for HX-Type of PL2303 
		case B2457600: baud = 2457600; break;
		case B3000000: baud = 3000000; break;
		case B6000000: baud = 6000000; break;
		default: throw new IOException("Baudrate not supported");
		} 
 
		if  ((baud > 1228800) && (PL2303type == 0)) throw new IOException("Baudrate not supported"); // Only PL2303HX supports the higher baudrates   
 
		PortSetting[0]=(byte) (baud & 0xff);
		PortSetting[1]=(byte) ((baud >> 8) & 0xff);
		PortSetting[2]=(byte) ((baud >> 16) & 0xff);
		PortSetting[3]=(byte) ((baud >> 24) & 0xff);
 
		// Setup Stopbits 
		switch (S) {
		case S1: PortSetting[4] = 0; break;
		case S2: PortSetting[4] = 2; break;
		default: throw new IOException("Stopbit setting not supported"); 
		} 
 
		// Setup Parity 
		switch (P) {
		case NONE: PortSetting[5] = 0; break;
		case ODD: PortSetting[5] = 1; break;
		case EVEN: PortSetting[5] = 2; break;
		default: throw new IOException("Parity setting not supported"); 
		} 
 
		// Setup Databits 
		switch (D) {
		case D5: PortSetting[6] = 5; break;
		case D6: PortSetting[6] = 6; break;
		case D7: PortSetting[6] = 7; break;
		case D8: PortSetting[6] = 8; break;
		default: throw new IOException("Databit setting not supported");
		} 
 
		// Set new configuration on PL2303 
		mConnection.controlTransfer(SET_LINE_REQUEST_TYPE, SET_LINE_REQUEST, 0, 0, PortSetting, 7, 100); 
		Log.d(TAG, "New serial configuration:" + PortSetting[0] + "," + PortSetting[1] + "," + PortSetting[2] + "," + PortSetting[3] + "," + PortSetting[4] + "," + PortSetting[5] + "," + PortSetting[6]);
		 
		// Disable BreakControl 
		mConnection.controlTransfer(BREAK_REQUEST_TYPE, BREAK_REQUEST, BREAK_OFF, 0, null, 0, 100);
 
		// Enable/Disable FlowControl 
		switch (F) {
		case OFF:
			mConnection.controlTransfer(VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0, 0x0, null, 0, 100);
			setRTS(false); 
			setDTR(false); 
			Flow = F;
			Log.d(TAG, "FlowControl disabled");
			break; 
			 
		case RTSCTS: 
			if (PL2303type == 1) mConnection.controlTransfer(VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0, 0x61, null, 0, 100);
			else mConnection.controlTransfer(VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0, 0x41, null, 0, 100);
			setDTR(true); 
			setRTS(true); 
			Flow = F;
			Log.d(TAG, "RTS/CTS FlowControl enabled");
			break; 
		 
		case RFRCTS: break;
		case DTRDSR: break;
		case XONXOFF: break;
		} 
		 
	} 
 
	/** 
	 * Switch DTR on or off 
	 * @param state 
	 */ 
	public void setDTR(boolean state) {
		if ((state) && !((ControlLines & CONTROL_DTR)==CONTROL_DTR)) ControlLines = ControlLines + CONTROL_DTR;
		if (!(state) && ((ControlLines & CONTROL_DTR)==CONTROL_DTR)) ControlLines = ControlLines - CONTROL_DTR;
		mConnection.controlTransfer(SET_CONTROL_REQUEST_TYPE, SET_CONTROL_REQUEST, ControlLines , 0, null, 0, 100);
		Log.d(TAG, "DTR set to " + state);
	} 
	 
	/** 
	 * Switch RTS on or off 
	 * @param state 
	 */ 
	public void setRTS(boolean state) {
		if ((state) && !((ControlLines & CONTROL_RTS)==CONTROL_RTS)) ControlLines = ControlLines + CONTROL_RTS;
		if (!(state) && ((ControlLines & CONTROL_RTS)==CONTROL_RTS)) ControlLines = ControlLines - CONTROL_RTS;
		mConnection.controlTransfer(SET_CONTROL_REQUEST_TYPE, SET_CONTROL_REQUEST, ControlLines , 0, null, 0, 100);
		Log.d(TAG, "RTS set to " + state);
	} 
	 
	public int queueRead(byte[] buffer, int offset, int length){
		int PacketSize = ep2.getMaxPacketSize();