UsbSerial
UsbSerial copied to clipboard
How can i get the respond when i write the code already ?
Hello,
I am very happy to see your library, and working to me, but i have the problem. I already connected to the terminal, and i can send a data, and its working, but the problem there is a response from the terminal, but i can't catch it, is there any missing to my code or wrong?
here is my code
public void openDevice()
{
connection = usbManager.openDevice(printer);
debug.append("printer: "+printer +"\n");
debug.append("connection: "+connection+"\n");
serialDevice = UsbSerialDevice.createUsbSerialDevice(printer, connection);
debug.append("serialDevice: "+serialDevice.toString()+"\n");
if (serialDevice != null) {
if (serialDevice.open()) {
serialDevice.setBaudRate(9600);
serialDevice.setDataBits(UsbSerialInterface.DATA_BITS_8);
serialDevice.setStopBits(UsbSerialInterface.STOP_BITS_1);
serialDevice.setParity(UsbSerialInterface.PARITY_NONE);
serialDevice.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
serialDevice.setRTS(true);
// adding this
serialDevice.read(mCallback);
} else {
debug.append("Printer port not open \n");
}
} else {
debug.append("Serial is null \n");
}
}
public void sendData(String data)
{
debug.append("sendData \n"+data.getBytes());
serialDevice.write(data.getBytes());
// adding this
serialDevice.read(mCallback);
}
// This is not working Why ?
String respond;
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] arg0)
{
Toast.makeText(getApplication(),"RESPONSE TERMINAL: "+arg0,Toast.LENGTH_LONG ).show();
}
};
please help me.
Dont raise a Toast in onReceivedData because is not executed in the UI thread