RFID Based Attendence System
It Require Following jar file(jSSC.jar):-
https://code.google.com/p/java-simple-serial-connector/downloads/detail?name=jSSC-2.6.0-Release.zip&can=2&q=
It Require Following jar file(jSSC.jar):-
https://code.google.com/p/java-simple-serial-connector/downloads/detail?name=jSSC-2.6.0-Release.zip&can=2&q=
Getting serial ports names
import jssc.SerialPortList; public class Main { public static void main(String[] args) { String[] portNames = SerialPortList.getPortNames(); for(int i = 0; i < portNames.length; i++){ System.out.println(portNames[i]); } } }
Writing data to serial port
import jssc.SerialPort; import jssc.SerialPortException; public class Main { public static void main(String[] args) { SerialPort serialPort = new SerialPort("COM1"); try { serialPort.openPort();//Open serial port serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0); serialPort.writeBytes("This is a test string".getBytes());//Write data to port serialPort.closePort();//Close serial port } catch (SerialPortException ex) { System.out.println(ex); } } }
Reading data from serial port
import jssc.SerialPort; import jssc.SerialPortException; public class Main { public static void main(String[] args) { SerialPort serialPort = new SerialPort("COM1"); try { serialPort.openPort();//Open serial port serialPort.setParams(9600, 8, 1, 0);//Set params. byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port serialPort.closePort();//Close serial port } catch (SerialPortException ex) { System.out.println(ex); } } }
Event mask & SerialPortEventListener interface
Note: The mask is an additive quantity, thus to set a mask on the expectation of the arrival of Event Data (MASK_RXCHAR) and change the status lines CTS (MASK_CTS), DSR (MASK_DSR) we just need to combine all three masks.
import jssc.SerialPort; import jssc.SerialPortEvent; import jssc.SerialPortEventListener; import jssc.SerialPortException; public class Main { static SerialPort serialPort; public static void main(String[] args) { serialPort = new SerialPort("COM1"); try { serialPort.openPort();//Open port serialPort.setParams(9600, 8, 1, 0);//Set params int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask serialPort.setEventsMask(mask);//Set mask serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener } catch (SerialPortException ex) { System.out.println(ex); } } /* * In this class must implement the method serialEvent, through it we learn about * events that happened to our port. But we will not report on all events but only * those that we put in the mask. In this case the arrival of the data and change the * status lines CTS and DSR */ static class SerialPortReader implements SerialPortEventListener { public void serialEvent(SerialPortEvent event) { if(event.isRXCHAR()){//If data is available if(event.getEventValue() == 10){//Check bytes count in the 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 is 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 is ON System.out.println("DSR - ON"); } else { System.out.println("DSR - OFF"); } } } } }
---------------------------------------------*************-------------------------------------
REFERNCE :-
https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_example
No comments:
Post a Comment