Arduino的Andr​​oid的USB连接 [英] Arduino Android USB connection

查看:211
本文介绍了Arduino的Andr​​oid的USB连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个Arduino <一href="http://en.wikipedia.org/wiki/List_of_Arduino_boards_and_compatible_systems#Superseded_versions"相对=nofollow> Duemilanove 和的Nexus 7 。我已经成功地检测到Arduino板和显示供应商ID和产品ID。我想从我的平板电脑传输数据到Arduino电路板,并试图闪烁href="http://en.wikipedia.org/wiki/Light-emitting_diode" rel="nofollow"> LED 的

I am using an Arduino Duemilanove and Nexus 7. I have successfully detected the Arduino board and displayed the vendor id and product id. I am trying to transfer data from my tablet to the Arduino board and trying to blink the LED on the board. The code for Android is as follows.

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

        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        UsbDeviceConnection connection;
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        UsbDevice device = null;
        while(deviceIterator.hasNext()){
            device = deviceIterator.next();
            String s = device.getDeviceName();
            int pid = device.getProductId();
            int did = device.getDeviceId();
            int vid = device.getVendorId();
            TextView tv = (TextView) findViewById(R.id.textview);
            tv.setText(s+"\n"+Integer.toString(pid)+"\n"+Integer.toString(did));
        }
        connection = manager.openDevice(device);
        DataTransfer dt = new DataTransfer(device,connection);
    }

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

DataTrasfer.java

public class DataTransfer extends Thread {
    UsbDevice device;
    UsbDeviceConnection connection;
    byte data = 1;
    public DataTransfer(UsbDevice device, UsbDeviceConnection connection) {
        // TODO: Auto-generated constructor stub
        device = this.device;
        connection = this.connection;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        UsbInterface usbIf = device.getInterface(0);
        UsbEndpoint end1 = usbIf.getEndpoint(0);

        //UsbRequest ureq = connection.requestWait();
        final Object[] sSendLock = new Object[]{};
        boolean mStop = false;
        byte mData = 0x00;

        if(!connection.claimInterface(usbIf, true))
        {
            return;
        }

        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);
        connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); //Baudrate 9600

        UsbEndpoint epIN = null;
        UsbEndpoint epOUT = null;

        for (int i = 0; i < usbIf.getEndpointCount(); i++) {
            if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
                    epIN = usbIf.getEndpoint(i);
                else
                    epOUT = usbIf.getEndpoint(i);
            }
        }

        for (;;) { // This is the main loop for transferring
            synchronized (sSendLock) { //OK, there should be a OUT queue, no guarantee that the byte is sent actually.
                try {
                    sSendLock.wait();
                }
                catch (InterruptedException e) {
                    if (mStop) {
                        return;
                    }
                    e.printStackTrace();
                }
                connection.bulkTransfer(epOUT,new byte[] {data}, 1, 0);
            }
        }
    }
}

和我的Arduino code是:

And my Arduino code is:

int incomingByte = 0; // For incoming serial data
void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600); // Opens serial port, sets data rate to 9600 bps.
}

void loop() {
    // Send data only when you receive data:
    if (Serial.available() > 0) {
        // Read the incoming byte:
        incomingByte = Serial.read();
        digitalWrite(13, HIGH);   // Set the LED on
        delay(1000);              // Wait for a second
        digitalWrite(13, LOW);    // Set the LED off

        // Say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte, DEC);
    }
}

现在的问题是,有没有LED发光的Arduino板。我不知道问题出在哪里,因为我刚开始在这方面的工作。

The problem is that there is no LED glowing on the Arduino board. I am not sure where the problem lies as I have just started working in this area.

推荐答案

您从引用该项目 HTTP: //android.serverbox.ch/?p=549 的意见将只与最近的Arduino板,如欧诺工作(或它使用CDC-ACM串行过USB方案别的东西,他们实现)。

The project you reference from http://android.serverbox.ch/?p=549 in comments will only work with the more recent Arduino boards such as the Uno (or with something else which uses the CDC-ACM serial-over-USB scheme which they implement).

旧的Arduino板,如您duemilanove使用FTDI USB转串口的芯片,这将需要不同的USB操作。

The older Arduino boards such as your duemilanove use an FTDI USB-serial chip, which would require different USB operations.

您也许能找到一些地方对接口的FT232系列的USB转串口转换器与Android手机的USB主机API(的FTDI零件广泛应用于嵌入式系统,因此它不是出了问题,你就会发现)或者你可以得到一个欧诺板试code你已经离开。

You may be able to find something somewhere about interfacing the FT232 family of USB-serial converters with Android's USB host api (the FTDI parts are widely used in embedded systems, so it's not out of the question you'll find something), or you can get an Uno board to try the code you already have.

这篇关于Arduino的Andr​​oid的USB连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆