• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Receive data from USB

cosmoff

Lurker
Hello everybody,

I implemented a code which allow to me to receive data from the USB. But the problem is I do not receive all the data, I am losing many data and I do not know why. from a microcontroller I am sending the number 1,2,3 until 100 on my smarthphone thanks to the link USB and I read for example 2,4,6,7,11,15...So I lost many data but I do not know why.

I precise that I send of the microcontroller a data every 1 second

this is my code (in kotlin) :
Code:
val t2_switchOnOff = thread(start = false, priority = 5) // //priorité du thread min
{
    while(true){//true
        Thread.sleep(700)
        device = intent?.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE) // j'inspecte le port usb
        if(device != null)  {
            vid = Integer.toHexString(device!!.getVendorId()).toUpperCase()
            pid = Integer.toHexString(device!!.getProductId()).toUpperCase()

            if(!mUsbManager!!.hasPermission(device)){//si on a pas encore la permission

                mPermissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION), 0);
                mUsbManager!!.requestPermission(device, mPermissionIntent);// lance également la méthode onReceive de l'objet mUsbmanager

            }
            else//si on a déja la permission
            {
                var data = bulkReadData(100)

                if(data != null){
                    //sample = data[0].toPositiveInt().shl(8).or(data[1].toPositiveInt())
                    sample = data[0].toInt().shl(8).or(data[1].toInt())
                    tabValue.add(sample)
                    this@MainActivity.runOnUiThread(java.lang.Runnable {
                    tv_info!!.setText("device USB detected : ${device!!.deviceName} \n& Authorization accorded\n& vid = ${vid}\n& pid = ${pid}\n& retrieving data...\n& data : ${sample} ")
                    })
                }
            }
        }
    }
}

and my function bulkReadData is :
Code:
fun bulkReadData(timeout: Int): ByteArray? {
        if((vid == "1FC9") && (pid == "2042")){

            intf = device!!.getInterface(0)
            endpoint = intf!!.getEndpoint(0)

            try {
                val connection = mUsbManager!!.openDevice(device)
                val data = ByteArray(8)//8
                if (connection != null) {
                    if (connection.claimInterface(intf, true)) {
                        val result = connection.bulkTransfer(endpoint, data, data.size, timeout)//data.size
                        connection.close()
                        if(result > -1) {
                            return data
                        } else {
                            return null
                        }
                    } else {
                        return null
                    }
                } else {
                    return null
                }
            } catch (ex: Exception) {
                ex.printStackTrace()
                return null
            }//end try
        }//end if
        else return null
    }

I hope you could help me.
Thanks for you help
 
Last edited:
Back
Top Bottom