如何在android中设置从19200到115200的波特率。

时间:2015-02-23 05:08:57

标签: android arduino usb communication

我正在创建一个由usb主机通信组成的android应用程序。在这个我使用19200波特率,但在这里我想要它115200.请告诉我如何设置波特率115200 这是我的活动:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textOut = (EditText)findViewById(R.id.textout);
        buttonSend = (Button)findViewById(R.id.send);

        buttonSend.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) 
            {
                textToSend = textOut.getText().toString();
                if(textToSend!="")
                {
                    stringToRx = "";

                    Thread threadsendArduinoText = new Thread(new Runnable()
                    {
                            @Override
                            public void run() 
                            {

                                sendArduinoText(textToSend);
                            }
                    });
                            threadsendArduinoText.start();
                }

            }});

        usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    }

    @Override
    public void onResume() {
        super.onResume();

        Intent intent = getIntent();
        String action = intent.getAction();

        UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) 
        {
            setDevice(device);
        } 
        else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) 
        {
            if (deviceFound != null && deviceFound.equals(device)) 
            {
                setDevice(null);
            }
        }
    }

    @SuppressWarnings("unused")
    private void setDevice(UsbDevice device) {
        usbInterfaceFound = null;
        endpointOut = null;
        endpointIn = null;

        for (int i = 0; i < device.getInterfaceCount(); i++) {          
            UsbInterface usbif = device.getInterface(i);

            UsbEndpoint tOut = null;
            UsbEndpoint tIn = null;

            int tEndpointCnt = usbif.getEndpointCount();
            if (tEndpointCnt >= 2) 
            {
                for (int j = 0; j < tEndpointCnt; j++) 
                {
                    if (usbif.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) 
                    {
                        if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT) 
                        {
                            tOut = usbif.getEndpoint(j);
                        } else if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN) 
                        {
                            tIn = usbif.getEndpoint(j);
                        }
                    }
                }

                if (tOut != null && tIn != null) 
                {
                    // This interface have both USB_DIR_OUT
                    // and USB_DIR_IN of USB_ENDPOINT_XFER_BULK
                    usbInterfaceFound = usbif;
                    endpointOut = tOut;
                    endpointIn = tIn;
                }
            }
        }

        if (usbInterfaceFound == null) 
        {
            return;
        }

        deviceFound = device;

        if (device != null)
        {
            UsbDeviceConnection connection = usbManager.openDevice(device);
            if (connection != null && 
                connection.claimInterface(usbInterfaceFound, true))
            {

                connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
                connection.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
                usbDeviceConnection = connection;
                Thread thread = new Thread(this);
                thread.start();

            } 
            else 
            {
                usbDeviceConnection = null;
            }
         }
    }

    private void sendArduinoCommand(int control)
    {
        synchronized (this) 
        {

            if (usbDeviceConnection != null)
            {
                byte[] message = new byte[2];
                usbDeviceConnection.bulkTransfer(endpointOut,message, message.length, 0);
                Log.d(TAG, "sendArduinoCommand: " + String.valueOf(control));
            }
        }
    }

    private void sendArduinoText(String s) 
    {
        synchronized (this) 
        {

            if (usbDeviceConnection != null) 
            {

                int length;
                out= textOut.getText().toString();
                byte[] b = new byte[1];
                b = out.getBytes();

                    usbDeviceConnection.controlTransfer(0x40, 0x03, 0x2580, 0, null, 0, 0); //baudrate 9600
                    usbDeviceConnection.bulkTransfer(endpointOut,b, b.length, 0);
                    try 
                    {
                        Thread.sleep(100);
                    } catch (InterruptedException e) 
                    {

                        e.printStackTrace();
                    }

            }
        }
    }

    @Override
    public void run() 
    {
        ByteBuffer buffer = ByteBuffer.allocate(1);
        UsbRequest request = new UsbRequest();
        request.initialize(usbDeviceConnection, endpointIn);
        while (true) 
        {
            request.queue(buffer, 1);
            if (usbDeviceConnection.requestWait() == request) 
            {
                byte dataRx = buffer.get(0);
            }
            } 

            }


    }

0 个答案:

没有答案