Results 1 to 4 of 4
  1. #1
    n3p3nthess is offline Member
    Join Date
    Jul 2012
    Location
    Borgloon, Belgium
    Posts
    4
    Rep Power
    0

    Default Java Serial Communications using RXTX

    Hello, I'm sorry if this post is not in the right place, I'm new to this forum.

    I am having a little problem sending data over a serial port to an ARM chip.

    The following code is my (from the RXTX website) SerialWriter class.
    With this, everything is working fine. This version reads the data from standard input.
    The ARM chip replies with "DATA RECIEVED".

    When I modify the class to just send a meaningless char, it stops working.
    There are no errors, but the command is just not send.



    Working version:
    Java Code:
    package com.codegrasp.arduinoSerialConnection;
    
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class SerialWriter implements Runnable {
    	private OutputStream out;
    
    	public SerialWriter(OutputStream out) {
    		this.out = out;
    	}
    
    	public void run() {
    
    		try {
    			int c = 0;
    			while ((c = System.in.read()) > -1) {
    				this.out.write(c);
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    modified version (not working):
    Java Code:
    package com.codegrasp.arduinoSerialConnection;
    
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class SerialWriter implements Runnable {
    	private OutputStream out;
    
    	public SerialWriter(OutputStream out) {
    		this.out = out;
    	}
    
    	public void run() {
    
    		try {
    			int c = 0;
    			while (c < 10) {
    				this.out.write('a');
                                    c++;
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Serial Communications using RXTX

    How does the device on the other side of the wire know that you are ready sending? In your first example you are probably sending a \r and \n at the end of transmission but you don't do anything like that in your second class and the device may still be waiting for more to come.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    n3p3nthess is offline Member
    Join Date
    Jul 2012
    Location
    Borgloon, Belgium
    Posts
    4
    Rep Power
    0

    Default Re: Java Serial Communications using RXTX

    Jeah, that was also my first thought.
    But when (in the working example), I change the line:
    Java Code:
    this.out.write(c);
    to:
    Java Code:
    this.out.write('a');
    The code still works, I still get a message back from the ARM chip.

    To me it looks like the code stops working when I remove the "System.in.read()" line.

  4. #4
    n3p3nthess is offline Member
    Join Date
    Jul 2012
    Location
    Borgloon, Belgium
    Posts
    4
    Rep Power
    0

    Default Re: Java Serial Communications using RXTX

    Ok, I found a solution. The ARM chip started responding when I'd put the thread to sleep for 1sec.
    The class now looks like this:
    Java Code:
    package com.codegrasp.arduinoSerialConnection;
     
    import java.io.IOException;
    import java.io.OutputStream;
     
    public class SerialWriter implements Runnable {
        private OutputStream out;
     
        public SerialWriter(OutputStream out) {
            this.out = out;
        }
     
        public void run() {
     
            try {
                int c = 0;
                while (c < 10) {
                    this.out.write('a');
                    c++;
                    Thread.sleep(1000);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
    			e.printStackTrace();
    		}
        }
    }

Similar Threads

  1. RXTX / JAVA Communications question =P
    By santa in forum New To Java
    Replies: 2
    Last Post: 05-08-2011, 11:41 PM
  2. Serial communication via ethernet-serial hub
    By Idiodyssey in forum New To Java
    Replies: 0
    Last Post: 05-04-2011, 02:23 PM
  3. RXTX Library install on Ubuntu 10.04
    By Z.K. in forum New To Java
    Replies: 0
    Last Post: 05-01-2011, 11:22 PM
  4. Rxtx
    By Jcbconway in forum New To Java
    Replies: 0
    Last Post: 12-13-2010, 10:40 PM
  5. Replies: 1
    Last Post: 07-30-2010, 08:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •