I am currently writing a Java program using RXTX that needs to take in raw data from one serial port, delay it, and send it to another port. The delay will be on the order of seconds so the resolution isn't that important.
I took a look at the examples RXTX has on their wiki and it seems that all of them use the read method from the inputstream api. For my application, this seems pretty useless since it just returns the number of bytes in the stream, when my program just needs to take in whatever data it sees.
The input data is coming from a radio which is receiving blocks of data at a rate of 5Hz (a block of data every ~0.2s).
I already took their example program and edited it such that I can connect to both serial ports of my choosing at once, so connecting to the ports is not a problem.
Can someone help me with using RXTX to read in data from a serial port and write it to another? Can I use sleep to introduce my delay here?
The InputStream doesn't just return the number of bytes, it returns the bytes with read()
. You can read the bytes, do the delay and write to the other serial port.
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = inputStream.read(buffer)) != -1)
{
Thread.sleep(5000);
// then write to the target serial port.
}