2012-08-28 32 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非常簡單。