问题描述
终于设法从 Windows 中的 rxtx 读取,但现在我无法让它在 Ubuntu 中工作.我使用 apt-get 获取 rxtx 库,但是当我运行应用程序时,我什么也看不到,尝试了几次-catch 块,我什至没有得到异常,而且由于目前无法进行基于 Ubuntu 的调试,因此我无法查明问题.(Ubuntu 是 12.04 64 位).
Finally managed to read from rxtx in windows but now I just cannot make it work in Ubuntu.I get the rxtx libs using apt-get ,but when i run the application,i can not see anything,tried a couple of try-catch blocks and i don't even get the exceptions,and since Ubuntu based debugging is not possible for now, i can not pinpoint the problem.(Ubuntu is 12.04 64 bit).
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;
}
}
推荐答案
昨天遇到了同样的问题,发现 这个:
I had the same problem yesterday, and found this:
String serialPortID = "/dev/ttyAMA0";
System.setProperty("gnu.io.rxtx.SerialPorts", serialPortID);
也就是说,你需要设置gnu.io.rxtx.SerialPorts
系统属性,值应该是你要打开的端口名.
Thath is, you need to set the gnu.io.rxtx.SerialPorts
system property, and the value should be the name of the port you want to open.
这篇关于RXTX 在 ubuntu 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!