0

I'm trying to write a single byte to an Arduino via serial. The "command" is a singe char sent from another method. The 's' correctly transfers to 115 when I view byteCommand[0] but when I view byteCommand as a whole it will show up as a string staring with [B@ and then a bunch of hex characters. I could modify the Arduino code to look for that rather than the char value, but I feel like the byte can be sent through properly.

        byte[] byteCommand = new byte[1];         
        byteCommand[0] = (byte) command[0]

        if (command == 's' || command == 'f') {
            mSerialPort.write(byteCommand);
            commandInfo.setText(String.valueOf(command));
            rawInfo.setText(String.valueOf(byteCommand));
        }
JHinne
  • 9
  • 6
  • I think, that it is quite normal, when you are sending array. If you will make on almost any array method toString(), then it will result in this king of weird things. I think, that this can help you: http://stackoverflow.com/questions/6684665/java-byte-array-to-string-to-byte-array – Mateusz Pryczkowski Dec 03 '14 at 20:22
  • This makes sense, but I'm still confused as to why the String changes with each iteration. If the only byte is `115` then shouldn't the string stay the same? – JHinne Dec 03 '14 at 20:25
  • It is changing, as it is showing memory location. – Mateusz Pryczkowski Dec 03 '14 at 20:28
  • No, it makes complete sense, part of the string you are seeing is the "hashCode" value of the object, and if you are creating different objects hashCodes might be different too and hence the string you are seeing..., it has nothing to do with the memory location... – Martin Cazares Dec 03 '14 at 20:29
  • Oh, understood. I switched my string views to `Arrays.toString()` and now it displays the byte. I'm going to change the viewer on the Arduino side to look for the byte value instead of the total byte. – JHinne Dec 03 '14 at 20:30

1 Answers1

0

That's because when you see the "byteCommand as a whole" you are not really looking at the values within the array but instead you are looking at the string returned by the object "byteArray.toString()" method, which is usually something like what your seeing...

If you want to actually see the values within your array, something like [115,23,56], then you must use the Arrays method to string as follows:

String arryValuesString = Arrays.toString(byteCommand);

Hope it helps!

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • I got that figured out, thanks! I'm now trying to figure out how to get that vaue read from the Arduino. – JHinne Dec 03 '14 at 20:36