1

I am reading Accelerometer signals connected to Arduino board through a serial interface. I am using the RxTx library and following is the code.

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

The problem I have is that, I'm reading the values in Windows environment perfectly, without any issues. But in Ubuntu I get data that is damaged and partially read. Here is the Java console output when I did this in ubuntu.

These are correct readings.

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

But I get readings like this too. (In a considerable amount)

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

Why does this happen, where I can read perfectly in Windows and Not in Ubuntu.

Any help please? Thank You.

  • Have you looked into what the default flow control modes are on each OS? try changing the code to enforce a particular mode and see if it has any effect. – BrettAM Dec 6 '14 at 3:47
  • Could you explain more on that Brett? Do you see any issue in my code? – kinath_ru Dec 6 '14 at 3:49
  • No issues per say, but you never explicitly set the flow control mode and your errors look similar to what happens when the receive buffer becomes full and starts dropping characters. Flow control requests the sender stop for a bit while the buffer gets processed. Try using different flow control modes with this: show.docjava.com/book/cgij/code/data/j4pDoc/serialPort/rxtx/… – BrettAM Dec 6 '14 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.