0
votes

String Serial_Input must contain a serial number card RFID (MIFARE) like this A45F45A7 (8 byte). sometimes when I approach the card to the RFID reader of arduino the string is like this A45F45 (truncated) , miss any char. there is a solution better that while loop ? (more elegant and efficient) Using Arduino IDE Serial Monitor the card's serial number is correct.

public static void connectionToCom(SerialPort serialPort, ComboBox<String> cbxComPort, TextArea txaMessages) throws SerialPortException
{       
    int baudrate = 9600; int databits = 8; int stopbits = 1; int parity = 0;

    serialPort.openPort() ;
    serialPort.setParams(baudrate, databits, stopbits, parity) ;

    String Serial_Input = null;

    try {
        while (true)
        {
            if (serialPort.readString() != null)
            {
                Serial_Input = serialPort.readString(8);

                System.out.println("Card Serial: " + Serial_Input + "\n");
                //serialPort.closePort();
            }
        }
    } 
    catch (SerialPortException ex){
        txaMessages.appendText(ex.toString());
    }
}

Here the result image

1

1 Answers

0
votes

You could use the method addEventListener(SerialPortEventListener listener, int mask). It call a callback method whenever you receive a byte via serialPort.

The problem with the incomplete string could be 2 problems

  1. The code is executing before the whole string is received. To solve this, you must add a code to verify the length of string you are receiving.

  2. You are using readString twice. You may lost some bytes of the string in first usage.