简体   繁体   English

召唤儿童班等待RFID标签

[英]Calling in Child class to wait for RFID Tag

So after a long period of research, I was able to make my RFID scanner work and detect the ports of my computer. 因此,经过长时间的研究,我能够使RFID扫描仪正常工作并检测计算机的端口。 I had to split the code into 2 class files because of two jar files having different features: 由于两个具有不同功能的jar文件,我不得不将代码分成2个类文件:

one is for reading the ID and the other is for reading the port. 一个用于读取ID,另一个用于读取端口。

Now that I had them, all I had to do is to call them into my main GUI project. 现在有了它们,我要做的就是将它们调用到我的主GUI项目中。 The issue I am facing right now is that the child wont wait for the ID to be scanned and instead give me a null value in return. 我现在面临的问题是孩子不会等待ID扫描,而是给我一个空值作为回报。 I want to make this work so I can just call my child classes into my Main Project. 我想做这项工作,所以我可以将我的孩子班级称为“主项目”。

here are my codes: 这是我的代码:

RFID_Reader.java RFID_Reader.java

import javax.swing.JOptionPane;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

public class RFID_Reader {
    static SerialPort serialPort;
    static String output;
    public String FinalOutput;

    //this probably is redundant and I am willing to remove it.
    public void checkConnection(){
        RFID_Scan_HW jCom = new RFID_Scan_HW();
        serialPort = new SerialPort(jCom.collect_Ports(""));
        startReading();
    }

    //Configuring the serialPort
    public void startReading(){
        try {
            serialPort.openPort();
            serialPort.setParams(SerialPort.BAUDRATE_9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
            //verbose, just to get the output with no words.
            serialPort.writeBytes("\002v0\003".getBytes());
            serialPort.closePort();

            serialPort.openPort();
            serialPort.setParams(9600, 8, 1, 0);
            int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;
            serialPort.setEventsMask(mask);
            serialPort.addEventListener(new SerialPortReader());
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

    //re-scan devices in port. if the device is not found, just try again.
    public void rescanConnection(){
        RFID_Scan_HW jCom = new RFID_Scan_HW();
        if(jCom.collect_Ports("")==""){
            JOptionPane.showMessageDialog(null, "No Scanner found. Please try again");
        }else{
            serialPort = new SerialPort(jCom.collect_Ports(""));
            startReading();
        }
    }

    //read the input from the device.
    class SerialPortReader implements SerialPortEventListener{
        @Override
        public void serialEvent(SerialPortEvent event) {           
            if(event.isRXCHAR()){
                if(event.getEventValue() == 22){
                    try{
                        byte[] bytes = serialPort.readBytes(22);
                        String card = new String(bytes);
                        String results[] = card.split(",");

                        String processed ="";
                        char[] cutdown = results[3].toCharArray();
                        for(int i=0; i<cutdown.length-1; i++){
                            processed +=cutdown[i];
                        }

                        String result = results[2]+"-"+processed;
                        FinalOutput = result;
                    }catch (SerialPortException ex) {
                        System.out.println(ex);
                    }
                }else{

                }
            }
        }   
    }

}

RFID_Scan_HW.java RFID_Scan_HW.java

import com.fazecast.jSerialComm.SerialPort;

public class RFID_Scan_HW {
    String masterPort = "";
    public String collect_Ports(String x){
        SerialPort ports[] = SerialPort.getCommPorts();
        String[] portList = new String[ports.length];
        for(int i=0; i<ports.length; i++){
            String check = ports[i].getDescriptivePortName();
            if(check.startsWith("Prolific USB-to-Serial Comm Port")==true){
                masterPort = ports[i].getSystemPortName();
            }
        }
        return masterPort;
    }
    public void displayPorts(){
        SerialPort ports[] = SerialPort.getCommPorts();
        for(SerialPort port : ports){
            System.out.println(port.getDescriptivePortName());
        }
    }
}

And here now is how I called them using a Button: 现在是我使用Button称呼他们的方式:

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        RFID_Reader rf = new RFID_Reader();
        String ID="null";
            rf.checkConnection();
            ID = rf.FinalOutput;
        JOptionPane.showMessageDialog(null, "The ID is: "+ID);
    } 

and the result: The ID is: null 结果为: The ID is: null

now here is what I wanted to happen. 现在这就是我想要发生的事情。

When I press the button, the button will wait for the scanner before prompting the ID from the card. 当我按下按钮时,该按钮将等待扫描仪,然后提示卡中的ID。

I'm pretty sure I'm doing this wrong so please help me out. 我敢肯定我做错了,所以请帮帮我。

Use threads and synchronize them using synchronized keywords. 使用线程并使用同步的关键字同步它们。 1st thread will wait for connection to be established, ID to be scanned and available. 第一个线程将等待建立连接,ID待扫描且可用。 Then it notifies 2nd thread which will read/write data to RFID device. 然后通知第二线程将读取/写入数据到RFID设备。

Also consider using serial communication manager library as it has many powerful APIs that may be used as is in your project. 还应考虑使用串行通信管理器库,因为它具有许多功能强大的API,可以在您的项目中直接使用它们。 Also share details about your RFID hardware. 还共享有关您的RFID硬件的详细信息。

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

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