java - JAVA中基于RFID的读写

标签 java rfid

我必须制作一个基于 RFID 的考勤系统,一旦从标签中读取并找出它是哪个学生,我必须写入数据库。我的问题是如何从 RFID 标签获取输入。输入缓冲区是否提供的 JAVA 类足以用于输入和输出,还是我必须采用不同的方法。

最佳答案

为此,您可以使用 Java Communications API。我正在做同样的工作,java comm api 和 rxtx 对此工作得很好。我有一个为此编写的程序。给你:

import java.io.*;

import java.util.*;

import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM13")) {
            //                if (portId.getName().equals("/dev/term/a")) {
                    SimpleRead reader = new SimpleRead();
                 }
            }
        }
    }

    public SimpleRead() {
        try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {System.out.println(e);}
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {System.out.println(e);}
        try {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
            System.out.println(e);
        }
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            System.out.println(e);
        }
        readThread = new Thread(this);
        readThread.start();
    }

    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {System.out.println(e);}
    }

    public void serialEvent(SerialPortEvent event) {
        switch(event.getEventType()) {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
            case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[20];

            try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);
                }
                System.out.print(new String(readBuffer));
            } catch (IOException e) {System.out.println(e);}
            break;
        }
    }
}

关于java - JAVA中基于RFID的读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12159215/

相关文章:

java - 使用 Java 更快地检索 SQL 数据并搜索大数据

java - Android 上带有 JMRTD 的外部 RFID 阅读器

javascript - 如何防止以html形式模仿RFID读卡器?

c - RFID:TMS3705 RFid 应答器基站 IC 和 TMS37157 RFid 设备之间的通信应使用多少 PowerBurst 时间?

java - 套接字服务器最佳方法

java - 在 Java 中使用 Selenium 选择下拉值

Android NFC 读取 ISO15693 RFID 标签

python - 使用 Adafruit PN532 库写入 RFID 标签内存时字节数组被截断

java - 三合会不出现战斗? (Java Set 缺少一项)

java - eclipse 可视化编辑器