但当我连接我的Android平板电脑并通过physicaloID使用测试项目来打开设备并开始通信时,每次我点击“打开”它会显示一个Toast说它无法打开.我每次提示时都允许访问USB设备.这是我为读取数据而创建的示例程序:
if(mPhysicaloID.open()){ Toast.makeText(getBaseContext(),"communicating",Toast.LENGTH_SHORT).show(); String signalToStart = new String("@"); byte[] bufToWrite = signalToStart.getBytes(); mPhysicaloID.write(bufToWrite,bufToWrite.length); byte[] buf = new byte[255]; mPhysicaloID.read(buf); String data = new String(buf); tvResult.setText(data); mPhysicaloID.close(); } else Toast.makeText(getBaseContext(),"no communication with device",Toast.LENGTH_LONG).show();
现在这就是我想知道的来自Arduino USB线的数据:它是采用RS232格式的AndroID设备无法理解(我不知道,我可能会在这里通过询问这些数据犯错误格式)或者是否适合AndroID设备理解的USB数据格式?请帮忙,我整天搜索过这个.我该怎么做才能打开设备并进行通信?
解决方法 我终于明白了从串口USB设备读取数据的想法.所以我想我会分享它:首先,连接所有USB设备(如果不止一个)并获得合适的接口并搜索要与之通信的端点.在初始化USB设备时,请确保考虑您真正想要与之通信的USB设备.您可以通过考虑产品ID和供应商ID来做到这一点.
做上述代码..
private boolean searchEndPoint() { usbInterface = null;//class level variables,declare these. endpointOut = null; endpointIn = null; Log.d("USB","Searching device and endpoints..."); if (device == null) { usbDevices = usbManager.getDeviceList(); Iterator<UsbDevice> deviceIterator = usbDevices.values().iterator(); while (deviceIterator.hasNext()) { UsbDevice tempDevice = deviceIterator.next(); /**Search device for targetvendorID(class level variables[vendorID = SOME_NUMBER and productID=SOME_NUMBER] which u can find) and targetProductID.*/ if (tempDevice .getvendorID() == vendorID) { if (tempDevice .getProductID() == productID) { device = tempDevice ; } } } } if (device == null){ Log.d("USB","The device with specifIEd vendorID and ProductID not found"); return false; } else Log.d("USB","device found"); /**Search for UsbInterface with Endpoint of USB_ENDPOINT_XFER_BulK,*and direction USB_DIR_OUT and USB_DIR_IN */ try{ for (int i = 0; i < device.getInterfaceCount(); i++) { UsbInterface usbif = device.getInterface(i); UsbEndpoint tOut = null; UsbEndpoint tIn = null; int tEndpointCnt = usbif.getEndpointCount(); if (tEndpointCnt >= 2) { for (int j = 0; j < tEndpointCnt; j++) { if (usbif.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BulK) { if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT) { tOut = usbif.getEndpoint(j); } else if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN) { tIn = usbif.getEndpoint(j); } } } if (tOut != null && tIn != null) { /** This interface have both USB_DIR_OUT * And USB_DIR_IN of USB_ENDPOINT_XFER_BulK */ usbInterface = usbif; endpointOut = tOut; endpointIn = tIn; } } } if (usbInterface == null) { Log.d("USB","No suitable interface found!"); return false; } else { Log.d("USB","Suitable interface found!"); return true; } }catch(Exception ex){ ex.printstacktrace(); return false; }}
现在,您可以使用设备,USB接口和端点进行通信.现在是时候在AndroID设备和USB设备之间建立连接了.
下面是这个代码(并检查连接是否正常并进行通信):
private boolean checkUsbCOMM() { /**Value for setting request,on the USB connection.*/ final int RQSID_SET_CONTRol_liNE_STATE = 0x22; boolean success = false; Log.d("USB","Checking USB Device for communication: "); try{ Boolean permitToRead = SUSBS_usbManager.hasPermission(SUSBS_device); if (permitToRead) { //class level variable(connection,usbManager : declare it) connection = usbManager.openDevice(device); if (connection != null) { connection.claimInterface(usbInterface,true); int usbResult; usbResult = connection.controlTransfer(0x21,//requestType RQSID_SET_CONTRol_liNE_STATE,//SET_CONTRol_liNE_STATE(request) 0,//value 0,//index null,//buffer 0,//length 500); //timeout = 500ms Log.i("USB","controlTransfer(SET_CONTRol_liNE_STATE)[must be 0 or greater than 0]: "+usbResult); if(usbResult >= 0) success = true; else success = false; } } else { /**If permission is not there then ask for permission*/ usbManager.requestPermission(device,mPermissionIntent); Log.d("USB","Requesting Permission to access USB Device: "); } return success; }catch(Exception ex){ ex.printstacktrace(); return false; }}
瞧,USB设备现在能够进行通信.所以让我们使用一个单独的线程来阅读:
if(device!=null){Thread readerThread = new Thread(){ public voID run(){ int usbResult = -1000; int totalBytes = 0; StringBuffer sb = new StringBuffer(); String usbReadResult=null; byte[] bytesIn ; try { while(true){ /**Reading data until there is no more data to receive from USB device.*/ bytesIn = new byte[endpointIn.getMaxPacketSize()]; usbResult = connection.bulkTransfer(endpointIn,bytesIn,bytesIn.length,500); /**The data read during each bulk transfer is logged*/ Log.i("USB","data-length/read: "+usbResult); /**The USB result is negative when there is failure in reading or * when there is no more data to be read[That is : * The USB device stops transmitting data]*/ if(usbResult < 0){ Log.d("USB","Breaking out from while,usb result is -1"); break; } /**Total bytes read from the USB device*/ totalBytes = totalBytes+usbResult; Log.i("USB","TotalBytes read: "+totalBytes); for(byte b: bytesIn){ if(b == 0 ) break; else{ sb.append((char) b); } } } /**Converting byte data into characters*/ usbReadResult = new String(sb); Log.d("USB","The result: "+usbReadResult); //usbResult holds the data read. } catch (Exception ex) { ex.printstacktrace(); } } }; /**Starting thread to read data from USB.*/ SUSBS_readerThread.start(); SUSBS_readerThread.join(); }
要获得权限,请确保添加PendingIntent以及向清单添加权限.
AndroIDManifest:< uses-feature androID:name =“androID.harDWare.usb.host”/>
的PendingIntent:
private PendingIntent mPermissionIntent;private static final String ACTION_USB_PERMISSION = "com.androID.example.USB_PERMISSION";mPermissionIntent = PendingIntent.getbroadcast(MainActivity.this,new Intent(ACTION_USB_PERMISSION),0); /**Setting up the broadcast receiver to request a permission to allow the APP to access the USB device*/ IntentFilter filterPermission = new IntentFilter(ACTION_USB_PERMISSION); registerReceiver(mUsbReceiver,filterPermission);总结
以上是内存溢出为你收集整理的android – 从Arduino UNO R3套件中读取数据全部内容,希望文章能够帮你解决android – 从Arduino UNO R3套件中读取数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)