1

I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data.

However I am just sending byte arrays over and back so I don't know what status the switch is in, is it in enable more or configuration mode etc. This leads me to possibly enter commands in the wrong mode and they won't work. The switch is then in an unrecoverable mode as I have sent a wrong command and there is no way to delete it or get to a new line. The problem I am trying to deal with for now is the one where I don;t know the mode the switch is in. I know that I can have three different return prompts after I send a command:

switch>
switch#
switch(config)#

So I was thinking that if I read the last two characters received I could tell which mode I'm in. h>, h# and )#

I get data back in this method, whenever data is received this is run:

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

    String str = new String(data);
    ((MyBAIsWrapper) bis).renew(data);

    mSession.write(str);
    mSession.notifyUpdate();
    viewHandler.post(updateView);
}

Would the best way to do this be search the byte array somehow or to convert it to a string and search the string for h>, h# and )#, then I could set a global variable depending on the value I get back? Maybe search from the end backwards?

5
  • 2
    I think, that converting it to String is the simplest and clear way. I would do so too. Commented Jan 9, 2013 at 13:17
  • 1
    When converting it to a string, don't forget to specify the encoding. Commented Jan 9, 2013 at 13:18
  • Why is it necessary to specify, I have never encoded a string, apologies for the ignorance. So i would encode it as ascii? UTF8? How do these choices affect me? Commented Jan 9, 2013 at 14:11
  • 1
    @Paul: you need to decode the bytes you get. Therefore you need to know the way they were encoded by the terminal. Commented Jan 9, 2013 at 14:48
  • well it looks like they are in decimal, as the decimal numbers on the ascii chart correspond to the numbers I am getting when I look at the letter beside it. i'm not sure what to do though. i will read about it. Commented Jan 9, 2013 at 15:15

2 Answers 2

4

I would search the byte[] and avoid the overhead from the conversion to a String.

If it is more performant to start search from the end, depends on the data you get. I cannot judge this by the little example you posted in your question.

If you only need to look at the last two characters:

if(data[data.length-2]=='h' && data[data.length-1]=='>') // "h>"


if(data[data.length-2]=='h' && data[data.length-1]=='#') // "h#"

Or if it is not so simple, use a loop to iterate the array.

P.s. I assumes ASCII encoding for the code above

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

10 Comments

Well I would enter a command and there could be thousands of characters returned, then a newline, then the prompt. So yes I imagine it would be the last two that would be of importance. Unless I can think of a case where they wont be the last two and I can use the loop. Your way seems reasonable. I suppose I shouldn't be converting to strings if it is easily avoided.
Only the 4 bytes? Do you know what they should mean? Interpreted as ASCII they are i escape [J.
It seams that your terminal does not use ASCII. So you must find out what encoding / charset it uses.
I think nearly every terminal emulator can use ASCII (You may need to configure it). But consider that then using ASCII you cannot transfer non ASCII characters.
h = 104 , > = 62 and # = 35
|
2

Do you mean?

if(str.endsWith("h>") || str.endsWith("h#") || str.endsWith(")#"))

or simpler

if(str.equals("switch>") || str.equals("switch#") || str.equals("switch(context)#"))

or using a regex

final Pattern switched = Pattern.compile("switch(>|#|\\(\\w+\\)#)");

if(str.matcher(switched).matches())

1 Comment

Thanks, I thought of regex etc but I need to know them separately to perform different operations, so I think it is too simple to bother with a regex and that MrSmith42's answer would suffice.

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.