2012-01-22 21 views
16

मैं एक ऐसे प्रोजेक्ट पर काम कर रहा हूं जो एंड्रॉइड 3.2 में यूएसबी होस्ट क्षमताओं का उपयोग करता है। मैं आम तौर पर यूएसबी/सीरियल संचार के संबंध में ज्ञान और प्रतिभा की अपमानजनक कमी से पीड़ित हूं। मुझे जो भी करने की ज़रूरत है उसके लिए मैं कोई अच्छा उदाहरण कोड नहीं ढूंढ पा रहा हूं।एंड्रॉइड यूएसबी होस्ट संचार

मुझे यूएसबी संचार डिवाइस से पढ़ने की आवश्यकता है।
पूर्व: जब मैं (मेरे पीसी पर) पोटीन के माध्यम से कनेक्ट मैं दर्ज करें:

>GO 

और डिवाइस मेरे लिए डेटा उगल शुरू होता है। पिच/रोल/अस्थायी/Checksum।

पूर्व:

$R1.217P-0.986T26.3*60 
$R1.217P-0.986T26.3*60 
$R1.217P-0.987T26.3*61 
$R1.217P-0.986T26.3*60 
$R1.217P-0.985T26.3*63 

मैं एंड्रॉयड डिवाइस जो उस समय मैं 'गो' की एक गूंज प्राप्त से प्रारंभिक 'गो' आदेश भेज सकते हैं।

तब किसी भी बाद के पढ़ने पर कुछ भी नहीं।

मैं कैसे कर सकता हूं: 1) 'go' कमांड भेजें। 2) परिणामस्वरूप डेटा की धारा में पढ़ें।

यूएसबी डिवाइस जिसके साथ मैं काम कर रहा हूं, में निम्न इंटरफेस (एंडपॉइंट्स) हैं।

उपकरण वर्ग: संचार उपकरण (0x2)

इंटरफेस:

इंटरफ़ेस # 0 कक्षा: संचार उपकरण (0x2) Endpoint # 0 दिशा: इनबाउंड (0x80) प्रकार: Intrrupt (0x3) पोल अंतराल: 255 अधिकतम पैकेट आकार: 32 गुण: 000000011

इंटरफ़ेस # 1 कक्षा: संचार डी उपाध्यक्ष कक्षा (सीडीसी) (0xA) Endpoint # 0 पता: 129 संख्या: 1 दिशा: इनबाउंड (0x80) प्रकार: थोक (0x2) पोल अंतराल (0) अधिकतम पैकेट आकार: 32 गुण: 000,000,010

Endpoint # 1 पता: 2 संख्या: 2 दिशा: जावक (0x0) प्रकार: थोक (0x2) पोल अंतराल (0) अधिकतम पैकेट आकार: 32 गुण: 000,000,010

मैं अनुमति से निपटने, डिवाइस से कनेक्ट करने, सही इंटरफ़ेस ढूंढने और एंडपॉइंट्स असाइन करने में सक्षम हूं। मुझे बस यह समझने में परेशानी हो रही है कि आरंभिक आदेश भेजने के लिए कौन सी तकनीक का उपयोग करना है, आने वाले डेटा को पढ़ें। मैं थोक के विभिन्न संयोजनों की कोशिश कर रहा हूँ ट्रांसफर और नियंत्रण किसी भी भाग्य के साथ ट्रांसफर।

धन्यवाद।

मैं # 1 इंटरफ़ेस का उपयोग कर रहा हूँ के रूप में नीचे देखी गई:

public AcmDevice(UsbDeviceConnection usbDeviceConnection, UsbInterface usbInterface) { 
    Preconditions.checkState(usbDeviceConnection.claimInterface(usbInterface, true)); 
    this.usbDeviceConnection = usbDeviceConnection; 

    UsbEndpoint epOut = null; 
    UsbEndpoint epIn = null; 
    // look for our bulk endpoints 
    for (int i = 0; i < usbInterface.getEndpointCount(); i++) { 
     UsbEndpoint ep = usbInterface.getEndpoint(i); 
    Log.d(TAG, "EP " + i + ": " + ep.getType()); 
     if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { 
     if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { 
      epOut = ep; 

     } else if (ep.getDirection() == UsbConstants.USB_DIR_IN) { 
      epIn = ep; 
     } 

     } 
    } 
    if (epOut == null || epIn == null) { 
     throw new IllegalArgumentException("Not all endpoints found."); 
    } 

    AcmReader acmReader = new AcmReader(usbDeviceConnection, epIn); 
    AcmWriter acmWriter = new AcmWriter(usbDeviceConnection, epOut); 
    reader = new BufferedReader(acmReader); 
    writer = new BufferedWriter(acmWriter); 
    } 
+0

, तुम कैसे इंटरफ़ेस 1, और क्या प्रयोजन 0 इंटरफ़ेस होता है, बाधा इंटरफेस, के लिए इस्तेमाल किया जा चुना? धन्यवाद! – Rachael

उत्तर

12

मैं अपने ही सवाल का जवाब देने से नफरत है लेकिन ... मैं यह पता लगा मिला है। मैं बस अपने पठन और लिख रहा था।इसके अतिरिक्त डिवाइस को '\ n' पसंद नहीं आया जो मैं अपने आदेशों के अंत में उपयोग कर रहा था। ऐसा लगता है कि '\ r' बहुत बेहतर है।

मैं एंड्रॉइड के थोक ट्रांसफर और लिखने के लिए ट्रांसफर का उपयोग कर समाप्त हुआ। मेरे लेखन इस तरह दिखते थे। समान थे

@Override

public void write(char[] buf, int offset, int count) throws IOException { 
    byte[] buffer = new String(buf, offset, count).getBytes(Charset.forName("US-ASCII")); 
    int byteCount = connection.bulkTransfer(endpoint, buffer, buffer.length, TIMEOUT); 
    } 

पढ़ता है::

try { 
      device.getWriter().write(command + "\r"); 
      device.getWriter().flush(); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 

और मेरे BufferedWriter के लिए मेरे ओवरराइड लिखने विधि

char[] buffer = new char[BUF_SIZE]; 
    try { 
     BufferedReader reader = device.getReader(); 

     int readBytes = reader.read(buffer); 
     Log.d(TAG, "BYTES READ: " + readBytes); 

    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 
    String strBuf = new String(buffer).trim(); 
    if (DEBUG) { 
     Log.d(TAG, "Read: " + strBuf); 
    } 

और:

@Override 
    public int read(char[] buf, int offset, int count) throws IOException { 
    byte[] buffer = new byte[count]; 
    int byteCount = connection.bulkTransfer(endpoint, buffer, buffer.length, TIMEOUT); 

    if (byteCount < 0) { 
     throw new IOException(); 
    } 
    char[] charBuffer = new String(buffer, Charset.forName("US-ASCII")).toCharArray(); 
    System.arraycopy(charBuffer, 0, buf, offset, byteCount); 
    return byteCount; 
    } 

यह सब सिर्फ दूर एक सूत्र में तो जैसे निकाल दिया गया था:

new Thread() { 
    @Override 
public void run() { 

    String command = "go"; 
    write(command); 

    while (true) { 
     String coords = read(); 
     } 
} 
}.start(); 

जाहिर है यह सिर्फ कॉम सामान है और मैं की आवश्यकता होगी करने के लिए अब यह (एक सेवा है कि वापस रिपोर्ट कर सकते हैं में रख के साथ कुछ करना एक हैंडलर का उपयोग कर एक शीर्ष स्तर यूआई गतिविधि के लिए)। लेकिन इसका यह हिस्सा पता लगाया गया है।

रोस्वावा (http://code.google.com/p/rosjava/) पर काम कर रहे लोगों के लिए बहुत बड़ा धन्यवाद ... उन्होंने कई महान परियोजनाएं एकत्र की हैं और उनका कोड बहुत उपयोगी था।

चीजों को स्पष्ट करने में सहायता के लिए मेरी डिवाइस कक्षा जोड़ना।

import com.google.common.base.Preconditions; 

import android.hardware.usb.UsbConstants; 
import android.hardware.usb.UsbDeviceConnection; 
import android.hardware.usb.UsbEndpoint; 
import android.hardware.usb.UsbInterface; 
import android.util.Log; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 

/* This class represents a USB device that supports the adb protocol. */ 
public class BKDevice { 

// private static final int TIMEOUT = 3000; 

private final UsbDeviceConnection usbDeviceConnection; 
private final BufferedReader reader; 
private final BufferedWriter writer; 
public static final String TAG = "AcmDevice"; 

public BKDevice(UsbDeviceConnection usbDeviceConnection, 
     UsbInterface usbInterface) { 
    Preconditions.checkState(usbDeviceConnection.claimInterface(
      usbInterface, true)); 
    this.usbDeviceConnection = usbDeviceConnection; 

    UsbEndpoint epOut = null; 
    UsbEndpoint epIn = null; 
    // look for our bulk endpoints 
    for (int i = 0; i < usbInterface.getEndpointCount(); i++) { 
     UsbEndpoint ep = usbInterface.getEndpoint(i); 
     Log.d(TAG, "EP " + i + ": " + ep.getType()); 

     if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { 
      if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { 
       epOut = ep; 

      } else if (ep.getDirection() == UsbConstants.USB_DIR_IN) { 
       epIn = ep; 

      } 
     } 
    } 
    if (epOut == null || epIn == null) { 
     throw new IllegalArgumentException("Not all endpoints found."); 
    } 

    BKReader acmReader = new BKReader(usbDeviceConnection, epIn); 
    BKWriter acmWriter = new BKWriter(usbDeviceConnection, epOut); 

    reader = new BufferedReader(acmReader); 
    writer = new BufferedWriter(acmWriter); 
} 

public BufferedReader getReader() { 
    return reader; 
} 

public BufferedWriter getWriter() { 
    return writer; 
} 
} 

जोड़ना BKReader कोड:

import android.hardware.usb.UsbDeviceConnection; 
import android.hardware.usb.UsbEndpoint; 
import android.util.Log; 

import java.io.IOException; 
import java.io.Reader; 
import java.nio.charset.Charset; 

public class BKReader extends Reader { 

    private static final int TIMEOUT = 1000; 

    private final UsbDeviceConnection connection; 
    private final UsbEndpoint endpoint; 

    public BKReader(UsbDeviceConnection connection, UsbEndpoint endpoint) { 
     this.connection = connection; 
     this.endpoint = endpoint; 
    } 

    @Override 
    public int read(char[] buf, int offset, int count) throws IOException { 
     byte[] buffer = new byte[count]; 
     int byteCount = connection.bulkTransfer(endpoint, buffer, buffer.length, TIMEOUT); 

     if (byteCount < 0) { 
      throw new IOException(); 
     } 
     char[] charBuffer = new String(buffer, Charset.forName("US-ASCII")).toCharArray(); 
     System.arraycopy(charBuffer, 0, buf, offset, byteCount); 
     return byteCount; 
    } 

    @Override 
    public void close() throws IOException { 
    } 

} 
हम में से उन लोगों के वहाँ संघर्ष के लिए
+0

आपको डिवाइस कैसे मिला .getwriter()? और फ्लश() विधि क्या करेगी? – Sathish

+0

मेरी डिवाइस क्लास में BufferedWriter शामिल था। getWriter उस वस्तु को वापस कर दिया। तो, फ्लश() बस धारा को फ्लश करता है। मदद करने के लिए, मैं उपरोक्त अपना क्लास कोड पेस्ट कर दूंगा। –

+0

बीके रीडर क्या है? आपके द्वारा लिखे गए वर्ग की तरह लगता है। क्या आप इसके लिए कुछ कोड भी दिखा सकते हैं? –

संबंधित मुद्दे