I am building an applet for my online reservation system. The staff members can add money and do transactions at the back end of my system. They communicate with the system via a secured website. When they do a transaction, they are asked to input their personal code of 8 characters. I want to build an applet that reads an RFID chip from a reader I already have.
The reader is connected to the computer via USB and Windows redirects this USB to a COM port.
I have been able to build an Java class that reads in the contents of the chip when I scan one with the following code:
package model;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Random;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import view.Program;
public class Reader implements Runnable, SerialPortEventListener {
private Program program;
CommPortIdentifier portId;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public Reader (Program program, CommPortIdentifier portId) {
System.out.println("");
this.program = program;
this.portId = portId;
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {
e.printStackTrace();
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
readThread = new Thread(this);
readThread.start();
}
@Override
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
@Override
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
System.out.println("Carrier detected;");
break;
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("Output buffer empty;");
break;
case SerialPortEvent.DATA_AVAILABLE:
try {
Random ran = new Random();
int random = ran.nextInt();
while (inputStream.available() > 0) {
//System.out.println("Data available: " + inputStream.read());
//int inputByte = inputStream.read();
//byte inputByte2 = (byte) inputByte;
System.out.println("Random " + random + " en resultaat " + Integer.toString(inputStream.read()));
String id = Integer.toString(inputStream.read());
program.setId(id);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
So far so good. But when I implement the class inside an applet I get the exception:
Caught java.lang.NullPointerException: name can't be null while loading driver com.sun.comm.Win32Driver
I searched the internet and I found that I can get it working in Eclipse by adding this to the init code of the applet:
System.setSecurityManager(null);
However when I run the applet inside the browser I get the following notification:
Can't find the solution to this problem.