端口未列出

时间:2013-04-10 06:46:02

标签: java linux rxtx

我想在linux下与USB GSM调制解调器进行串行通信。我使用了以下代码。但是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");
}
}

输出为:

  

稳定的图书馆

     

Native lib Version = RXTX-2.1-7   Java lib Version = RXTX-2.1-7   开始: false   停   实验:JNI_OnLoad叫。

System.out.println(“start:”+ ports.hasMoreElements());打印为 false 。请帮我。感谢你

2 个答案:

答案 0 :(得分:0)

注意设备命名,因为linux无法识别任何命名模式。

也许我的回答可以提供帮助:

serial port identification with java on ubuntu

答案 1 :(得分:0)

Ubuntu 14.04下的类似错误。端口未列出。 通过以root身份运行程序找到解决方法。