Quantcast
Channel: 質問!ITmedia 新着質問(Java/253)
Viewing all articles
Browse latest Browse all 2439

javaでUSBのbulk転送

$
0
0
下記のコードでUSBのbulk転送のテストをしています。 Q1)質問箇所にありますThread.sleep(1)の街時間を小さくしますと、 writeBulkが早く完了しますが、これは当然でしょうか? Q2)close()をforループの外に出しますと、cmpエラーとなりますが、この回避の 方法はありますか? ======= import ch.ntb.usb.Device; import ch.ntb.usb.USB; import ch.ntb.usb.USBException; public class UsbMain { public static void main(String[] args){ UsbMain usbMain = new UsbMain(); usbMain.firstLight(); } public UsbMain(){} public void firstLight(){ // ベンダID: 0xFFFF // プロダクトID: 0x0009 Device dev; dev = USB.getDevice((short) 0xffff, (short) 0x0009); if(dev == null){ System.out.println("failed to open USB device!"); System.exit(1); } try{ dev.open(1, 0, -1); } catch (USBException e) { e.printStackTrace(); } for(int i=0; i<10; i++){ // Device dev = USB.getDevice((short) 0xffff, (short) 0x0009); // if(dev == null){ // System.out.println("failed to open USB device!"); // System.exit(1); // } // try { // dev.open(1, 0, -1); //RED light byte[] red = {1, 2, 3, 4, 5,}; //System.out.println("result: " + dev.writeInterrupt(0x04, red, red.length, 100, true)); try { dev.writeBulk(0x01, red, 5, 100, true); try { Thread.sleep(1); //質問箇所 } catch (InterruptedException e) { e.printStackTrace(); } byte dt[]=new byte[20]; //input bufferのクリヤー for(int j=0; j<20; j++) dt[j]=0; dev.readBulk(0x81, dt, 5, 500, true); System.out.println("i=" + i +" "+dt[0]+" "+dt[1]+" "+dt[2]+" "+dt[3]+" "+dt[4]+" "); if(i==9){ dev.close(); //close()は、for文の内側に配置出来ない? System.out.println("closed: " ); } } //dev.writeBulk(0x01, red, 5, 100, true); catch (USBException e) { //dev.writeBulk(0x01, red, 5, 100, true); e.printStackTrace(); } //dev.writeBulk(0x01, red, 5, 100, true); } //for //dev.close(); //この位置では、cmpエラー } //public void firstLight(){ } //public class UsbMain { =====

Viewing all articles
Browse latest Browse all 2439

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>