Results 1 to 4 of 4
- 07-24-2012, 05:05 PM #1
Member
- Join Date
- Jul 2012
- Location
- Genk, Belgium
- Posts
- 5
- Rep Power
- 0
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(); } } }
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(); } } }
- 07-24-2012, 05:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-24-2012, 11:18 PM #3
Member
- Join Date
- Jul 2012
- Location
- Genk, Belgium
- Posts
- 5
- Rep Power
- 0
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);
Java Code:this.out.write('a');
To me it looks like the code stops working when I remove the "System.in.read()" line.
- 07-25-2012, 02:21 PM #4
Member
- Join Date
- Jul 2012
- Location
- Genk, Belgium
- Posts
- 5
- Rep Power
- 0
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
-
RXTX / JAVA Communications question =P
By santa in forum New To JavaReplies: 2Last Post: 05-09-2011, 12:41 AM -
Serial communication via ethernet-serial hub
By Idiodyssey in forum New To JavaReplies: 0Last Post: 05-04-2011, 03:23 PM -
RXTX Library install on Ubuntu 10.04
By Z.K. in forum New To JavaReplies: 0Last Post: 05-02-2011, 12:22 AM -
Rxtx
By Jcbconway in forum New To JavaReplies: 0Last Post: 12-13-2010, 11:40 PM -
serial communication is very slow while reading from serial port
By elsanthosh in forum AWT / SwingReplies: 1Last Post: 07-30-2010, 09:29 AM
Bookmarks