0

I am working on a simple fingerprint scanner that is connected to my Arduino Uno. I have connected my circuit via usb to my computer, so that I can read serially the data that Arduino is processing for the fingerprint scanner. I am in the process of making a simple UI for the project using JavaFX. Thus far, I have made a few stages.

The first is the home stage which has a single button on it. Basically, clicking this button opens the serial port and listens for 1 of 2 things, the enroll/verify mode (physical buttons on circuit, which after pressed send the strings "Enrollment" or "Verification" to the Serial). After pressing the enrollment, a new stage opens up and since the Arduino is already prepped to enroll the print, I don't have to read the Serial again. After scanning the print, I fill out a form, and hit a submit button which takes the fingerprint id that was created by the fingerprint scanner and links it with one of the fields in the form (behind the scenes). Then the stage closes, and I begin to re-read the Serial.

This is where the problem begins.

I am looking for which window to open, but how can I open the window (depending on which physical button is pressed on circuit) first, then re-read the Serial for whether the user was verified, and then change things on the window?

It may seem complicated, I am aware, which is why some of the code is below.

submitForm.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent e) 
        {
            if(firstNameTextField.getText().length()>0 && 
               lastNameTextField.getText().length()>0 && 
               gpnTextField.getText().length() == 8)
            {
                enrollStage.hide();
                try {
                    //System.out.println("winfwnif ");
                    if(readEnroll(sp)==true)
                    {
                        id++;
                    }
                    Employee employee = new Employee(Long.parseLong(gpnTextField.getText()), id);
                    pw = new PrintWriter(file);
                    pw.println(employee.getFP() + "," + employee.getGPN());
                    pw.close();
                } catch (FileNotFoundException | SerialPortException | InterruptedException e2) {

                    e2.printStackTrace();
                }   
                firstNameTextField.setText("");
                lastNameTextField.setText("");
                gpnTextField.setText("");
                enrollStage.hide();
                home.show();
                try
                {
                    home.close();
                    int mod = checkMode(sp);
                    if(mod == 1)
                    {
                        enrollStage.show();
                    }
                    else if(mod == 2)
                    {

                        boolean f = false;
                        //Thread.sleep(2000);
                        synchronized(verificationStage)
                        {
                            verificationStage.show();
                            Thread.sleep(2000);
                            f = readVerify(sp);

                        }
                        if(f)
                        {
                            userImageIV.setImage(new Image("/images/temp.png", 250, 400, true, false));
                            thumbIV.setImage(new Image("/images/greenThumb.png", 150, 300, true, false));
                        }

                    }
                }
                catch(SerialPortException | InterruptedException err)
                {

                }
            }
        }
    });

This is the function I use to read the verification output from the Arduino.

public boolean readVerify(SerialPort s) throws SerialPortException, InterruptedException
{
    while (true) 
    {
        if (sp.getInputBufferBytesCount() > 0) 
        {   //check size of buffer
            String x = s.readString();
            System.out.println(x);
            if(x.contains("Verified"))
            {
                return true;
            }
        }
        Thread.sleep(100);
    }

}

I tried waiting 2 seconds after opening the window, and then calling readVerify(SerialPort s), but for some reason it reads the print, verifies, and then opens the window. I have been trying for the opposite - open the window, verify, and then change a couple of images on the window if verified. How do I go about doing this?

One last thing,

checkMode(SerialPort s) 

is pretty much is used to read button input on circuit. Depending on what button is pressed, checkMode(SerialPort s) returns 1 (enrollment mode) or 2 (verification mode).

halfer
  • 19,824
  • 17
  • 99
  • 186
Cid
  • 66
  • 7

1 Answers1

0

I found this question where someone asked here how to run code after the window is loaded. Using a WindowEvent may be a solution where you would break up the code so readVerify() is in the WindowEvent listener method.

Nyla of Arda
  • 106
  • 3
  • 9