最后设法从windows中读取rxtx,但现在我不能使它在Ubuntu中工作。我使用apt-get获得rxtx库,但是当我运行应用程序时,我看不到任何东西,尝试了几个try-catch块,我甚至没有得到例外,并且由于现在基于Ubuntu的调试是不可能的,所以我不能确定问题。 (Ubuntu是12.04 64位)。RXTX不能在ubuntu工作
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;
}
}
是否在类路径中添加了rxtx库。我发现你没有在你的java文件中导入适当的类。 –
RXTXcomm.jar文件被添加到构建路径... – Sin5k4