终于设法在 Windows 中从 rxtx 读取,但现在我无法在 Ubuntu 中使用它。我使用 apt-get 获取 rxtx 库,但是当我运行应用程序时,我看不到任何东西,尝试了几次-catch block ,我什至没有得到异常,并且由于目前无法进行基于 Ubuntu 的调试,所以我无法查明问题所在。 (Ubuntu 是 12.04 64 位)。
import gnu.io.*;
import java.io.*;
import javax.swing.JOptionPane;
public class ReadComPort {
public static void main(String[] s) {
readcomport();
}
public static String readcomport() {
String value = null;
try {
// CommPortIdentifier portIdentifier = CommPortIdentifier
// .getPortIdentifier("COM1");
// String comportidentifier = "COM1"; //*win
String comportidentifier = "/dev/ttyS0";
CommPortIdentifier portIdentifier = null;
portIdentifier = CommPortIdentifier.getPortIdentifier(comportidentifier);
if (portIdentifier.isCurrentlyOwned()) {
JOptionPane.showMessageDialog(null, "port in use");
} else {
SerialPort serialPort = (SerialPort) portIdentifier.open("ReadComPort", 500);
JOptionPane.showMessageDialog(null, serialPort.getBaudRate());
serialPort.setSerialPortParams(serialPort.getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.setDTR(true);
serialPort.setRTS(true);
InputStream mInputFromPort = serialPort.getInputStream();
Thread.sleep(500);
byte mBytesIn[] = new byte[32];
mInputFromPort.read(mBytesIn);
value = new String(mBytesIn);
mInputFromPort.close();
serialPort.close();
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Exception : " + ex.getMessage());
}
return value;
}
}
最佳答案
检查配置文件javax.comm.properties
是否在类路径中。因为这个文件,我在 RXTX 上遇到了无穷无尽的问题——它只是默默地失败了。
关于java - RXTX 在 ubuntu 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10620691/