我正在使用 PC 上的普通串行端口在 Java 应用程序中发送和接收数据。PC 运行带有 java 1.6.0 的 Windows XP SP3。这是代码:
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.util.concurrent.ArrayBlockingQueue;
// Open the serial port.
CommPortIdentifier portId;
SerialPort serialPort;
portId = CommPortIdentifier.getPortIdentifier("COM1");
serialPort = (SerialPort) portId.open("My serial port", 1000 /* 1 second timeout */);
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// Set up input and output streams which will be used to receive and transmit data on the UART.
InputStream input;
OutputStream output;
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
// Wrap the input and output streams in buffers to improve performance. 1024 is the buffer size in bytes.
input = new BufferedInputStream(input, 1024);
output = new BufferedOutputStream(output, 1024);
// Sync connection.
// Validate connection.
// Start Send- and Receive threads (see below).
// Send a big chunk of data.
为了发送数据,我设置了一个线程,该线程从队列 (ArrayBlockingQueue) 中获取数据包并将其发送到 UART。接收类似。应用程序的其他部分可以简单地将包插入发送队列,然后轮询接收队列以获取回复。
private class SendThread extends Thread {
public void run() {
try {
SendPkt pkt = SendQueue.take();
// Register Time1.
output.write(pkt.data);
output.flush();
// Register Time2.
// Put the data length and Time2-Time1 into an array.
// Receive Acknowledge.
ResponsePkt RspPkt = new ResponsePkt();
RspPkt.data = receive(); // This function calls "input.read" and checks for errors.
ReceiveQueue.put(RspPkt);
} catch (IOException e) { ... }
}
每个发送数据包最多 256 字节,传输需要 256*8 位 / 115200 位/秒 = 17.7 毫秒。
我将 Time2-Time1 的测量值放在一个数组中,即发送时间,稍后再检查。事实证明,有时 256 字节的传输需要 15 毫秒才能传输,这似乎很好,因为它接近理论最小值。我不确定为什么它在实践中比理论上更快。然而,问题是有时 256 字节的传输需要 32 毫秒,即所需时间的两倍。这可能是什么原因造成的?
/亨里克