终于从窗户RXTX阅读,但现在我不能让它在Ubuntu.I工作中使用apt-get的,但是当我运行应用程序,我看不到任何东西拿到RXTX库,尝试了一对夫妇的try-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;
}
}