Read double from serial port

DavidM

Im trying to read a double from a serial port. I can read it in String, but when I parse the String to Double, the result makes no sense, and it's not the same from the String.

This is my code:

public void serialEvent(SerialPortEvent event){
        switch(event.getEventType()) {

            case SerialPortEvent.DATA_AVAILABLE:
                byte[] readBuffer = new byte[8];
                try {
                    while (inputStream.available()>0) {
                     int numBytes = inputStream.read(readBuffer);
                     String peso = new String (readBuffer, 0, numBytes, "UTF-8");
                     System.out.print(new String (readBuffer, 0, numBytes, "UTF-8"));
                    //double d = ByteBuffer.wrap(readBuffer).order(ByteOrder.LITTLE_ENDIAN ).getDouble();
                    //System.out.print(d);
                     m_dWeightBuffer = Double.parseDouble(peso);
                     System.out.print(m_dWeightBuffer);
                     }
                     //System.out.print(new String (readBuffer, 0, numBytes, "UTF-8"));
                    } catch (IOException ex) {}
                break;

        } 
    }

But now, you know how I can convert this string to Double?

System.out.print(new String (readBuffer, 0, numBytes, "UTF-8")); Output: 000.250 Good

m_dWeightBuffer = Double.parseDouble(peso); System.out.print(m_dWeightBuffer); Output: 0.00.0250.0 Bad

System.out.println(new String (readBuffer, 0, numBytes, "UTF-8")); Output:

0 0 0 .250

Mueretee

Try doing this:

 public void serialEvent(SerialPortEvent event){
            switch(event.getEventType()) {

                case SerialPortEvent.DATA_AVAILABLE:
                    byte[] readBuffer = new byte[8];


                    try {
                         while (inputStream.available()>0) {
                             int numBytes = inputStream.read(readBuffer);
                             peso += new String (readBuffer, 0, numBytes, "UTF-8").replaceAll("\\s+","").replaceAll("\\n", "").replaceAll("\\r", "");

                         }
                    } catch (IOException ex) {}

                    break;

            } 
    }

Inicialize the String peso OUTSIDE of this SerialEvent method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read double from serial port

From Dev

Read from serial port in Windows

From Dev

Pharo: read byte from Arduino serial port

From Dev

How to read serial port data from JavaScript

From Dev

Read and Store Bytes from Serial Port

From Dev

How to Read Data from Serial Port in R

From Dev

Putting delay in read and write from serial port

From Dev

How to read data from the serial port in QT?

From Dev

How to read the response from serial port

From Dev

serial port thread locking while processing data read from serial port

From Dev

Pyserial read from serial port and write to C program

From Dev

Read serial port from client(Browser) using Applet + Javascript

From Dev

How to read Binary Data from Serial Port without any Encoding?

From Dev

read more than 8 bytes from the serial port

From Dev

Read data from serial port device in c#

From Dev

Read string from serial port and store into 2 different arrays

From Dev

Serial port program to write file/read to file from tty?

From Dev

Read String from Arduino serial port with JAVA and jssc lib

From Dev

How to read data from a serial port and write it to a file

From Dev

How to Read the serial port name from text box (editable)

From Dev

Pycharm Kivy Serial Port Read

From Dev

vxworks serial port read timeout

From Dev

Python read from serial

From Dev

Cannot read data from serial port from Ubuntu guest on a OSX host

From Dev

Data from USB ~ Serial Port

From Dev

Reading from serial port in C

From Dev

Receiving data from Serial Port

From Dev

code not reading from serial port

From Dev

Wake up blocking serial port read in Linux