Unable to read serial data from COM port in Java
<p>I am trying to read serial data for my Java application, which I am developing on Linux (Ubuntu 12.10). This is my code:</p>

<pre><code>try {
    portId = CommPortIdentifier.getPortIdentifier("/dev/ttyS0");
    SimpleRead reader = new SimpleRead();
} catch (Exception e) {
    TimeStamp=new Date().toString();
    System.out.println(TimeStamp+": ttyS0 "+portId);
    System.out.println(TimeStamp+": msg1 - "+e);
}
</code></pre>

<p>SimpleRead constructor and other required code:</p>

<pre><code>public SimpleRead() {
    try {
        TimeStamp=new Date().toString();
        serialPort = (SerialPort) portId.open("SimpleRead", 2000);
        System.out.println(TimeStamp + ": " + portId.getName() + " opened for scanner input");
    } catch (PortInUseException e) {
        System.out.println(e);
    }
    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {
        System.out.println(e);
    }
    System.out.println("Input Stream set");
    try {
        serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {
        System.out.println(e);
    }
    System.out.println("Event listener added");
    serialPort.notifyOnDataAvailable(true);
    try {
        serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        serialPort.setDTR(false);
        serialPort.setRTS(false);
    } catch (UnsupportedCommOperationException e) {
        System.out.println(e);
    }
    System.out.println("Parameters written");
    readThread = new Thread(this);
    readThread.start();
}

@Override
public void run() {
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        System.out.println(e);
    }
}

@Override
public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            System.out.println("Hello");
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[20];
            System.out.println("Hello");
            try {
                while (inputStream.available() &gt; 0) {
                    int numBytes = inputStream.read(readBuffer);
                    if (numBytes&gt;=16)
                        break;
                }
                //System.out.print(new String(readBuffer));
                TimeStamp = new java.util.Date().toString();
                System.out.println(TimeStamp + ": scanned input received:" + new String(readBuffer));
                inputStream.close();
            } catch (IOException e) {
                System.out.println(e);
            }
            break;
    }
}
</code></pre>

<p>For some reason, there is no serial data being read from the port, since the "Hello" in the body of the serialEvent() method is not printed in the output screen. Please do help me out here!</p>

<p><strong>EDIT</strong> Code taken from: <a href="http://www.java-samples.com/showtutorial.php?tutorialid=11" rel="nofollow">http://www.java-samples.com/showtutorial.php?tutorialid=11</a>
And yes, the device was configured for the given parameters before attempting to read from it.</p>

<p><strong>New Edit</strong> I am using Java SE 8 Development Kit for Linux x64 platform, and the Java Communications API taken from <a href="http://www.java2s.com/Code/Jar/c/Downloadcomm20jar.htm" rel="nofollow">http://www.java2s.com/Code/Jar/c/Downloadcomm20jar.htm</a>. My IDE is Netbeans 8.1.</p>