我想在Linux中使用USB GSM Modem进行串行通信。我已经使用了下面的代码。但是portList.hashElements返回false。我已经使用了RXTX librarry 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");
}
}
输出是:
稳定图书馆
本地库版本= RXTX-2.1-7 的Java库版本= RXTX-2.1- 7 开始:false stop 实验:调用JNI_OnLoad。
System.out.println(“start:”+ ports.hasMoreElements());打印为false。请帮帮我。感谢你
看起来像用于LTP和COM端口的RXTX库,不适用于USB。 – user1516873 2013-04-10 07:00:09