Cleaton Pais
Cleaton Pais

Reputation: 160

Send data to serial android application

I am attempting to send a string of data from an android app to Zigbee through serial.

The application will be similar to Serial USB Terminal app on the Play Store. this link is https://play.google.com/store/apps/details?id=de.kai_morich.serial_usb_terminal&hl=en_IN

This is the task comparison of the USB Serail application Vs My application

USB Serial Application

  1. able to detect USB and connect
  2. send a data in hex generated by XCTU application
  3. get a response of the data sent

My application

  1. able to detect USB and connect
  2. send a data in hex generated by XCTU application
  3. no response received

I am unable to send the hex data the code for the same is Code for getting usb device and connecting

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
if (availableDrivers.isEmpty()) {
    responseText.setText("No USB detected");
    return;
}

UsbSerialPort port = null;

responseText.append("Device Count:"+availableDrivers.size());

// Open a connection to the first available driver.
UsbSerialDriver driver = availableDrivers.get(0);
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
// add UsbManager.requestPermission(driver.getDevice(), ..) handling here
    return;
}

try {
    port = driver.getPorts().get(0); // Most devices have just one port (port 0)
    port.open(connection);
    port.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException ioe){
    showToast("IO Exception at open");
    try{
        port.close();
    } catch (IOException ioe2){
        showToast("IO Exception at open disconnection");
    }
}
// if device connected and open show write and read feature
if(port != null){
    responseText.append("\n" + "Port"+port.toString());
    showSerialConnected();
}

sending request to serial

public void writeToDevice(){
        serialInputOutputManager = new SerialInputOutputManager(port, mListener);
        Executors.newSingleThreadExecutor().submit(serialInputOutputManager);
        String data = requestEdit.getText().toString();
        try{
            port.write(data.getBytes(), 5000);
        } catch(IOException ioe) {
            responseText.append("\n IO Exception at write");
        }
    }

Data to be sent 7E 00 2B 10 01 00 13 A2 00 41 A7 A7 ED FF FE 00 00 24 7C 53 56 7C 31 31 7C 31 30 38 7C 31 33 30 33 32 30 31 30 30 33 30 36 7C 33 39 7C 23 2E

response listener

ExecutorService mExecutor;
SerialInputOutputManager serialInputOutputManager;
SerialInputOutputManager.Listener mListener;

mExecutor = Executors.newSingleThreadExecutor();
        mListener = new SerialInputOutputManager.Listener() {
            @Override
            public void onRunError(Exception e) {
                Log.d(TAG, "Runner stopped.");
            }

            @Override
            public void onNewData(final byte[] data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                         String message = " bytes: \n"+ HexDump.dumpHexString(data);
                         responseText.append(message);  
                    }
                });
            }
        };

Thanks in Advance

Upvotes: 2

Views: 4418

Answers (2)

kai-morich
kai-morich

Reputation: 437

to isolate the problem you could try to fork and adapt my open source variant https://github.com/kai-morich/SimpleUsbTerminal of USB Serial Application to figure out where your implementation differs

Upvotes: 2

tomlogic
tomlogic

Reputation: 11694

Try isolating the problem. To start, can you connect to a serial port on your PC (or some other device) instead of the XBee?

  • Confirm that you can receive data from the USB Serial Application and the Android device sees responses. This is your baseline -- the PC is just giving you a method to see what's working in the "black box" of your current problem (I send data but don't get a response).
  • Does the PC receive the same data from your application? If it looks garbled, maybe you're using the wrong baud rate in your app.
  • Does your app receive responses from the PC?
  • Maybe the XBee is configured for hardware flow control (CTS/RTS), but you're not setting that up correctly in your app, and it's causing the XBee to not send data.

Other tests with the current setup:

  • Create two "AT Command" packets in XCTU to toggle a digital output between high and low, perhaps one connected to an LED on your development board. That would give you confirmation that the XBee at least receives your data, and then you can troubleshoot why you don't see responses.
  • Disconnect the XBee module and use jumper wires to connect the Tx and Rx pins to each other (and possibly CTS and RTS pins if you have hardware handshaking). Again, test in USB Serial Application first, but every character you send should echo back. Test in your app. If this works, but a connection to the PC doesn't, you could be at the wrong baud rate.

Upvotes: 1

Related Questions