首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过ATMega16 USB-Uart在Android与ATMega16单片机之间建立串行通信

通过ATMega16 USB-Uart在Android与ATMega16单片机之间建立串行通信
EN

Stack Overflow用户
提问于 2015-04-16 18:23:31
回答 1查看 1.4K关注 0票数 0

我们目前设置的组件包括:

1)一种每次处理卡时读取卡并发送唯一代码的硬件装置。该系统由射频识别读卡器、ATMega16单片机和PL2303 UART组成,用于与安卓设备进行串行通信.

2)当硬件设备处理卡时接收唯一代码(由微控制器发送)的Android应用程序。

用于接收来自硬件的唯一代码的代码:

代码使用Android USB主机API

代码语言:javascript
复制
    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);
}
    }

我希望看到的:

从硬件设备接收的数据应存储在字节数组中。

实际发生了什么:

该程序提供“缓冲区启动或长度超出界限”错误!

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2015-04-16 23:37:08

您从不初始化数组bytes

使用byte[] bytes = new byte[32];

如果您需要更改数组的长度,那么conn.bulkTransfer(end, bytes, bytes.length, 0);是安全的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29683005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档