我正在开发一个连接到我的 Arduino Uno 的简单指纹扫描仪。我已经通过 USB 将我的电路连接到我的电脑,这样我就可以串行读取 Arduino 正在为指纹扫描仪处理的数据。我正在使用 JavaFX 为项目制作一个简单的 UI。到目前为止,我已经做了几个阶段。
第一个是主舞台,上面有一个按钮。基本上,单击此按钮会打开串行端口并侦听 2 项中的 1 项,即注册/验证模式(电路上的物理按钮,按下后将字符串“注册”或“验证”发送到串行)。按下注册后,一个新的阶段打开了,由于 Arduino 已经准备好注册打印,我不必再次阅读 Serial。扫描打印件后,我填写了一张表格,然后点击提交按钮,该按钮获取由指纹扫描仪创建的指纹 ID,并将其与表单中的一个字段链接(在幕后)。然后舞台关闭,我开始重新阅读连载。
这就是问题的开始。
我正在寻找要打开哪个窗口,但是如何先打开窗口(取决于在电路上按下了哪个物理按钮),然后重新读取 Serial 以了解用户是否已通过验证,然后更改窗口上的内容?
我知道,这可能看起来很复杂,这就是为什么下面有一些代码。
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)
{
}
}
}
});
这是我用来从 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);
}
}
我尝试在打开窗口后等待 2 秒,然后调用 readVerify(SerialPort s),但由于某种原因它读取打印,验证,然后打开窗口。我一直在尝试相反的方法-打开窗口,验证,然后在验证后更改窗口上的几个图像。我该怎么做呢?
最后一件事,
checkMode(SerialPort s)
几乎用于读取电路上的按钮输入。根据按下的按钮,checkMode(SerialPort s) 返回 1(注册模式)或 2(验证模式)。