2013-04-10 48 views
1

我想在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。請幫幫我。感謝你

+1

看起來像用於LTP和COM端口的RXTX庫,不適用於USB。 – user1516873 2013-04-10 07:00:09

回答

0

我Similiar錯誤的Ubuntu 14.04下方。端口未列出。 通過以root身份運行程序找到了解決方法。