javaserial-portarduinojssc

My SerialPortEvent does not receive data using jSSC in a continous loop


I have been trying to use serial communication with my Arduino Uno and have used the library jSSC-2.6.0. I am using a SeriaPortEvent listener to receive bytes from the Serial Port (Arduino) and store them in a linked list.

public synchronized void serialEvent(SerialPortEvent serialPortEvent) {
    if (serialPortEvent.isRXCHAR()) { // if we receive data
        if (serialPortEvent.getEventValue() > 0) { // if there is some existent data
            try {
                byte[] bytes = this.serialPort.readBytes(); // reading the bytes received on serial port
                if (bytes != null) {
                    for (byte b : bytes) {
                        this.serialInput.add(b); // adding the bytes to the linked list

                        // *** DEBUGGING *** //
                        System.out.print(String.format("%X ", b));
                    }
                }           
            } catch (SerialPortException e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }
    }

}

Now if I send individual data in a loop and don't wait for any response the serialEvent usually prints bytes received, to the Console. But If I try and wait till there is some data in the linked list the program just keeps on looping and the SerialEvent never adds bytes to the LinkedList, it even does not even register any bytes being received.

This works and the correct bytes are sent by Arduino received by SerialEvent and printed to the Console:

while(true) {
    t.write((byte) 0x41);
}

But this method just stucks at this.available() which returns the size of the LinkedList, as in no data is actually received from the Arduino or receieved by the serialEvent:

public boolean testComm() throws SerialPortException {
    if (!this.serialPort.isOpened()) // if port is not open return false
        return false;

    this.write(SerialCOM.TEST); // SerialCOM.TEST = 0x41

    while (this.available() < 1)
        ; // we wait for a response

    if (this.read() == SerialCOM.SUCCESS)
        return true;
    return false;
}

I have debugged the program and sometimes debugging, the program does work but not always. Also the program only gets stuck when i try and check if there is some bytes in the linkedlist i.e while(available() < 1). Otherwise if I dont check I eventually receive the correct response of bytes from Arduino


Solution

  • Found the answer myself after wasting 4hours. I was better off using the readBytes() method with a byteCount of 1 and timeOut of 100ms just to be on the safe side. So now the read method looks like this.

        private byte read() throws SerialPortException{
        byte[] temp = null;
        try {
            temp = this.serialPort.readBytes(1, 100);
            if (temp == null) {
                throw new SerialPortException(this.serialPort.getPortName(),
                        "SerialCOM : read()", "Can't read from Serial Port");
            } else {
                return temp[0];
            }
        } catch (SerialPortTimeoutException e) {
            System.out.println(e);
            e.printStackTrace();
        }
        return (Byte) null;
    }