简体   繁体   English

使用rxtx将数据从一个串行端口传送到另一个串行端口

[英]Using rxtx to pipe data from one serial port to another

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. 我目前正在使用RXTX编写Java程序,该程序需要从一个串行端口获取原始数据,延迟它并将其发送到另一个端口。 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. 我看了RXTX在其wiki上的示例,似乎它们所有人都使用inputstream api中的read方法。 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). 输入数据来自无线电,无线电以5Hz的速率接收数据块(每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? 有人可以帮助我使用RXTX从串行端口读取数据并将其写入另一个端口吗? 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() . InputStream不仅返回字节数,还返回带有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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM