如何使用jSerialComm在Java中自动获取运动数据?

时间:2019-02-06 10:35:53

标签: javafx port

使用此代码,我在单击获取按钮(由代码中的doFetch()表示)后从comport获取数据(数据基本上是我的rfid标签的UID)。当我必须获取另一个rfid标签的数据时,我又必须按下获取按钮。我希望它以某种方式工作,一旦我单击“获取”按钮,它就会在点击时自动显示rfid标签的数据,然后在点击时显示另一个rfid标签的数据。意味着我要避免一次又一次单击获取按钮。 基本上我是从comport读取rfid标签的UID,然后它与数据库中的UID匹配,并在tableView中显示与该UID相关的整个数据。 我希望它只需要按一次提取按钮就可以工作,然后希望它在点击rfid标签时继续在tableView中显示数据。

这是我的控制器代码:

public class detectController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<detectBean> tableView;

    @FXML
    private TextField txtSTID;


    ObservableList<detectBean> list;

    public static SerialPort s1;
    static String temp="";
    static String temp1="";

    static void doAlert(String msg)
    {
        Alert alert=new Alert(AlertType.INFORMATION);
        alert.setTitle("Alert..");
        alert.setContentText(msg);
        alert.show();
    } 


    ObservableList<detectBean> getRecordsFromTableSome(String sID) throws FileNotFoundException
    {
         list=FXCollections.observableArrayList();

        try {
            pst=con.prepareStatement("select * from stuRegis where studentID=?");
            pst.setString(1, sID);

            ResultSet rs=  pst.executeQuery();
            while(rs.next())
            {
                String studentID1=rs.getString("studentID");
                String studentID = studentID1.substring(1);
                String name=rs.getString("name");
                String sroll=rs.getString("sroll");
                String clas=rs.getString("clas");
                String fname=rs.getString("fname");
                String contact=rs.getString("contact");
                String pic = rs.getString("pic");
                FileInputStream photo=new FileInputStream(pic);
                Image image1 = new Image(photo, 100, 100, false, false);
                Date dob=rs.getDate("dob");
                String dobb=dob.toString();
                 Date dos=rs.getDate("dos");
                    String doss=dos.toString();
                detectBean bean=new detectBean(studentID, name, sroll, clas, fname, contact, new ImageView(image1), dobb, doss );
                list.add(bean);
            }

            } 
        catch (SQLException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;

  }
    ObservableList<detectBean> getRecordsFromTable() throws FileNotFoundException
    {
         list=FXCollections.observableArrayList();

        try {
              pst=con.prepareStatement("select * from stuRegis");

            ResultSet rs=  pst.executeQuery();
            while(rs.next())
            {
                String studentID1=rs.getString("studentID");
                String studentID = studentID1.substring(1);
                String name=rs.getString("name");
                String sroll=rs.getString("sroll");
                String clas=rs.getString("clas");
                String fname=rs.getString("fname");
                String contact=rs.getString("contact");
                String pic = rs.getString("pic");
                FileInputStream photo=new FileInputStream(pic);
                 Date dob=rs.getDate("dob");
                    String dobb=dob.toString();
                 Date dos=rs.getDate("dos");
                    String doss=dos.toString();

                Image image1 = new Image(photo, 100, 100, false, false);
                detectBean bean=new detectBean(studentID,  name, sroll, clas, fname, contact, new ImageView(image1), dobb, doss);
                list.add(bean);
            }

            } 
        catch (SQLException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;

    } 

    @FXML
    void doDelete(ActionEvent event) {

        int selectedIndex = tableView.getSelectionModel().getSelectedIndex();


        detectBean asm = (detectBean)tableView.getSelectionModel().getSelectedItem();
        String selectedItem = asm.getStudentID();


        if (selectedIndex >= 0) {

             try {
                pst = con.prepareStatement("DELETE FROM stuRegis WHERE studentID = ?");
                pst.setString(1, selectedItem);
                pst.execute();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
             tableView.getItems().remove(selectedIndex);

        } else {
            doAlert("Record deleted successfully");
        }
    }

    @FXML
    void doExcel(ActionEvent event) {

        try {
            writeExcel();
            doAlert("Exported to excel..");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void writeExcel() throws Exception {
        Writer writer = null;
        try {
            FileChooser chooser=new FileChooser();

            chooser.setTitle("Select Path:");

            chooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("All Files", "*.*")

                );
             File file=chooser.showSaveDialog(null);
            String filePath=file.getAbsolutePath();
            if(!(filePath.endsWith(".csv")||filePath.endsWith(".CSV")))
            {
                doAlert("The File name must have .csv extension");
                return;
            }
             file = new File(filePath);



            writer = new BufferedWriter(new FileWriter(file));
            String text="Student ID, Name, Roll No., Class, Father's Name, Contact No., Date of Birth, Date of Admission\n";
            writer.write(text);
            for (detectBean p : list)
            {
                text =  p.getStudentID()+ "," +p.getName()+ "," + p.getSroll()+ "," + p.getClas()+ "," + p.getFname()+ "," + p.getContact()+"," + p.getDob()+"," + p.getDos()+"\n";
                writer.write(text);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {

            writer.flush();
             writer.close();
        }
    }

    /////////////////////////////////////////////////////

    @FXML
    void doFetch(ActionEvent event) throws IOException{
        //while(true){
        String a = recall();
        txtSTID.setText(a.substring(1));
        ObservableList<detectBean> list=getRecordsFromTableSome(a);

        //tableView.setItems(list);
        tableView.getItems().addAll(list);
        //}
    }

    @FXML
    void doReset(ActionEvent event) {
        txtSTID.setText("");
        tableView.getItems().clear();
    }

    @FXML
    void doFetchAll(ActionEvent event)throws FileNotFoundException {
        ObservableList<detectBean> list=getRecordsFromTable();
        tableView.setItems(list);
    }

    @FXML
    void doComClose(ActionEvent event) {

        if(s1.closePort()){
            doAlert("Port Closed");
            System.out.println("Port closed successFully");
        }else{
            doAlert("Failed to Close Port");
            System.out.println("Failed to close port");
        }
    }

    @FXML
    void doOpenCom(ActionEvent event) {

        port();
    }
    /////////////////////////////////////////////////////

    public static void port() 
    {
        SerialPort[] s=SerialPort.getCommPorts();
        for(SerialPort port:s){
            System.out.println(""+port.getSystemPortName());
            s1=SerialPort.getCommPort(port.getSystemPortName());
            if(s1.openPort()){
                doAlert("Port Opened");
                System.out.println("Port opened successFully ");
            }else{
                doAlert("Failed to Open Port");
                System.out.println("Failed to open port");
            }
        }
        s1.setBaudRate(9600);
      s1.n



    }

    public static String recall() throws IOException
    {   
InputStream is=s1.getInputStream();


        StringBuilder st = new StringBuilder();
        for(int i=0,x=0;true;i++){
    //for (int i =0;i<11;i++){


        st=st.append((char)is.read());

       temp1=st.toString();

       if(temp1.length()==13)
       { System.out.print(temp1);
         //System.out.print(temp);
         //System.out.print(temp1.length());
           break;
       }
       System.out.print(temp1);


}

//System.out.print(""+(char)is.read());



    temp=temp1.substring(0,11);
    System.out.print(temp.length());
    System.gc();
    return temp;



    }



    //////////////////////////////////////////////////////

    PreparedStatement pst;
    Connection con;

    @FXML
    void initialize() throws IOException, FileNotFoundException {

        con=MysqlConnection.doConnect();


        TableColumn<detectBean, String> studentID=new TableColumn<detectBean, String>("Student ID");//Dikhava Title
        studentID.setCellValueFactory(new PropertyValueFactory<>("studentID"));//bean field name
        studentID.setMinWidth(110);

        TableColumn<detectBean, String> name=new TableColumn<detectBean, String>("Name");//Dikhava Title
        name.setCellValueFactory(new PropertyValueFactory<>("name"));//bean field name

        TableColumn<detectBean, String> sroll=new TableColumn<detectBean, String>("Roll No.");//Dikhava Title
        sroll.setCellValueFactory(new PropertyValueFactory<>("sroll"));//bean field name

        TableColumn<detectBean, String> clas=new TableColumn<detectBean, String>("Class");//Dikhava Title
        clas.setCellValueFactory(new PropertyValueFactory<>("clas"));//bean field name

        TableColumn<detectBean, String> fname=new TableColumn<detectBean, String>("Father's Name");//Dikhava Title
        fname.setCellValueFactory(new PropertyValueFactory<>("fname"));//bean field name
        fname.setMinWidth(110);

        TableColumn<detectBean, String> contact=new TableColumn<detectBean, String>("Contact No.");//Dikhava Title
        contact.setCellValueFactory(new PropertyValueFactory<>("contact"));//bean field name
        contact.setMinWidth(90);

        TableColumn<detectBean, Image> pic=new TableColumn<detectBean, Image>("Image");//Dikhava Title
        pic.setCellValueFactory(new PropertyValueFactory<>("pic"));//bean field name
        pic.setMinWidth(110);  

        TableColumn<detectBean, String> dob=new TableColumn<detectBean, String>("Date of Birth");
        dob.setCellValueFactory(new PropertyValueFactory<>("dob"));//field name in bean
        dob.setMinWidth(140); 

        TableColumn<detectBean, String> dos=new TableColumn<detectBean, String>("Date of Admission");
        dos.setCellValueFactory(new PropertyValueFactory<>("dos"));//field name in bean
        dos.setMinWidth(140); 

        tableView.getColumns().clear();
        tableView.getColumns().addAll(studentID,name,sroll,clas,fname,contact,pic, dob, dos);








    }
}

0 个答案:

没有答案