Problem: Trying to read from some electronic scales using the comport via Java
I am trying to read from a com port using Java. So far I have been successfull in creating a small application that uses the Java SerialPort and InputStream classes to read from the comport.
The application uses a SerialPortEventListener to listen to event sent via the comport of the scale to the computer. So far I have had some success by using an InputStream inside the event listener to read some bytes from the comport, however the output does not make any sense and I keep getting messages in the form of:
[B@8813f2
or
[B@1d58aae
To clarify I am receiving these messages on screen when I interact with the keypad of the scale. I just need some help on interpreting the output correctly. Am I using the correct classes to read and write to the comport?
You have read the data into a
byte[], and then attempted to dump it by usingSystem.out.println(data)wheredatais declaredbyte[] data. That, unfortunately will just print the array’s internal representation, which is, uselessly, ‘[‘ followed by the hex hash code.Instead, you want to dump the contents of the array. Using
is the simplest way which should work for you.
Otherwise, you need to iterate the array, and print each byte, or transform the byte array to a
Stringusing, for example,new String(data)(which will use the platform default encoding).