我正在用Java构建一个程序,从arduino(x,y,z)接收sensordata。我得到了数据,但是格式错误。

我需要将数据作为单个值读取,以便可以将它们放入arraylist。

public class SerialInput {
SerialPort sp;
int counter = 0;

public SerialInput() {
    sp = new SerialPort("COM4");
    System.out.println("Opening port...");
    try {
        sp.openPort();
    } catch (SerialPortException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        sp.addEventListener(new SerialPortEventListener() {

            @Override
            public void serialEvent(SerialPortEvent arg0) {
                try {
                    String output = sp.readString();
                    System.out.println(output);
                }
                catch (SerialPortException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    } catch (SerialPortException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    new SerialInput();
    }
}

最佳答案

我有类似的问题,我这样解决了。在我的情况下,我的数据就像h,x1,y1,z1,x2,y2,z2。因此,只需对方法进行少量修改即可解析数据x,y,z,那么它将满足您的要求

public class SerialDataReader {

static SerialPort serialPort;
private static final int DATA_RATE = 9600;
private static final String PORT_NAME = "COM4";

public static void main(String[] args) {
    SerialDataReader serialDataReader = new SerialDataReader();
}

public SerialDataReader() {
    serialPort = new SerialPort(PORT_NAME);
    try {
        serialPort.openPort();
        serialPort.setParams(DATA_RATE, 8, 1, 0);

        serialPort.addEventListener(new SerialPortReaderListener());
    } catch (SerialPortException ex) {
        System.out.println(ex);
    }

    Runtime.getRuntime().addShutdownHook(
            new Thread(
                    new Runnable() {
                        public void run() {
                            try {
                                System.out.println("Closing...... : SerialDataReader");
                                serialPort.removeEventListener();
                                serialPort.closePort();
                            } catch (SerialPortException e) {
                                e.printStackTrace();
                            }
                        }
                    }));

}

private class SerialPortReaderListener implements SerialPortEventListener {
    public SerialPortReaderListener() {
        System.out.println("Starting..... : Serial port listener");
    }

    public void serialEvent(SerialPortEvent event) {
        StringBuilder stringBuilder = new StringBuilder();
        char singleChr;
        try {
            singleChr = (char) (serialPort.readBytes(1)[0]);
            if (singleChr == 'h') {
                do {
                    stringBuilder.append(singleChr);
                    singleChr = (char) (serialPort.readBytes(1)[0]);
                } while (singleChr != '\n');

                String strOutput = stringBuilder.toString();

                double[] valueOutput = StrToDoubleArray(strOutput);
                for (double in : valueOutput) {
                    yourMethode(in);
                }
            }

        } catch (SerialPortException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    private double[] StrToDoubleArray(String in) {
        String[] strArray = in.split(",");

        if (!strArray[0].equals("h") || strArray.length != 7) {
            return new double[0];
        }

        double[] output = new double[6];
        for (int i = 0; i < output.length; i++)
            output[i] = Double.parseDouble(strArray[i + 1]);

        return output;
    }
}


}

10-05 18:23