java - Calling in Child class to wait for RFID Tag -
so after long period of research, able make rfid scanner work , detect ports of computer. had split code 2 class files because of 2 jar files having different features:
one reading id , other reading port.
now had them, had call them main gui project. issue facing right child wont wait id scanned , instead give me null value in return. want make work can call child classes main project.
here 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 redundant , willing remove it. public void checkconnection(){ rfid_scan_hw jcom = new rfid_scan_hw(); serialport = new serialport(jcom.collect_ports("")); startreading(); } //configuring serialport public void startreading(){ try { serialport.openport(); serialport.setparams(serialport.baudrate_9600,serialport.databits_8,serialport.stopbits_1,serialport.parity_none); //verbose, output 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 device not found, 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 input 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 how called them using 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 result: the id is: null
now here wanted happen.
when press button, button wait scanner before prompting id card.
i'm pretty sure i'm doing wrong please me out.
use threads , synchronize them using synchronized keywords. 1st thread wait connection established, id scanned , available. notifies 2nd thread read/write data rfid device.
also consider using serial communication manager library has many powerful apis may used in project. share details rfid hardware.
Comments
Post a Comment