Simon H
Simon H

Reputation: 1745

Java reading from comport

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?

Upvotes: 0

Views: 584

Answers (3)

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64026

You have read the data into a byte[], and then attempted to dump it by using System.out.println(data) where data is declared byte[] 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

System.out.println(Arrays.toString(data))

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 String using, for example, new String(data) (which will use the platform default encoding).

Upvotes: 1

Charlie
Charlie

Reputation: 7349

The object you have there is apparently a byte array. I take it you're taking the object and printing it to the console. See: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getName() and: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString()

Upvotes: 0

bmargulies
bmargulies

Reputation: 99993

Those look like the result of printing a byte array object as a raw object reference. So your call has some sort of confused call to System.out.something or System.err.something, most likely.

Upvotes: 1

Related Questions