0

Application receives data on USB port via CP2102 uart to USB converter on button pressed. I can open usb device, read PID, but when I insert code snippet where serial port is opened application crashes. Does anyone have idea what is the problem? ( CP2102 library used)

public class MainActivity extends AppCompatActivity {

private ToggleButton toggle;
private TextView textView;
private Toolbar toolbar;
private UsbSerialDevice serialPort;
private UsbManager usbManager;
private UsbDevice device;
private UsbDeviceConnection connection;


private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback()
{
    @Override
    public void onReceivedData(byte[] arg0)
    {
        // Code here

    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    textView =(TextView) findViewById(R.id.textView);
    toggle = (ToggleButton) findViewById(R.id.toggleButton);
    setSupportActionBar(toolbar);


    usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    device = null;
    connection = null;
    HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();

    if(!usbDevices.isEmpty())
    {
        boolean keep = true;
        for(Map.Entry<String, UsbDevice> entry : usbDevices.entrySet())
        {
            device = entry.getValue();
            int deviceVID = 0;
            deviceVID = device.getVendorId();
            int devicePID = device.getProductId();
            if(deviceVID != 0x1d6b || (devicePID != 0x0001 || devicePID != 0x0002 || devicePID != 0x0003))
            {
                // We are supposing here there is only one device connected and it is our serial device
                connection = usbManager.openDevice(device);
                textView.append("\nConnected USB Device ID:");
                textView.append(Integer.toString(deviceVID));

                serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);

                keep = false;
            }else
            {
                connection = null;
                device = null;
            }

            if(!keep)
                break;
        }
    }






    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {
                textView.append("\nLogging Started!\n");

                if(serialPort != null)
                {
                    if(serialPort.open())
                    {
                        // Devices are opened with default values, Usually 9600,8,1,None,OFF
                        // CDC driver default values 115200,8,1,None,OFF

                        textView.setText("Serial port opened");
                        serialPort.setBaudRate(115200);
                        serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8);
                        serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1);
                        serialPort.setParity(UsbSerialInterface.PARITY_NONE);
                        serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
                        serialPort.read(mCallback);
                    }else
                    {
                        // Serial port could not be opened, maybe an I/O error or it CDC driver was chosen it does not really fit
                        textView.setText("Could not open port");
                    }
                }else
                {
                    // No driver for given device, even generic CDC driver could not be loaded
                    textView.setText("No driver");
                }

            } else {

                textView.append("Logging Stopped!");
            }
        }
    });

}

@Override
protected void onDestroy() {
    super.onDestroy();

}
Nikson
  • 67
  • 7
  • What does the Android Monitor in Android Studio? Which Android version do you use? Some more info would help. – andred Sep 07 '16 at 00:09
  • I can't see what does the Android Monitor, because I have to disconnect the usb cable and connect the CP2102. I use 6.0.1 Android vesrion – Nikson Sep 07 '16 at 05:15
  • As far as your device and your development machine are in the same network, you can connect adb via network. This is described here http://stackoverflow.com/q/2604727/589216 – andred Sep 07 '16 at 07:05

1 Answers1

0

The problem was in not getting the permission (Android 6.0), but it was a little bit misleading because it was possible to read product and vendor ID of CP2102 without obtaining permission.

Nikson
  • 67
  • 7