所以经过长时间的研究,我能够让我的 RFID 扫描仪工作并检测到我电脑的端口。由于两个 jar 文件具有不同的功能,我不得不将代码拆分为 2 个类文件:
一个用于读取 ID,另一个用于读取端口。
现在我有了它们,我所要做的就是将它们调用到我的主要 GUI 项目中。我现在面临的问题是孩子不会等待 ID 被扫描,而是给我一个空值作为回报。我想完成这项工作,这样我就可以将我的子类调用到我的主项目中。
这是我的代码:
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());
}
}
}
现在是我使用按钮调用它们的方式:
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);
}
结果:The ID is: null
现在这就是我想要发生的事情。
当我按下按钮时,按钮将等待扫描仪从卡中提示 ID。
我很确定我做错了,所以请帮帮我。