UsbSerial icon indicating copy to clipboard operation
UsbSerial copied to clipboard

How can i get the respond when i write the code already ?

Open JudeBautista opened this issue 8 years ago • 1 comments

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.

JudeBautista avatar Oct 01 '18 11:10 JudeBautista

Dont raise a Toast in onReceivedData because is not executed in the UI thread

felHR85 avatar Dec 31 '18 18:12 felHR85