0

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?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

3 Comments

Yes that is correct, is there a better way to get the data sent from the comport? Should I be using an InputStreamReader or something?
A lot depends on what the scale is transmitting, and what you're trying to do. (This is where code from your question would also help) You might be able to iterate over that byte array to get the contents. InputStreamReader is to take a stream of bytes -> a stream of characters (you have to provide a character set).
Oh ok ... let me try that and see what happens. I can't provide code because it is on another machine. Thanks for the feedback thou.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.