java - JSSC - Adding SerialPortEventListener to JFrame -


i need on how eventlisteners work in java. i'll honest, listener know how use actionlisteners forgive me not knowing how. start with, put on reference.

im working on rfid scanner desktop app on java , epoch gave me this: jssc

the jssc able provide me tools need read rfid time, i'm having issued on implementing on application.

here current code:

first, imports

import jssc.serialport; import jssc.serialportevent; import jssc.serialporteventlistener; import jssc.serialportexception; 

now class:

class serialportreader implements serialporteventlistener {         serialport serialport;         @override         public void serialevent(serialportevent event) {             if(event.isrxchar()){//if data available                 if(event.geteventvalue() == 10){//check bytes count in input buffer                     //read data, if 10 bytes available                      try {                         byte buffer[] = serialport.readbytes(10);                     }                     catch (serialportexception ex) {                         system.out.println(ex);                     }                 }             }else if(event.iscts()){//if cts line has changed state                 if(event.geteventvalue() == 1){//if line on                     system.out.println("cts - on");                 }                 else {                     system.out.println("cts - off");                 }             }else if(event.isdsr()){///if dsr line has changed state                 if(event.geteventvalue() == 1){//if line on                     system.out.println("dsr - on");                 }else {                     system.out.println("dsr - off");                 }             }         }     }  public class testrf extends javax.swing.jframe implements serialporteventlistener{     serialport serialport;      public testrf() {         initcomponents();         setserialport();      }      public void setserialport(){         serialport serialport = new serialport("com7");         try {             serialport.openport();//open serial port             serialport.setparams(9600, 8, 1, 0);//set params.             byte[] buffer = serialport.readbytes(10);//read 10 bytes serial port             //serialport.closeport();//close serial port             serialport.addeventlistener(this);//add serialporteventlistener         }         catch (serialportexception ex) {             system.out.println(ex);         }     } 

and lastly, abstract method.. (i dont know honest , im picking books right now.)

@override     public void serialevent(serialportevent spe) {         throw new unsupportedoperationexception("not supported yet."); //to change body of generated methods, choose tools | templates.     } 

to put things simple. need rfid read , pass value anywhere in project. thought of trying extend jframe classes class file far, no luck. tutorials anyone?

you can create 2 threads. 1st thread read data serial port (rfid) while 2nd thread handles task specific application. consider using serial communication manager library serial port communication in java.

please share rfid hardware details can think best application design use it.


Comments