0

I am using a terminal emulator library to create a terminal and then I use it to send the data entered over serial to a serial device. When the data is sent back I want to parse it and show the most important information to the user in an editText. Currently I receive byte arrays/chunks and I convert them to a string. When I get a \r or a \n I crete a new string and the process repeats. This is fine for most commands, however some commands return results over multiple lines like "show vlan" here:

enter image description here

When I loop through this I get a string for each line. The first would contain VLAN Name Status and Ports, as an example. So now I have a problem, how can I VLAN 1 has x ports active. They are in different strings. Here is the code and screenshot for a current easier command where I am interested in one line:

enter image description here

Handler viewHandler = new Handler();
Runnable updateView = new Runnable() {
    @Override

    public void run() {

        mEmulatorView.invalidate();

        if (statusBool == true) {
            for (int i = 0; i < dataReceived.length(); i++) {
                parseCommand = parseCommand + dataReceived.charAt(i);

 if (dataReceived.charAt(i) == '\n' || dataReceived.charAt(i) == '\r'){

    if(parseCommand.contains("KlasOS"))
            {

        String[] tokens = parseCommand.split("\\s{1,}");

        final String ReceivedText = mReceiveBox.getText().toString() + " "
        + new String("Software Version: " + tokens[1] + "\n" );

        runOnUiThread(new Runnable() {
            public void run() {
                                mReceiveBox.setText(ReceivedText);
                                mReceiveBox.setSelection(ReceivedText.length());

                            }
                        });         

                            }

                parseCommand = "";

            }               
            }

            statusBool = false;
            viewHandler.postDelayed(updateView, 1000);
        }
    }
};

Now I would like to change this so i can deal with multiple lines. Would the ebst way be to store strings if they contain certain information?

I need this outputted on the right hand editText:

"The following ports are on vlan 1: Fa1/0, fa1/1, fa1/2, fa1/3, fa1/4, fa1/5, fa1/6, fa1/7, fa1/8, fa1/9, fa1/10, fa1/11, Gi0"

2 Answers 2

2
+50

Basically, you need a way to reliably detect the end of a command result. Then it boils down to sending your command, reading data from the device until you encounter the end of result, and finally parsing that result.

I would scan for the prompt (switch#) as you do in your own answer. Maybe your are even able to force the device to use a more peculiar character sequence, which is unlikely to occur in the regular output of commands and makes it easier to detect the end of a result. For example, you could try to configure the prompt to include a control character like ^G or ^L. Or if your users don't mind, you could always send a second command that emits such a sequence, for example, "show vlan; echo ^G".

You should also be prepared for command errors, which result in a different output, for example, more or fewer lines as expected or a totally different output format. A result may even contain both, a regular output and a warning or an error.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very interesting, I'll accept your answer. I like the echo option which would partially work for me (just works for the buttons atm), something to think about. Unfortunately customers can change the switch name to anything they want, which I've just thought about, however it will always end in #. I may need to change my code now due to this, maybe checking that there is a hash with a letter before it. Thanks, depending on the command I will deal with any outputs, luckily I code the actual switch also so know there won't be much deviation.
0

I solved this in a messy way with a boolean and a few strings. i made a method for appending strings.

if((parseCommand.contains("VLAN Name") && parseCommand.contains("Status")&& parseCommand.contains("Ports")) 
                            || ((ShowVlanAppend.contains("VLAN Name")&& ShowVlanAppend.contains("Status")&& ShowVlanAppend.contains("Ports"))))
                        {
                                commandParse();
                                if(finalCommandBool == true){
                                runOnUiThread(new Runnable() {
                                    public void run() {

                                        mReceiveBox.setText(finalCommand);
                                        mReceiveBox.setSelection(finalCommand.length());
                                        ShowVlanAppend = "";
                                        finalCommand = "";
                                        finalCommandBool = false;
                                    }
                                });

                                }


                        }
 public void commandParse()

        {
            if (!parseCommand.contains("switch#")){
            ShowVlanAppend = ShowVlanAppend + parseCommand;
            }
            else{
                finalCommand = ShowVlanAppend;
                finalCommandBool = true;
            }

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.