1

I have a code below that I am sending to a serial USB port... it was working, now its not! the akber() function dies if the wrong string is sent...

if I send akber("1.0.0.0.1.5") - it works perfectly,

if I send akber("23.0.128.0.0.5") - it does not work...

See code below

    public static byte[] akber(final String input) {
        StringTokenizer tokens = new StringTokenizer(input, ".");
        int numberOfArrays = tokens.countTokens();
        byte[][] byteArrays;
        byteArrays = new byte[numberOfArrays][4];
        int i = 0;
        int space = 0;
        while (tokens.hasMoreTokens()) {
            int x = Integer.valueOf(tokens.nextToken());
            if (x<256) { space++; } else { space+=2; }  
            byteArrays[i] = BigInteger.valueOf(x).toByteArray();
            i++;
        }
        final byte[] output = new byte[space];
        copySmallArraysToBigArray(byteArrays, output);
        return output;
    }

    public static void copySmallArraysToBigArray(final byte[][] smallArrays, final byte[] bigArray) {
        int currentOffset = 0;
        for (final byte[] currentArray : smallArrays) {
            System.arraycopy(currentArray, 0, bigArray, currentOffset, currentArray.length);
            currentOffset += currentArray.length;
        }
}

called from function:

serialPort.writeBytes(akber(data));

I would need it to work with any combination of numbers in the "data" string, so it converts them to the right type of bytes and writes to port... its not my code, and I don't quite understand it, but still need to fix it :-)

masoud
  • 55,379
  • 16
  • 141
  • 208
  • 1
    what do you mean by 'the akber() function dies' ? is there any stacktrace? – linski Mar 21 '13 at 11:08
  • If I do System.out.println("akberData=" + akber("1.0.2.0.0.5")); It returns akberData=[B@101e178 ...If I do System.out.println("akberData=" + akber("1.0.128.0.0.5")); It returns null, and the function its in stops at that point. ...Actually it seems a value over 128 the function returns nothing, if below 128 it works... ? – user2194607 Mar 21 '13 at 11:20

2 Answers2

1

Change this line:

if (x<256) { space++; } else { space+=2; }  

to

if (x<128) { space++; } else { space+=2; }  

I ran your code, originally it throws an IndexOutOfBoundsException for

akber("1.0.128.0.0.5");

so check your code that it is not consuming exceptions somewhere, e.g.

try {
     exceptionThrowingMethod();
}      
catch(Exception e) {
}

If the exceptionThrowingMethod throws an exception the code will continue as if the exception was not thrown (but the exceptionThrowingMethod didn't execute succesfully!)

linski
  • 5,046
  • 3
  • 22
  • 35
0

Actually although the above allowed the code to continue working, it did not solve the problem as byte values from the function above 128 were the negative equivalent or something, and sent the wrong values, so the USB hardware was receiving the incorrect characters and not working... by looking at other posts about "Java 128 bytes" on stackoverflow, worked out its something to with byte code above 128 being the same as its negative equivalent, so solved it with trial and error, very annoying - but changed that line to:

    if (x<128) { space++; } else { space+=2;
                int x2 = (x-128)*2;
                x=x-(x*2);
                if (x<-128) { x=x+x2; }
                }

which seemed to work. So happy days, till I find another issue with it! Might be a simple solution for people looking to convert values above 127 to bytes, than more standard Java solution I saw and didnt really understand, as am more used to scripting. Thanks.