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 :-)