1

我正在使用 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 毫秒,即所需时间的两倍。这可能是什么原因造成的?

/亨里克

4

2 回答 2

3

(Windows)PC 不是实时机器。这意味着当您的应用程序必须访问硬件层时,它可能会被延迟。您无法控制这一点,并且由于系统(内核)的工作方式,在进入函数和退出函数之间没有固定的时间量。

大多数 Linux 机器的行为方式相同。在后台运行的其他任务(应用程序)会消耗处理能力,因此您的应用程序可能会在发送实际数据之前在进程队列中移动一点。

即使在发送过程中,发送的每个字节之间也可能存在延迟。这一切都由内核/硬件层处理,您的软件无法改变它。

如果您确实需要实时执行,那么您将不得不寻找实时操作系统

这条线很好地总结了它:

RTOS 的一个关键特性是它在接受和完成应用程序任务所需的时间量方面的一致性水平。可变性是抖动。

对于 RTOS,此抖动是已知/已定义的,而对于普通操作系统,此抖动是未知/未定义的。

于 2012-05-22T11:07:34.693 回答
0

你用 System.nanoTime() 测量时间吗?

System.currentTimeMillis() 使用的 Windows 时钟分辨率默认约为 15 毫秒,因此实时每个任务可能需要 20 毫秒,但有些任务分布在两个滴答声上而不是一个滴答声上。

有关更多信息,请参阅System.currentTimeMillis 与 System.nanoTime

于 2012-05-22T14:42:58.807 回答