在Ubuntu中使用RXTX错误地读取串行

时间:2014-12-06 02:06:31

标签: java ubuntu serial-port rxtx

我正在读取通过串行接口连接到Arduino板的Accelerometer信号。我正在使用RxTx库,以下是代码。

private void initializeSerial() {
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currentPortIdentifier = (CommPortIdentifier) portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currentPortIdentifier.getName().equals(portName)) {
                portId = currentPortIdentifier;
                break;
            }
        }
    }

    if (portId == null) {
        System.out.println("Port not found");
        return;
    }

    try {

        serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000);
        serialPort
                .setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();

        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);

    } catch (Exception e) {
        System.err.println("Initialization failed : " + e.toString());
    }
}

@Override
public void serialEvent(SerialPortEvent event) {
    if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine = input.readLine();
            String[] inputValues = inputLine.split(",");
            System.out.println(inputLine);

            if (inputValues.length == 10 && inputValues[0].equals("STX")
                    && inputValues[inputValues.length - 1].equals("ETX")) {

                double classid = 0;
                double accx = 0;
                double accy = 0;
                double accz = 0;
                boolean num_error = false;

                try {

                    classid = Integer.parseInt(txtClassID.getText());
                    accx = Double.parseDouble(inputValues[2]);
                    accy = Double.parseDouble(inputValues[4]);
                    accz = Double.parseDouble(inputValues[6]);
                    num_error = false;

                } catch (NumberFormatException numex) {
                    num_error = true;
                }

                if (recording) {

                    if (!num_error) {
                        gestureList.add(new double[] { classid, accx, accy, accz });
                        System.out.println("ClassID : " + classid + " \tAdding : "+ accx +","+ accy+","+ accz);
                    }

                    System.out.println("ClassID : " + classid + " \tAdding : " + accx + accy + accz);
                } else {
                    if (gestureList.size() > 0) {
                        gestureCollection.add(gestureList);
                        System.out.println("Segment :" + (gestureCollection.size()) + "   Items : "
                                + gestureList.size());
                        txtSegments.append("Segment " + (gestureCollection.size()) + "\tItems : "
                                + gestureList.size() + "\n");

                        // comboSegments.addItem(gestureCollection.size());

                        gestureList = new ArrayList<>();
                    }
                }

                txtRawData.append(inputLine + "\n");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

我遇到的问题是,我正在完美地阅读Windows环境中的值,没有任何问题。但是在Ubuntu中,我得到了损坏和部分读取的数据。这是我在ubuntu中执行此操作时的Java控制台输出。

这些是正确的读数。

STX,16564.00,16565.65,-352.00,-431.09,-17840.00,-17941.79,-178.64,-42.72,ETX STX,16524.00,16562.53,-440.00,-431.76,-17848.00,-17934.75,-178.63,-42.73,ETX STX,16572.00,16563.24,-420.00,-430.88,-17992.00,-17939.05,-178.64,-42.72,ETX

但我也得到这样的读数。 (相当多)

STX,16580.00,16551.27,-432.00,-438.85,-17936.00,-17922.1724.00,16548.94,-380.00,-439.41,-17880.00,-17921.05,-178.61,-42.71,ETX

9-178.56,-42.75,ETX

, - 454.82,-17944.00,-17916.45,-178.54,-42.72,ETX

STX,16584.00,16558.19,-464.00,-465.68,-18007416.00,-446.99,-18008.00,-17935.43,-178.56,-42.71,ETX

为什么会发生这种情况,我可以在Windows中完美阅读,而不是在Ubuntu中。

请帮忙吗?谢谢。

0 个答案:

没有答案