【问题标题】:Java serial port cannot change speed on Raspberry PiJava 串口无法在 Raspberry Pi 上改变速度
【发布时间】:2017-10-04 18:23:09
【问题描述】:

我正在做一个项目,通过 RS485 串行连接在两个 Raspberry Pi 之间发送数据。为此,我编写了一个小 Java 程序来发送数据。 设置正在运行,但我无法更改 Java prog 中数据传输的速度。我正在使用一个包含 25kb 随机数据和两个 Raspberry Pi Model 1B 的测试文件。作为一个库,我使用的是 RXTX Java 库。

我已经更改了/boot/config.txt/boot/cmdline.txt 中的设置,因此我可以使用串行端口并更改速度。

为了测试硬件是否可以做到,我用简单的控制台命令发送了一些数据。一个 Raspi 发送:

cat 25kTestfile.txt > /dev/ttyAMA0

对方收到:

cat /dev/ttyAMA0 > 25kTestfile.txt

我用sudo stty 改变了速度。 通过这个命令行设置,我可以发送高达 1Mbs 的数据,并根据自己的喜好改变速度。

不过,在我的 Java 程序中,速度并没有改变。无论我将串口设置为 115200 还是 1000000,它发送的速度都保持不变。使用我的程序,发送 25k 需要不到 3 秒,而控制台需要 0.3 秒。

Java 程序由两个文件组成。我在下面包含了程序中最重要的部分。这里有一个设置串行端口连接的文件。

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialConnect {

  private SerialPort serialPort = null;
  private InputStream in = null;
  private OutputStream out = null;
  private CommPort commPort = null;
  private CommPortIdentifier portIdentifier = null;

  void connect( String portName ) throws Exception {
    portIdentifier = CommPortIdentifier
        .getPortIdentifier( portName );
    if( portIdentifier.isCurrentlyOwned() ) {
      System.out.println( "Error: Port is currently in use" );
    } else {
      int timeout = 2000;
      commPort = portIdentifier.open( this.getClass().getName(), timeout );

      if( commPort instanceof SerialPort ) {
        serialPort = ( SerialPort )commPort;
        serialPort.setSerialPortParams( 1000000,
                                        SerialPort.DATABITS_8,
                                        SerialPort.STOPBITS_1,
                                        SerialPort.PARITY_NONE );
        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

        in = serialPort.getInputStream();
        out = serialPort.getOutputStream();

      } else {
        System.out.println( "Error: Only serial ports are handled by this example." );
      }
    }
  }

  [...]

  public void write(byte[] input) {
      try {
        for(int i = 0; i < input.length; i++) {
          this.out.write( input[i] );
        }
        this.out.flush();
      } catch( IOException e ) {
        e.printStackTrace();
      }
  }

  public void write(byte input) {
      try {
         this.out.write(input);
      } catch( IOException e ) {
        e.printStackTrace();
      }
  }


  [...]

  public SerialConnect() {}

  public SerialConnect(String portName) {
    try{
      connect(portName);
    }
    catch (Exception e) {
      System.out.println("Error Failed to connect port.\n");
    }
  }
}

另一个使用串行连接的在这里:

File outFile = new File("./25kTestfile.txt");
FileInputStream in = new FileInputStream(outFile);
byte[] c = new byte[(int)outFile.length()];
in.read(c);
SerialConnect serialConnection = new SerialConnect("/dev/ttyAMA0"); 
if(serialConnection == null) {
    System.out.println("Could not open Serial port.\n");
    return;
}
serialConnection.write(c);

我现在的问题是:为什么速度没有变化?我需要设置一些其他的东西吗?有没有可能Java在Raspi上太慢以至于发送速度不能更快?

【问题讨论】:

    标签: java serial-port raspberry-pi data-transfer


    【解决方案1】:

    我发现了问题。 我用来发送字节数组的写入函数正在循环中将数据逐字节写入输出流。这显然是非常低效的。 我现在将 write 函数更改为如下所示:

    public void write(byte[] input) {
          try {
            this.out.write( input );  // no loop
            this.out.flush();
          } catch( IOException e ) {
            e.printStackTrace();
          }
      }
    

    这使发送过程快了大约 10 倍。

    所以问题不在于连接速度没有改变,而是函数不够快,无法向流提供足够的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2021-06-27
      • 1970-01-01
      • 2016-02-08
      • 2016-01-16
      • 1970-01-01
      相关资源
      最近更新 更多