My SerialPortEvent is not receiving data using jSSC in a continuous loop

I am trying to use serial communication with an Arduino Uno and was using the jSSC-2.6.0 library. 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 a response, serialEvent usually prints the received bytes to the Console. But if I try to wait until there is no data in the linked list, the program will just keep looping and the SerialEvent never adds bytes to the LinkedList, it doesn't even register the received bytes.

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

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

      

But this method just stucks onto this.available (), which returns the size of the LinkedList, since no data is actually received from the Arduino or no serialEvent is received:

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've debugged the program and sometimes debugged, the program works, but not always. Also the program gets stuck when I try to check if there are some bytes in the linked list ie while (available () <1). Otherwise if I don't check I end up getting the correct bytes response from the Arduino

0


source to share


1 answer


Found the answer myself after spending 4 hours. I was better off using a method readBytes()

with a Count 1 byte and a timeOut of 100ms to be safe. 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;
}

      

0


source







All Articles