最后设法从windows中的rxtx读取但现在我无法在Ubuntu中使用它。我使用apt-get获取rxtx库,但是当我运行应用程序时,我看不到任何东西,尝试了几次尝试-catch块,我甚至没有得到例外,因为现在不能进行基于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;
}
}
答案 0 :(得分:1)
检查配置文件javax.comm.properties
是否在类路径中。由于这个文件,我遇到了无数的RXTX问题 - 它只是无声地失败。
答案 1 :(得分:1)
昨天我遇到了同样的问题,发现了this:
String serialPortID = "/dev/ttyAMA0";
System.setProperty("gnu.io.rxtx.SerialPorts", serialPortID);
也就是说,您需要设置gnu.io.rxtx.SerialPorts
系统属性,该值应该是您要打开的端口的名称。