Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reading wrong value from serial port

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading wrong value from serial port

    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:
    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
    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.

    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;
            }
        }
        }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Reading wrong value from serial port

    You're doing buffered IO, so you can't usually depend on reading complete transfers from your source. InputStream.available() tells you that *some* bytes are available, and you read them, emptying the buffer quicker than your source fills it. You hit the end of the buffer, available() returns false, and then your code prints the bytes-so-far followed by a newline. Your code needs to build up an internal buffer somehow of bytes that are marked by an end-of-transmission marker sent by the source. Looking at your output, I suspect that the source is sending its own newline. Try adding another println() that uses something like Arrays.toString(readBuffer) to see what individual bytes are being sent by the source.

    Sorry if my reply is too late, I just joined this forum and the other members are so efficient, I had to look back a long way to find an unanswered question :p

Similar Threads

  1. Serial Port Programming(detecting 5V signal from USB)
    By abhishekshishodia in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: June 24th, 2011, 12:53 PM
  2. Serial comm reading freeze
    By java_dude in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 10th, 2011, 02:27 PM
  3. reading stream of bytes from serial port
    By KrisTheSavage in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 3rd, 2011, 11:02 AM
  4. Relay data from remote port to local port
    By chegers in forum Java Networking
    Replies: 0
    Last Post: November 7th, 2010, 12:46 PM
  5. theory about serial / parallel port & java
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: January 4th, 2010, 10:08 PM

Tags for this Thread