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 4 of 4

Thread: Serial Programming Logic Error???

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Serial Programming Logic Error???

    Codes for SerialReadByEvents

    /*
     * SerialReadByEvents.java
     *
     * Created on September 22, 2011, 1:21 PM
     *
     * To change this template, choose Tools | Options and locate the template under
     * the Source Creation and Management node. Right-click the template and choose
     * Open. You can then make changes to the template in the Source Editor.
     */
     
    /**
     *
     */
    import java.awt.*;
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
    /**
     * Read from a Serial port, notifying when data arrives.
     * Simulation of part of an event-logging service.
     */
    public class SerialReadByEvents extends CommPortOpen
            implements SerialPortEventListener {
        public static void main(String[] argv)
        throws IOException, NoSuchPortException, PortInUseException,
                UnsupportedCommOperationException {
            new SerialReadByEvents(null).converse();
        }
        /* Constructor */
        public SerialReadByEvents(Frame f)
        throws IOException, NoSuchPortException, PortInUseException,
                UnsupportedCommOperationException {
            super(f);
     
        }
     
        protected BufferedReader ifile;
        byte ans[] = new byte[255];
        /**
         * Hold the conversation.
         */
        protected void converse() throws IOException {
            if (!(thePort instanceof SerialPort)) {
                System.err.println("But I wanted a SERIAL port!");
                System.exit(1);
            }
    // Tell the Comm API that we want serial events.
            ((SerialPort)thePort).notifyOnDataAvailable(true);
            try {
                ((SerialPort)thePort).addEventListener(this);
            } catch (TooManyListenersException ev) {
    // "CantHappen" error
                System.err.println("Too many listeners(!) " + ev);
                System.exit(0);
            }
    // Make a reader for the input file.
            ifile = new BufferedReader(new InputStreamReader(is));
    //
        }
        int i=0;
        public void serialEvent(SerialPortEvent ev) {
     
            String line;
            try {
                is = new DataInputStream(thePort.getInputStream());
                ifile = new BufferedReader(new InputStreamReader(is));
                System.out.println(ifile.read()+" dfvvvvvvft "+ (i++));
     
                } catch (IOException e) {
    System.err.println("Read failed");
                    System.exit(1);
                }
     
             catch (IOException ex) {
                System.err.println("IO Error " + ex);
            }
     
        }
    }

    my error is the java program will only get the input from the serial port the first time, all subsequent inputs are ignored and was not displayed on the console.. need help on this.. thanks...


  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: Serial Programming Logic Error???

    A BufferedReader decouples reads of the source of data from reads by the consumer, so when you .read() on a BufferedReader, it'll return only one byte to you, but may read much more from the source - up to the size of its internal buffer. You create a new BufferedReader on every event, each one of which may read more from the serial port than you ask them to with read()...

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Serial Programming Logic Error???

    now my code has a new logic error...

    switch (ev.getEventType())
           {
               case SerialPortEvent.DATA_AVAILABLE:
                   try {
                is = new DataInputStream(thePort.getInputStream());
                ifile = new BufferedReader(new InputStreamReader(is));
                c= ifile.read();
               while (c>=0)
               {
                   test=(char)c;
                    line =line +test;
                     System.out.println(line +" dfvvvvvvft "+ (i++));
     
                    System.out.println("sux v3");
                    c=ifile.read();
     
               }
             total = total +(Double.parseDouble(line));
               System.out.println("Total" +total);               
     
     
            } catch (IOException ex) {
                System.err.println("IO Error " + ex);
            }
                   break;
     
               default:
                   break;
     
           }

    it did not go the line the add the total and print out the current total amount.. it just exit the try block and preparing itself for the next input..

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

    Default Re: Serial Programming Logic Error???

    You're not showing us enough code - like where 'c' is declared, for example. I suspect that unless you've properly fixed your initial logic problem that you have a race condition. Try declaring c inside the try block. You don't need it outside, so it's only good practice to limit your variable scope to as small a code block as possible.

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, 11:53 AM
  2. java programming error
    By priya_501 in forum Member Introductions
    Replies: 2
    Last Post: May 3rd, 2011, 11:15 AM
  3. Serial Bluetooth Programming
    By blackridinghood in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: January 28th, 2011, 11:15 AM
  4. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  5. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM