简体   繁体   中英

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. I had to split the code into 2 class files because of two jar files having different features:

one is for reading the ID and the other is for reading the port.

Now that I had them, all I had to do is to call them into my main GUI project. 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. 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

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

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:

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

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.

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. Then it notifies 2nd thread which will read/write data to RFID device.

Also consider using serial communication manager library as it has many powerful APIs that may be used as is in your project. Also share details about your RFID hardware.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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