Adafruit FT232H突破可以用于Android智能手机吗?

时间:2017-11-13 09:03:36

标签: android gpio adafruit

正如标题所描述的那样,Adafruit FT232H Breakout可以用作传感器和Android智能手机等外围设备之间的GPIO接口吗?

1 个答案:

答案 0 :(得分:0)

是的。它需要FTDI在您的libs /目录中的d2xx.jar。然后,您可以使用FTDI提供的示例将其连接到设备。

这是我的MainActivity.java,我必须将其附加到Adafruit的FT232H:

    package com.XXXXX.ftdi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdi.j2xx.D2xxManager;
import com.ftdi.j2xx.FT_Device;

public class MainActivity extends AppCompatActivity {
    public static D2xxManager ftD2xx = null;
    FT_Device ftDevice = null;
    static Context DeviceInformationContext;
    int devCount=0;
    TextView textNumberDeviceValue;
    TextView textDeviceName;
    TextView textDeviceSerialNo;
    TextView textDeviceDescription;
    TextView textDeviceID;
    TextView textDeviceLocation;
    TextView textLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            ftD2xx = D2xxManager.getInstance(this);
        } catch (D2xxManager.D2xxException ex) {
            ex.printStackTrace();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SetupD2xxLibrary();

        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        filter.setPriority(500);

        textNumberDeviceValue = findViewById(R.id.numDev);
        textDeviceName = findViewById(R.id.devName);
        textDeviceSerialNo = findViewById(R.id.device_information_serialno);
        textDeviceDescription = findViewById(R.id.device_information_description);
        textDeviceID = findViewById(R.id.device_information_deviceid);
        textDeviceLocation = findViewById(R.id.device_information_devicelocation);
        textLibrary = findViewById(R.id.device_information_library);
        devCount = -1;
        ConnectFunction();
        try {
            GetDeviceInformation();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            String s = e.getMessage();
            e.printStackTrace();
        }
        this.registerReceiver(mUsbReceiver, filter);
    }
    private void SetupD2xxLibrary () {
        /*
        PackageManager pm = getPackageManager();

        for (ApplicationInfo app : pm.getInstalledApplications(0)) {
          Log.d("PackageList", "package: " + app.packageName + ", sourceDir: " + app.nativeLibraryDir);
          if (app.packageName.equals(R.string.app_name)) {
              System.load(app.nativeLibraryDir + "/libj2xx-utils.so");
              Log.i("ftd2xx-java","Get PATH of FTDI JIN Library");
              break;
          }
        }
        */
        // Specify a non-default VID and PID combination to match if required

        //if(!ftD2xx.setVIDPID(0x0403, 0xada1))
        if(!ftD2xx.setVIDPID(0x0403, 0x6014))
            Log.i("ftd2xx-java","setVIDPID Error");

    }

    /***********USB broadcast receiver*******************************************/
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String TAG = "FragL";
            String action = intent.getAction();
            if(UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
            {
                Log.i(TAG,"DETACHED...");
            }
        }
    };

    public void GetDeviceInformation() throws InterruptedException {

        //int devCount = 0;

       // devCount = ftD2xx.createDeviceInfoList(DeviceInformationContext);

        Log.i("FtdiModeControl",
                "Device number = " + Integer.toString(devCount));
        if (devCount > 0) {
            D2xxManager.FtDeviceInfoListNode[] deviceList = new D2xxManager.FtDeviceInfoListNode[devCount];
            ftD2xx.getDeviceInfoList(devCount, deviceList);

             deviceList[0] = ftD2xx.getDeviceInfoListDetail(0);

            textNumberDeviceValue.setText("Number of Devices: "
                    + Integer.toString(devCount));

            if (deviceList[0].serialNumber == null) {
                textDeviceSerialNo.setText("Device Serial Number: " + deviceList[0].serialNumber + "(No Serial Number)");
            } else {
                textDeviceSerialNo.setText("Device Serial Number: " + deviceList[0].serialNumber);
            }

            if (deviceList[0].description == null) {
                textDeviceDescription.setText("Device Description: " + deviceList[0].description+ "(No Description)");
            } else {
                textDeviceDescription.setText("Device Description: " + deviceList[0].description);
            }

            textDeviceLocation.setText("Device Location: " + String.format("%04x",deviceList[0].location));

            textDeviceID.setText("Device ID: " + String.format("%08x",deviceList[0].id));

            int libVersion = D2xxManager.getLibraryVersion();
            textLibrary.setText("Library Version: " + convertIntToBcdString(libVersion) );

            // display the chip type for the first device
            switch (deviceList[0].type) {
                case D2xxManager.FT_DEVICE_232B:
                    textDeviceName.setText("Device Name : FT232B device");
                    break;

                case D2xxManager.FT_DEVICE_8U232AM:
                    textDeviceName.setText("Device Name : FT8U232AM device");
                    break;

                case D2xxManager.FT_DEVICE_UNKNOWN:
                    textDeviceName.setText("Device Name : Unknown device");
                    break;

                case D2xxManager.FT_DEVICE_2232:
                    textDeviceName.setText("Device Name : FT2232 device");
                    break;

                case D2xxManager.FT_DEVICE_232R:
                    textDeviceName.setText("Device Name : FT232R device");
                    break;

                case D2xxManager.FT_DEVICE_2232H:
                    textDeviceName.setText("Device Name : FT2232H device");
                    break;

                case D2xxManager.FT_DEVICE_4232H:
                    textDeviceName.setText("Device Name : FT4232H device");
                    break;

                case D2xxManager.FT_DEVICE_232H:
                    textDeviceName.setText("Device Name : FT232H device");
                    break;

                case D2xxManager.FT_DEVICE_X_SERIES:
                    textDeviceName.setText("Device Name : FTDI X_SERIES");
                    break;

                case D2xxManager.FT_DEVICE_4222_0:
                case D2xxManager.FT_DEVICE_4222_1_2:
                case D2xxManager.FT_DEVICE_4222_3:
                    textDeviceName.setText("Device Name : FT4222 device");
                    break;

                default:
                    textDeviceName.setText("Device Name : FT232B device");
                    break;
            }
        } else {
            textNumberDeviceValue.setText("Number of devices: 0");
            textDeviceName.setText("Device Name : No device");
            textDeviceSerialNo.setText("Device Serial Number: NULL");
            textDeviceDescription.setText("Device Description: None");
            textDeviceLocation.setText("Device Location: Nowhere");
            textDeviceID.setText("Device ID: None");
            textLibrary.setText("Library Version: None");
        }
    }
    String convertIntToBcdString(int num)
    {
        String str = "";
        str = Integer.toString((num>>28) & 0xf) + "." +
                Integer.toString((num>>24) & 0xf) +
                Integer.toString((num>>20) & 0xf) + "." +
                Integer.toString((num>>16) & 0xf) +
                Integer.toString((num>>12) & 0xf) + "." +
                Integer.toString((num>>8) & 0xf) +
                Integer.toString((num>>4) & 0xf) +
                Integer.toString(num & 0xf);
        return str;
    }
    public void ConnectFunction() {
        int openIndex = 0;

        if (devCount > 0)
            return;

        devCount = ftD2xx.createDeviceInfoList(this);

        if (devCount > 0) {
            ftDevice = ftD2xx.openByIndex(this, openIndex);

            if(ftDevice == null)
            {
                Toast.makeText(this,"ftDev == null",Toast.LENGTH_LONG).show();
                return;
            }

            if (true == ftDevice.isOpen())
            {
                ftDevice.setBitMode((byte) 0, D2xxManager.FT_BITMODE_RESET);
                ftDevice.setBaudRate(9600);
                ftDevice.setDataCharacteristics(D2xxManager.FT_DATA_BITS_8,
                        D2xxManager.FT_STOP_BITS_1, D2xxManager.FT_PARITY_NONE);
                ftDevice.setFlowControl(D2xxManager.FT_FLOW_NONE, (byte) 0x00, (byte) 0x00);
                ftDevice.setLatencyTimer((byte) 16);
                ftDevice.purge((byte) (D2xxManager.FT_PURGE_TX | D2xxManager.FT_PURGE_RX));


            }
            else
            {
                Toast.makeText(this, "Need to get permission!",Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            Log.e("j2xx", "DevCount <= 0");
        }
    }
}