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:
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):
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();
}
}
}
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
Re: Java Serial Communications using RXTX
Jeah, that was also my first thought.
But when (in the working example), I change the line:
to:
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.
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:
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();
}
}
}