Java 未使用RXTX列出端口
我想使用串行端口API连接到USB调制解调器。所以我使用了下面的代码,即使连接了USB调制解调器,也没有列出 我在fedora工作,安装了RXTX库,但问题就在那里。请帮助我Java 未使用RXTX列出端口,java,Java,我想使用串行端口API连接到USB调制解调器。所以我使用了下面的代码,即使连接了USB调制解调器,也没有列出 我在fedora工作,安装了RXTX库,但问题就在那里。请帮助我 import java.io.*; import java.util.*; import gnu.io.*; public class SimpleWrite { static Enumeration portList; static CommPortIdentifier portId; static String
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;
public static void main(String[] args) throws NoSuchPortException {
portList = CommPortIdentifier.getPortIdentifiers();
//System.out.println("JVM Version: " +System.getProperty("java.runtime.version"));
System.out.println(portList.hasMoreElements()); //portList.hasMoreElements()
//portList.hasMoreElements()
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/ttyUSB0")) {
try {
serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {
}
}
}
}
}
}
主要问题是“portList.hasMoreElements()返回false”
谢谢另外,由于它位于USB网桥的另一侧,因此库可能无法将其识别为COM端口。