felHR85 / UsbSerial

Usb serial controller for Android
MIT License
1.81k stars 590 forks source link

How to serial read until char? #213

Closed vinzzz34 closed 5 years ago

vinzzz34 commented 5 years ago

Hi all, i need help to read commands from my arduino, How to do a serial read until \n with this library, I have tested protocole buffer withour sucess

 private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
        @Override
        public void onReceivedData(byte[] arg0) {
            ProtocolBuffer buffer = new ProtocolBuffer(ProtocolBuffer.TEXT); //Also Binary
            buffer.setDelimiter("\r\n");
            buffer.appendData(arg0);
            while(buffer.hasMoreCommands()){
                String data = buffer.nextTextCommand();
                // Do your thing with textCommand
                try {
                    data = new String(arg0, "UTF-8");
                    if (mHandler != null)
                        mHandler.obtainMessage(MESSAGE_FROM_SERIAL_PORT, data).sendToTarget();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

        }
    };

Please help me

felHR85 commented 5 years ago

@vinzzz34 Sorry. There is a bug in the documentation. ProtocolBuffer should be created outside onReceivedData

vinzzz34 commented 5 years ago

@vinzzz34 Sorry. There is a bug in the documentation. ProtocolBuffer should be created outside onReceivedData

Thank you! Work great!