serial port java program for RFID reader - code almost working

I have a project for RFID.
I have attached the RFID and it works fine with the computer. There was a sample program provided with it to make it read the tag and is working fine. It is the RFID Demo V1.0.0 and is reading the tag's broadcast ID '2Kw4' which is repeated ~1sec. I want to create a java program that does the same thing... basically poll the serial port. (which has a serial to USB converter as I am using a laptop if that makes any difference). I have tailered the code from this website http://www.captain.at/howto-java-serial-port-javax-comm-rxtx.php to just poll the port that the RFID connects to. However when I run it the result is distorted as you can see in the General Output screen in JCreater in the screenshot. Instead of it being '2Kw4 2Kw4 2Kw4 2Kw4', it is outputing '2K222K2K2w4 222 22K2'.
Any suggestions to make it work the same as the RFID Demo V1.0.0 as per the screenshot.
import java.util.Scanner;
import java.io.*;
import java.util.*;
import gnu.io.*; // for rxtxSerial library
 
public class ReadRFIDPort implements SerialPortEventListener {
   static CommPortIdentifier portId;
   static CommPortIdentifier saveportId;
   static Enumeration        portList;
   InputStream           inputStream;
   SerialPort           serialPort;
   
 
   public static void main(String[] args) {
      boolean           portFound = false;
      String           rfidPort;
      Scanner input = new Scanner (System.in);
      System.out.println("Please enter the port here");
      rfidPort = input.next();
      System.out.println("Set default port to "+ rfidPort);
      
		// parse ports and if the default port is found, initialized the reader
      portList = CommPortIdentifier.getPortIdentifiers();
      while (portList.hasMoreElements()) {
         portId = (CommPortIdentifier) portList.nextElement();
         if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals(rfidPort)) {
               System.out.println("Found port: "+rfidPort);
               portFound = true;
               
               ReadRFIDPort reader = new ReadRFIDPort();
            } 
         } 
         
      } 
      if (!portFound) {
         System.out.println("port " + rfidPort + "not found.");
      } 
      
   } 
 
   public ReadRFIDPort() {
      // initalize serial port
      try {
         serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
      } catch (PortInUseException e) {}
   
      try {
         inputStream = serialPort.getInputStream();
      } catch (IOException e) {}
   
      try {
         serialPort.addEventListener(this);
      } catch (TooManyListenersException e) {}
      
      // activate the DATA_AVAILABLE notifier
      serialPort.notifyOnDataAvailable(true);
   
      try {
         // set port parameters
         serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
                     SerialPort.STOPBITS_1, 
                     SerialPort.PARITY_NONE);
      } catch (UnsupportedCommOperationException 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:
         // we get here if data has been received
         byte[] readBuffer = new byte[40];
         try {
            // read data
            while (inputStream.available() > 0) {
               int numBytes = inputStream.read(readBuffer);
            } 
            // print data
            String result  = new String(readBuffer);
            System.out.println(result + "      ");
                       
         } catch (IOException e) {}
   
         break;
      }
   } 
 
}

Open in new window

screener.jpg
LVL 1
jtiernan2008Asked:
Who is Participating?
 
CEHJCommented:
Try it like this and tell us what it prints:
// read data
while (inputStream.available() > 0) {
	System.out.print((char)inputStream.read());
} 

Open in new window

0
 
jtiernan2008Author Commented:
Hi CEHJ,

thanks and nice try however exact same style output...
I think it the issue is that the reading and writing to the console screen is not synchronised and that it only reads part if it then writes only that part... but I thought the process should be fast enough to work!
0
 
jtiernan2008Author Commented:
But wait..... I didnt delete the second part...
put it in this time and it works perfect!!!!!
CEHJ.... there is a reason you are top of the list!

thanks a million!
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
jtiernan2008Author Commented:
Is there any chance, you can explain the code snippet you provided and why mine wasnt working?
0
 
jtiernan2008Author Commented:
Very quick!!! and had no access to the hardware!!
0
 
CEHJCommented:
Well my code simply reads one byte at a time so doesn't have to worry about where in the buffer is getting filled. Filling in the wrong place could cause the 'distortion' you mentioned. It would probably be better to do
// read data
int buf = -1;
while ((buf = inputStream.read()) > -1) {
	System.out.print((char)buf);
} 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.