Manthan Thakar
Manthan Thakar

Reputation: 209

Establishing a serial communication between Android and ATMega16 microcontroller via PL2303 USB-Uart

Constituents of our current set up include:

1) A hardware device to read cards and send the unique code every time a card is processed. Consists an RFID Card Reader, ATMega16 Microcontroller and PL2303 USB-UART for serial communication with the Android device.

2) An Android App that receives the unique code (Sent by the microcontroller) every time a card is processed by the hardware device.

The code that we are using to receive the unique-code from the hardware :

The code uses Android USB Host API

    package com.example.admin.hardware;
    import android.content.Context;
    import android.content.Intent;
    import android.hardware.usb.UsbDevice;
    import android.hardware.usb.UsbDeviceConnection;
    import android.hardware.usb.UsbEndpoint;
    import android.hardware.usb.UsbInterface;
    import android.hardware.usb.UsbManager;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends ActionBarActivity {
    UsbManager manager;
    byte[] bytes;
    UsbEndpoint end;
    UsbInterface inter;
    UsbDevice device;
    UsbDeviceConnection conn;
    TextView data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    if(device == null)
    {
        Toast.makeText(this, "NO DEVICE CONNECTED!", Toast.LENGTH_LONG).show();
    }
    else
    {
        data = (TextView) findViewById(R.id.data);
        String name = device.getDeviceName();
        //int vendorID = device.getVendorId();

        inter = device.getInterface(0);
        int i = inter.getEndpointCount();
        end = inter.getEndpoint(0);

        Toast.makeText(this, "Name Of The "+name, Toast.LENGTH_LONG).show();

        //RETURNS 128 if USB_DIR_IN and 0 if USB_DIR_OUT
        int direction = end.getDirection();
        Toast.makeText(this, "Direction of The EndPoint "+String.valueOf(direction), Toast.LENGTH_LONG).show();
            manager = (UsbManager) getSystemService(Context.USB_SERVICE);
            conn = manager.openDevice(device);

            conn.claimInterface(inter, true);
            new Thread(new Runnable(){
                @Override
                public void run() {
                    try {
                        conn.bulkTransfer(end, bytes, 32, 0);
                    }
                    catch(Exception e){
                        data.setText(e.getMessage());
                    }
                }
            }).start();

    }


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
    }

What I expect to see:

The data received from the hardware device should be stored in the byte array.

What is actually happening:

The program gives "Buffer start or length out of bounds" error!

Any help will be greatly appreciated!

Upvotes: 0

Views: 1511

Answers (1)

D Krueger
D Krueger

Reputation: 2486

You never initialize the array bytes.

Use byte[] bytes = new byte[32];.

Then conn.bulkTransfer(end, bytes, bytes.length, 0); to be safe in case you need to change the length of the array.

Upvotes: 1

Related Questions