2012-08-28 33 views
2

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

回答

3

您可以为此使用Java通信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; 
     } 
    } 
} 
+0

能否请您更详细地解释您的答案。 – Luffy

+1

@Luffy很抱歉回到这么晚。这里有一个链接可以用来更详细地了解它。我已经使用了java通信API来进行串口之间的通信。 http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/javax/comm/package-summary.html –

1

它从那以后 一直是过去几年我曾与RFID(ThinkMagic阅读器)和Java,但每次在当时使用的读者我 有其自己的专有API。 只有在购买阅读器的情况下才能访问JAR。 时代可能已经改变,可能是开源阅读器/实现,但请查看当前品牌和型号的 供应商网站。一旦你有权访问它们,这些API非常简单。