我使用PC上的普通串口在Java應用程序中發送和接收數據。 PC使用java 1.6.0運行Windows XP SP3。下面是代碼:爲什麼我在Java中的UART性能有所不同?
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,7ms轉移。
我把Time2-Time1的測量值放在一個數組中,即發送時間,並在稍後檢查。事實證明,有時256字節的傳輸需要15ms傳輸,這看起來很好,因爲它接近理論最小值。我不確定爲什麼它在實踐中比理論上更快。然而,問題在於,有時256字節的傳輸需要32ms,即需要的兩倍。什麼可能導致這個?
/Henrik
我看到你添加了「input = new BufferedInputStream(input,1024);」我認爲這會解決我的問題,但這似乎沒有做任何事情。看起來你將被限制在實際的UART芯片上的緩衝區大小,沒有什麼比這更少的了。嘗試不同大小然後發送自己的字符,並保持在4096最大。我希望有一種方法可以知道uart的最大緩衝區大小,儘管java –