简体   繁体   English

如何在javafx项目中运行RFID标签监听器方法?

[英]How to run RFID tag listener method in javafx project?

I am working on a project involves RFID technology, what i am trying to do at the moment, once the RFIDTagListener method runs, I basically pass a tag to a reader which its serial number will be sent to a server to get some relevant data back and pop them on a GUI screen.我正在做一个涉及 RFID 技术的项目,目前我正在尝试做的是,一旦RFIDTagListener方法运行,我基本上将一个标签传递给阅读器,它的序列号将被发送到服务器以获取一些相关数据并在 GUI 屏幕上弹出它们。 what I have done so far is getting the data upon sending reader's data manually without passing a tag becasue I don't know how to do it otherwise and here is the problem.到目前为止,我所做的是在不传递标签的情况下手动发送阅读器的数据时获取数据,因为我不知道否则该怎么做,这就是问题所在。 where I am working on a javafx project and when I tried to put the RFIDTagListener method within the MainController class and on compiling, the taglistner method wouldn't be triggered and just ignored, it's only the GUI screen will be opened.However, I also tried to have RFIDTagListener within the main class but on compiling, the taglistner method would be run first and when it's finished in 5 seconds, my GUI window will be opened next.我正在处理一个 javafx 项目,当我尝试将RFIDTagListener方法放在MainController类中并进行编译时,不会触发 taglistner 方法而只是忽略它,只会打开 GUI 屏幕。但是,我也试图在main类中使用RFIDTagListener但在编译时,将首先运行taglistner方法,当它在 5 秒内完成时,接下来将打开我的 GUI 窗口。 SO I Don't know where this method should be located exactly.所以我不知道这个方法应该准确定位在哪里。 Essentially What I want is to have them both running at the same time, the taglistener running in the background with the GUI window opened simulitniously.基本上我想要的是让它们同时运行, taglistener在后台运行,同时打开 GUI 窗口。

Any recommendation guys would be much appricated.任何推荐的家伙都会很受欢迎。

MainController class :主控制器类:

public class MainController {
    RFID rfid = new RFID();

    String ReaderNo = null;
    String walletJson = new String();
    Gson gson = new Gson();

    public static String sensorServerURL = "http://localhost:8080/PhidgetServer2019/SensorServerRFIDdata";

    walletDAO dao = new walletDAO();
    ArrayList<wallet> allwallets = new ArrayList<wallet>();

    @FXML VBox ConsultHR;
    @FXML private Label message;
    @FXML private Label WalletName;
    @FXML private ListView<ArrayList<wallet>> list;
    @FXML private ListView<ArrayList<wallet>> RoomAList;
    @FXML private TableView<wallet> tableViewData;
    @FXML private TableColumn<wallet, String> NameColumn;
    @FXML private TableColumn<wallet, String> LocationColumn;
    @FXML private TableColumn<wallet, String> TagColumn;


    public void getTags(ActionEvent event) throws SQLException {
        allwallets = dao.getWalletTag();
        try {
            allwallets = dao.getWalletTag();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(allwallets);
        list.getItems().add(allwallets);
    }

    public MainController() throws PhidgetException {
        // Make the RFID Phidget able to detect loss or gain of an rfid card
        rfid.addTagListener(new RFIDTagListener() {
            // What to do when a tag is found
            public void onTag(RFIDTagEvent e) {
                try {
                    ReaderNo = String.valueOf(rfid.getDeviceSerialNumber());
                } catch (PhidgetException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                System.out.println("Reader serial number is " + ' '+ReaderNo);
                wallet walletData = new wallet("385055");   
                walletJson = gson.toJson(walletData);       

                String resultfromserver = sendToServer(walletJson); 
                System.out.println("DEBUG: data in json : " +resultfromserver);
                wallet  walletObject = gson.fromJson(resultfromserver, wallet.class);                       

                System.out.println("DEBUG: The wallet's Data: "+' '+ walletObject);

                WalletName.setText(walletObject.getWalletName());
            }
        });

        rfid.addTagLostListener(new RFIDTagLostListener() {
            // What to do when a tag is lost
            public void onTagLost(RFIDTagLostEvent e) {
                // optional print, used as debug here
                System.out.println("DEBUG: Tag lost: " + e.getTag());
            }
       });
    }
}

Main class :主要班级:

public class Main extends Application {
   //RFID rfid = new RFID();

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Wallet locator !");
    try {
        Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
 public static void main(String[] args) throws PhidgetException {
    new MainController();           
    launch(args);
 }
}

Main.FXML :主.FXML :

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TableColumn?>  
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="269.0" prefWidth="403.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <Button layoutX="149.0" layoutY="251.0" mnemonicParsing="false" onAction="#getTags" prefHeight="46.0" prefWidth="82.0" text="tags" />
   <Label fx:id="message" layoutX="139.0" layoutY="209.0" prefHeight="35.0" prefWidth="101.0" />
   <ListView id="studentObservableList" fx:id="list" layoutY="209.0" prefHeight="131.0" prefWidth="139.0" />

   <TableView fx:id="tableViewData" prefHeight="200.0" prefWidth="231.0" style="-fx-border-color: red;">
      <columns>
         <TableColumn fx:id="NameColumn" prefWidth="75.0" text="Name"/>
         <TableColumn fx:id="LocationColumn" prefWidth="75.0" text="Location" />
         <TableColumn fx:id="TagColumn" prefWidth="75.0" text="Tag" />
      </columns>
   </TableView>

   <Button layoutX="251.0" layoutY="14.0" mnemonicParsing="false" onAction="#getWallets" prefHeight="56.0" prefWidth="82.0" text="wallets" />
</AnchorPane>

1st point第 1 点

The main method is inside the MainController class and it's instantiating itself, which is possible but not conventional. main方法在MainController类中,它正在实例化自己,这是可能的,但不是传统的。

2nd point第二点

Never put your main method into a controller!永远不要将您的main方法放入控制器中! Put it into your Main class instead.把它放到你的Main类中。

3rd point第三点

You must define the application launch inside your main method, by calling Application.launch() .您必须通过调用Application.launch()在 main 方法中定义应用程序启动。 This method will call your overriden start method (among other things) and display the GUI:此方法将调用您覆盖的start方法(除其他外)并显示 GUI:

public static void main(String[] args) throws PhidgetException {
    launch(args);
    new MainController();           
}

4th point第四点

You don't need to instantiate directly a controller in JavaFX.您不需要在 JavaFX 中直接实例化控制器。 With your current code you're creating a MainController instance which is unlinked to your GUI;使用您当前的代码,您正在创建一个未链接到您的 GUI 的MainController实例; that's why you can see your GUI without interacting with it.这就是为什么您可以在不与之交互的情况下看到您的 GUI。 You need to remove the new MainController();您需要删除new MainController(); line into your main method, then check if the fx:controller attribute is defined in the root of your FXML file.进入您的main方法,然后检查fx:controller属性是否在您的 FXML 文件的根目录中定义。

You can call this controller instance using root.getController() in your start method.您可以在start方法中使用root.getController()调用此控制器实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM