2016-03-30 22 views
1

'u kullanarak usb üzerinden veri alınamadı. Aşağıdaki UsbSerial örneğini kullanarak aşağıdaki https://github.com/felHR85/SerialPortExample bağlantısını kullanıyorum. Fotoğrafta gösterilen cihazdan usb üzerinden veri almak istiyorum. Aygıt, temel olarak, seri bağlantı noktası üzerinden sayaç verileri gönderen bir sayaç makinesidir. pic 2Usbserial örneği

pic 1

ondan veri akışını okumak için ondan cihaz ve açık port bağlanabiliyor ama kuramıyorum. Aşağıda kullanılan kod. Kod Ben biraz geç, ancak sadece benzer bir sorun rastlamak olabilir başkalarına yardım etmek, kendi sorununa çözüm bulabildin mi biliyorum herhangi hatayı

MainActivity sınıfını

public class MainActivity extends AppCompatActivity { 

    /* 
    * Notifications from UsbService will be received here. 
    */ 
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      switch (intent.getAction()) { 
       case UsbService.ACTION_USB_PERMISSION_GRANTED: // USB PERMISSION GRANTED 
        Toast.makeText(context, "USB Ready", Toast.LENGTH_SHORT).show(); 
        break; 
       case UsbService.ACTION_USB_PERMISSION_NOT_GRANTED: // USB PERMISSION NOT GRANTED 
        Toast.makeText(context, "USB Permission not granted", Toast.LENGTH_SHORT).show(); 
        break; 
       case UsbService.ACTION_NO_USB: // NO USB CONNECTED 
        Toast.makeText(context, "No USB connected", Toast.LENGTH_SHORT).show(); 
        break; 
       case UsbService.ACTION_USB_DISCONNECTED: // USB DISCONNECTED 
        Toast.makeText(context, "USB disconnected", Toast.LENGTH_SHORT).show(); 
        break; 
       case UsbService.ACTION_USB_NOT_SUPPORTED: // USB NOT SUPPORTED 
        Toast.makeText(context, "USB device not supported", Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    }; 
    private UsbService usbService; 
    private TextView display; 
    private EditText editText; 
    private MyHandler mHandler; 
    private final ServiceConnection usbConnection = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName arg0, IBinder arg1) { 
      usbService = ((UsbService.UsbBinder) arg1).getService(); 
      usbService.setHandler(mHandler); 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName arg0) { 
      usbService = null; 
     } 
    }; 

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

     mHandler = new MyHandler(this); 
     display = (TextView) findViewById(R.id.textView1); 
     editText = (EditText) findViewById(R.id.editText1); 
     Button sendButton = (Button) findViewById(R.id.buttonSend); 
     sendButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (!editText.getText().toString().equals("")) { 
        String data = editText.getText().toString(); 
        if (usbService != null) { // if UsbService was correctly binded, Send data 
         display.append(data); 
         usbService.write(data.getBytes()); 
        } 
       } 
      } 
     }); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     setFilters(); // Start listening notifications from UsbService 
     startService(UsbService.class, usbConnection, null); // Start UsbService(if it was not started before) and Bind it 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     unregisterReceiver(mUsbReceiver); 
     unbindService(usbConnection); 
    } 

    private void startService(Class<?> service, ServiceConnection serviceConnection, Bundle extras) { 
     if (!UsbService.SERVICE_CONNECTED) { 
      Intent startService = new Intent(this, service); 
      if (extras != null && !extras.isEmpty()) { 
       Set<String> keys = extras.keySet(); 
       for (String key : keys) { 
        String extra = extras.getString(key); 
        startService.putExtra(key, extra); 
       } 
      } 
      startService(startService); 
     } 
     Intent bindingIntent = new Intent(this, service); 
     bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE); 
    } 

    private void setFilters() { 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(UsbService.ACTION_USB_PERMISSION_GRANTED); 
     filter.addAction(UsbService.ACTION_NO_USB); 
     filter.addAction(UsbService.ACTION_USB_DISCONNECTED); 
     filter.addAction(UsbService.ACTION_USB_NOT_SUPPORTED); 
     filter.addAction(UsbService.ACTION_USB_PERMISSION_NOT_GRANTED); 
     registerReceiver(mUsbReceiver, filter); 
    } 

    /* 
    * This handler will be passed to UsbService. Data received from serial port is displayed through this handler 
    */ 
    private static class MyHandler extends Handler { 
     private final WeakReference<MainActivity> mActivity; 

     public MyHandler(MainActivity activity) { 
      mActivity = new WeakReference<>(activity); 
     } 

     @Override 
     public void handleMessage(Message msg) { 
      mActivity.get().display.append("Handle:"); 
      switch (msg.what) { 
       case UsbService.MESSAGE_FROM_SERIAL_PORT: 
        String data = (String) msg.obj; 
        mActivity.get().display.append(data); 
        break; 
      } 
     } 
    } 
} 

cevap

0

vermiyor? Değilse, sizin tarafınızdan atıfta bulunulan örnekte açıklandığı gibi servisle (USBService.java) karşılık gelen diğer java dosyasını göremiyorum. Aynı dosya, neyin yanlış gittiğini bulmak için hata ayıklamak istediğiniz aşağıdaki kod snippet'ini içerir (dizgenin string dönüştürmesiyle ilgili bir sorun olabilir). Bu yardımcı olur umarım.

/* 
* Data received from serial port will be received here. Just populate onReceivedData with your code 
* In this particular example. byte stream is converted to String and send to UI thread to 
* be treated there. 
*/ 
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() 
{ 
    @Override 
    public void onReceivedData(byte[] arg0) 
    { 
     try 
     { 
      String data = new String(arg0, "UTF-8"); 
      if(mHandler != null) 
       mHandler.obtainMessage(MESSAGE_FROM_SERIAL_PORT,data).sendToTarget(); 
     } catch (UnsupportedEncodingException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
};