0

我正在为我的在线预订系统构建一个小程序。工作人员可以在我的系统后端添加资金并进行交易。他们通过安全网站与系统通信。当他们进行交易时,他们被要求输入他们的 8 个字符的个人代码。我想构建一个小程序,从我已有的阅读器中读取 RFID 芯片。

阅读器通过 USB 连接到计算机,Windows 将此 USB 重定向到 COM 端口。

当我使用以下代码扫描一个时,我已经能够构建一个读取芯片内容的 Java 类:

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;
    }

}

}

到目前为止,一切都很好。但是当我在一个小程序中实现这个类时,我得到了异常:

Caught java.lang.NullPointerException: name can't be null while loading driver com.sun.comm.Win32Driver

我搜索了互联网,发现可以通过将其添加到小程序的初始化代码中来使其在 Eclipse 中工作:

System.setSecurityManager(null);

但是,当我在浏览器中运行小程序时,我收到以下通知:

来自浏览器中 Java 小程序的通知/异常

找不到此问题的解决方案。

4

0 回答 0