我想使用串口API连接USB MODEM。所以我使用了以下代码,即使连接了USB MODEM,也没有列出。
我在fedora工作,安装了RXTX库,但问题就在那里..请帮助我。
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 ”
感谢你