我想在 linux 中与 USB GSM Modem 进行串行通信。我使用了以下代码。但 portList.hashElements 返回为假。我使用的是 RXTX 库 2.1.7 版本。请帮我。
代码
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;
private static SerialPort p;
public static void main(String[] args) throws NoSuchPortException {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
System.out.println("start: "+ ports.hasMoreElements());
while(ports.hasMoreElements())
{
CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
System.out.print(port.getName() + " -> " + port.getCurrentOwner() + " -> ");
switch(port.getPortType())
{
case CommPortIdentifier.PORT_PARALLEL:
System.out.println("parell");
break;
case CommPortIdentifier.PORT_SERIAL:
System.out.println("serial");
try {
p = (SerialPort) port.open("SimpleWrite", 1000);
int baudRate = 57600; // 57600bps
p.setSerialPortParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (PortInUseException e) {
System.out.println(e.getMessage());
} catch (UnsupportedCommOperationException e) {
System.out.println(e.getMessage());
}
break;
}
}
System.out.println("stop");
}
}
输出是:
稳定的图书馆
Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 start: false stop Experimental: JNI_OnLoad called .
System.out.println("开始:"+ ports.hasMoreElements()); 打印为false。请帮我。感谢您