4

私は、Android フォンと、USB 通信に FTDI チップ (FT232R) を使用するマイクロコントローラーとの間でデータを交換できるようにするプログラムに取り組んでいます。これまでのところ、アプリケーションの残りの部分に影響を与えずに USB 通信を実行できるスレッドを作成できました。さらに、初期化と FTDI デバイスへのデータ送信の両方に成功しましたが、デバイスからデータを読み取ることができないようです。100 ミリ秒ごとに 4 バイトのパケットを送信するように USB デバイスを設定して、パケットを読み取ってバスにエコー バックできるかどうかを確認しました。ただし、データを読み取ることができません。

デバイス側では、4 バイトの ASCII パケット ''a''b''c''d' を 100 ミリ秒ごとに送信しています。ホスト側、具体的には USB スレッドで、このデータを受信できるかどうかを確認しようとしています。受信バッファーの最初の位置が、送信されている文字 (a、b、c、または d) のいずれかと等しいかどうかを確認する while ループ内でデータを読み取っています。これを行った理由は、データがごちゃ混ぜになっている疑いがあったからです。while ループ内では、毎回バッファがクリアされます。次に、bulkTransfer 関数が呼び出されてデータの受信を試みます。正しく理解できれば、64 バイトを受信するか、1000 ミリ秒が経過するまで (タイムアウト) データを受信します。次に、バッファの最初の要素にある 4 文字のいずれかをチェックして、送信されたデータを受信したかどうかをチェックします。文字を検出すると、while ループが終了し、バッファから最初の 4 文字をバスに送信します。ただし、ロジック アナライザーでバスを監視していますが、ホストからの送信をまったく検出できず、データを受信して​​いないことがわかります。どんな助けでも大歓迎です。この時点に到達するために多くの作業を行いましたが、行き詰まっているようです。ありがとうございました。この時点に到達するために多くの作業を行いましたが、行き詰まっているようです。ありがとうございました。この時点に到達するために多くの作業を行いましたが、行き詰まっているようです。ありがとうございました。

USB スレーブ コード (C プログラム)

while(1)
    {
        __delay_ms(100);
        unsigned char response[4] = {0};
        response[0] = 'a';
        response[1] = 'b';
        response[2] = 'c';
        response[3] = 'd';
        send_UART(4, response);
    }

Android コード

    public class MainActivity extends Activity
    {
        // Developer Notifications
        private boolean developer_notifications = true;

        // Sensor Constants
        public static int temperature;
        public static int humidity;
        public static int lpg;
        public static int alcohol;

        // Layout
        ListView listView;

        // USB
        UsbDevice USBDevice_s;
        UsbDeviceConnection USBDeviceConnection_s;
        UsbEndpoint USBEndpoint_s_control;
        UsbEndpoint USBEndpoint_s_data;
        UsbEndpoint USBEndpoint_s_data_out = null;
        UsbEndpoint USBEndpoint_s_data_in = null;
        UsbInterface USBInterface_s_control;
        UsbInterface USBInterface_s_data;
        UsbManager USBManager_s;
        UsbRequest USBRequest_s;
        ByteBuffer USBBuffer_s;
        public static int USBBuffer_s_length;
        private static final byte[] USBBuffer_s_received_data = new byte[]{ (byte)0x00, 0x00, 0x00, 0x00};
        //USBBuffer_s_received_data = 0;

        // Communication Flags
        public static int Sense_Sensor_ID_Request = 0;
        public static int Sense_Sensor_ID_Response = 0;

        // Thread
        private Thread USB_Communication_Handler;
        public static int threadflag = 0;


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

            // Initialize Interface
            Model.LoadModel();
            listView = (ListView) findViewById(R.id.listView);
            String[] ids = new String[Model.Items.size()];
            for (int i= 0; i < ids.length; i++)
            {ids[i] = Integer.toString(i+1);}
            ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids);
            listView.setAdapter(adapter);


            // Developer Notifications
            developer_notifications = true;
            if ((developer_notifications))
            {Toast.makeText(this,"Developer Notifications Enabled", Toast.LENGTH_LONG).show();}

            // Begin USB Configuration | Grab Device List
            USBManager_s = (UsbManager) getSystemService(Context.USB_SERVICE);
            HashMap<String, UsbDevice> deviceList = USBManager_s.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
            while(deviceIterator.hasNext())
            {USBDevice_s = deviceIterator.next();}        
            Intent Intent_s = getIntent();
            String action = Intent_s.getAction();
            USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE);

            // Upon Device Attachment
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action))
            {
                initialize_USB();
                USB_Communication_Handler();

            }

            else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) 
            {
                USBDevice_s = (UsbDevice) Intent_s.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (USBDevice_s != null) 
                {
                    USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s);
                    //USBDeviceConnection_s.releaseInterface(USBInterface_s);
                    USBDeviceConnection_s.close();

                    Toast.makeText(this,"Device Removed, Interface Released", Toast.LENGTH_LONG).show();
                }
                return;
            }



            //Layout / GUI Created - wait for USB data...
            temperature = 1; humidity = 63; lpg = 5000; alcohol = 500;
            Model.LoadModel();
            ItemAdapter adapter2 = new ItemAdapter(this,R.layout.row, ids);
            listView.setAdapter(adapter2);
            adapter.notifyDataSetChanged();

        }   

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


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case R.id.action_settings:
            //Toast.makeText(this, "Exiting", Toast.LENGTH_SHORT)
               // .show();
            android.os.Process.killProcess(android.os.Process.myPid());
            finish();
            break;
          default:
            break;
          }

          return true;
        }

        //@Override
        public void initialize_USB() 
        {
            USBInterface_s_control = USBDevice_s.getInterface(0);      
            USBDeviceConnection_s = USBManager_s.openDevice(USBDevice_s);
            USBDeviceConnection_s.claimInterface(USBInterface_s_control,  true);
            if ((developer_notifications))
            {Toast.makeText(this,"Sense found ("+USBDevice_s.getDeviceName()+")", Toast.LENGTH_LONG).show();}

            // Send Initialization Packets   
            if (USBDeviceConnection_s != null && USBDeviceConnection_s.claimInterface(USBInterface_s_control, true))
            {
                // Reset, Clear RX/TX, & Set Baudrate
                USBDeviceConnection_s.controlTransfer(0x40, 0, 0, 0, null, 0, 0);
                USBDeviceConnection_s.controlTransfer(0x40, 0, 1, 0, null, 0, 0);
                USBDeviceConnection_s.controlTransfer(0x40, 0, 2, 0, null, 0, 0);
                USBDeviceConnection_s.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);

                if ((developer_notifications))
                {Toast.makeText(this,"Sense Enumerated & Initialized", Toast.LENGTH_LONG).show();}
            }

            // Grab Interface | Grab Endpoints | Configure for Data Exchange
            USBInterface_s_data = USBDevice_s.getInterface(0);
            for (int i = 0; i < USBInterface_s_data.getEndpointCount(); i++)
            {
                if (USBInterface_s_data.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) 
                {
                    if (USBInterface_s_data.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
                        USBEndpoint_s_data_in = USBInterface_s_data.getEndpoint(i);
                    else
                        USBEndpoint_s_data_out = USBInterface_s_data.getEndpoint(i);
                }
            }

            USBBuffer_s_length = USBEndpoint_s_data_in.getMaxPacketSize();
            USBBuffer_s = ByteBuffer.allocate(USBBuffer_s_length);

            if ((developer_notifications))
            {Toast.makeText(this,"Ready for Data Exchange", Toast.LENGTH_LONG).show();}
            //USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_out, new byte[] { 0x64 }, 1, 0);

        }

        public void send_data_USB() 
        {}
        public void receive_data_USB() 
        {}

        public void send_data_USB() 
        {}
        public void receive_data_USB() 
        {}

        public void USB_Communication_Handler()
        {
            USB_Communication_Handler = new Thread()
            {
                public void run()
                {
                    while(USBBuffer_s_received_data[0] != 'a' || USBBuffer_s_received_data[0] != 'b' ||USBBuffer_s_received_data[0] != 'c' ||USBBuffer_s_received_data[0] != 'd')
                    {
                        for(int i = 0 ; i < USBBuffer_s_received_data.length ; i++) 
                        {
                            USBBuffer_s_received_data[i] = '\0';
                        }
                        //USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_in, USBBuffer_s_received_data, 0, 8, 500);
                        //USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_out, USBBuffer_s_received_data, 0, 8, 500);
                        USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_in, USBBuffer_s_received_data, 64, 1000);
                    }
                    USBDeviceConnection_s.bulkTransfer(USBEndpoint_s_data_out, USBBuffer_s_received_data, 4, 500);
                    }

                }

            };
            USB_Communication_Handler.start();
        }

        }
4

0 に答える 0