Reputation: 160
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
My application
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
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
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?
Other tests with the current setup:
Upvotes: 1