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. Not that I have that working I want to create some other graphical interface, maybe just a textBox, on screen which shows a briefer version of the results for the user alongside the full version in the terminal. They can just glance at this for a quicker view of results. I'm wondering how to parse it best. When I send a command a byte array is sent back to me containing data like this as an example of one set of results:

Interface           IP-Address       Status                Protocol        
vlan1               192.168.1.1      up                    up  
Fa0/1i              unassigned       administratively down down
Fa0/1o              unassigned       down                  down
Fa1/0               unassigned       down                  down
Fa1/1               unassigned       down                  down
Fa1/2               unassigned       down                  down
Fa1/3               unassigned       down                  down
Fa1/4               unassigned       down                  down
Fa1/5               unassigned       down                  down
Fa1/6               unassigned       down                  down
Fa1/7               unassigned       down                  down
Fa1/8               unassigned       up                    up  
Fa1/9               unassigned       down                  down
Fa1/10              unassigned       up                    up  
Fa1/11              unassigned       down                  down
Gi0                 unassigned       up                    up  
switch# 

How would I parse this? I would like something like "The interface vlan1 is up with an IP of 192.1.1.168" or "the interface Fa1/8 is up with an unassigned IP address." to appear for the user, just listing the most pertinent data. Would I just say something like:

    if (dataReceived.charAt(i) == 'v'
    && dataReceived.charAt(i + 1) == 'l'
    && dataReceived.charAt(i + 2) == 'a'
    && dataReceived.charAt(i + 3) == 'n')
    && dataReceived.charAt(i + 4) == '1')
    && etc //check i is in bounds, check is the next non-whitespace character a number or a u somehow and so on 
        {
         //do things here
         //print out "The interface vlan1 is up with an IP of 192.1.1.168"
        }

This way seems quite tedious, checking every byte, seeing what the next bytes are and so on. Is this the best/a correct way however?

EDIT: here is a sample I have at the moment, where I am receiving data, and where I am testing it for certain data:

public void onDataReceived(int id, byte[] data) {


            dataReceived = new String(data);


        try {
            dataReceivedByte = dataReceived.getBytes("ASCII");
        } catch (UnsupportedEncodingException e) {
            Log.d(TAG, "exception");
            e.printStackTrace();
        }
        statusBool = true;
        Log.d(TAG, "in data received " + dataReceived);
        ((MyBAIsWrapper) bis).renew(data);


        runOnUiThread(new Runnable(){

            @Override
            public void run() {


            }});
        viewHandler.post(updateView);
    }


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

        public void run() {

            mEmulatorView.invalidate();

            if (statusBool == true) {
                for (int i = 1; i < dataReceived.length() - 1; i++) {

                    if (dataReceived.charAt(i) == '>') {

                        Log.d(TAG, "found >");
                        deviceStatus = 0;
                    }
                    if (dataReceived.charAt(i) == '#'
                            && dataReceived.charAt(i - 1) != ')') {

                        Log.d(TAG, "found #");
                        deviceStatus = 1;
                    }
                    if ((i + 1) <= (dataReceived.length())
                            && dataReceived.charAt(i) == ')'
                            && dataReceived.charAt(i + 1) == '#') {

                        Log.d(TAG, "found config )#");
                        deviceStatus = 2;
                    }

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

            }
        }
    };
2
  • Do you get bytes or chars? Commented Jan 30, 2013 at 16:22
  • I get bytes but I just converted it to a string to use charAt dataReceived = new String(data); Commented Jan 30, 2013 at 16:37

2 Answers 2

1

As you are using charAt(), I suppose that dataReceived is a String. If not, it might be convenient to convert the data into a string, as you can then make use of the convenient String methods. You can covnert a byte array to a string using the String(byte[]) constructor.

So, you could also use the contains() method to check if the string contains a particular substring:

if(dataReceived.contains("vlan1")) {
  // do things here
}

Or if you are interested in the location of the substring you can use indexOf().

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

3 Comments

Yeah I converted the byte array to a string when it arrived to use the methods. The problem I see with .contains is what happens if one byte array comes in with vla and the next comes in with n1 and then they are two separate strings etc.
So you basically need to buffer the incoming byte arrays/chunks until you have a complete line (eg when a newline is received) and then create the String out of these byte arrays...
Yeah that sounds good. Then I could use contains and it would be much cleaner.
1

This would be very be easy to parse properly but it degrades in one place from the well structured table into free text. The code below uses the patching replacement; it is not very clean solution. If some delimiter like \t exists only between columns but not between words in the column, using it would be much better. Parsing fixed width sections is possible but also a mess.

Assuming the input is a string x:

class Record {
  String mItf, mIp, mStatus, mProt;
}

ArrayList<Record> records = new ArrayList<Record>();

StringTokenizer st = new StringTokenizer(x,"\r\n"); 
  // Line by line but do not split at spaces.

st.nextToken(); // Discard the header line.

while (st.hasMoreTokens()) {
   String line = st.nextToken();

   // Group in a single word problematic two word cases (may be multiple):
   line = line.replace("administratively down","administratively_down");

   StringTokenizer sr = new StringTokenizer(line);
   if (sr.countTokens() >= 4) {
     Record r = new Record();
     r.mItf = sr.nextToken();
     r.mIp = sr.nexttoken();
     r.mStatus = sr.nextToken();
     r.mProt = sr.nextToken();
     records.add(r);
   }
}

After that, you can do generalizations you like.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.