I have to read the 12-digit tag number from an RFID reader and print it to console. When I use this program to read the tag I get some weird spacing in between.
E.g. My tag number is 4400E6EF1A57. When I keep scanning this tag, the console window shows the following:
It appears to be that there is a long string of 0's and 1's that gets read in and only a few of those are the actual tag IDs. I don't know in what order I am reading these 0's and 1's.4400E6EF1
A57
4400E
6EF1A57
4400E6EF1A57
4400E6EF1
A57
4400E6EF1A57
4400
E6EF1A57
4
400E6EF1A57
4400E6EF1A
57
4
400E6EF1A57
4400E6EF1A5
7
4400E6EF1A57
4400E6EF1
A57
4400E6EF1A57
4400E
6EF1A57
4400
E6EF1A57
4400E6EF1A57
4400E6EF1A57
4400E6EF
1A57
4400E
6EF1A57
Here is my code: (Some SQL and JDBC stuff incorporated, can ignore)
import java.io.*; import java.util.*; import gnu.io.*; import java.sql.*; public class trying5 implements Runnable, SerialPortEventListener { static Enumeration portList; static CommPortIdentifier portId; SerialPort serialPort; InputStream inputStream; Thread readThread; Connection con; 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("COM3")) { trying5 reader = new trying5(); } } } } public trying5() { try { serialPort = (SerialPort) portId.open("trying5Application", 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]; // print to console try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); } String newtuple = new String(readBuffer); usercon newcon = new usercon(con, newtuple); System.out.print(newtuple + "\n"); } catch (IOException e) { System.out.println(e); } break; } } }