2012-05-16 47 views
3

最後設法從windows中讀取rxtx,但現在我不能使它在Ubuntu中工作。我使用apt-get獲得rxtx庫,但是當我運行應用程序時,我看不到任何東西,嘗試了幾個try-catch塊,我甚至沒有得到例外,並且由於現在基於Ubuntu的調試是不可能的,所以我不能確定問題。 (Ubuntu是12.04 64位)。RXTX不能在ubuntu工作

import gnu.io.*; 
import java.io.*; 
import javax.swing.JOptionPane; 

public class ReadComPort { 

    public static void main(String[] s) { 
     readcomport(); 
    } 

    public static String readcomport() { 
     String value = null; 

     try { 
      // CommPortIdentifier portIdentifier = CommPortIdentifier 
      // .getPortIdentifier("COM1"); 

      // String comportidentifier = "COM1"; //*win 
      String comportidentifier = "/dev/ttyS0"; 

      CommPortIdentifier portIdentifier = null; 
      portIdentifier = CommPortIdentifier.getPortIdentifier(comportidentifier); 

      if (portIdentifier.isCurrentlyOwned()) { 
       JOptionPane.showMessageDialog(null, "port in use"); 
      } else { 

       SerialPort serialPort = (SerialPort) portIdentifier.open("ReadComPort", 500); 
       JOptionPane.showMessageDialog(null, serialPort.getBaudRate()); 

       serialPort.setSerialPortParams(serialPort.getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, 
         SerialPort.PARITY_NONE); 
       // serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT); 
       serialPort.setDTR(true); 
       serialPort.setRTS(true); 

       InputStream mInputFromPort = serialPort.getInputStream(); 

       Thread.sleep(500); 
       byte mBytesIn[] = new byte[32]; 
       mInputFromPort.read(mBytesIn); 

       value = new String(mBytesIn); 

       mInputFromPort.close(); 
       serialPort.close(); 
      } 
     } catch (Exception ex) { 
      JOptionPane.showMessageDialog(null, "Exception : " + ex.getMessage()); 

     } 

     return value; 

    } 
} 
+0

是否在類路徑中添加了rxtx庫。我發現你沒有在你的java文件中導入適當的類。 –

+0

RXTXcomm.jar文件被添加到構建路徑... – Sin5k4

回答

1

檢查配置文件javax.comm.properties是否在類路徑上。由於這個文件,我對RXTX有無盡的問題 - 它只是默默地失敗。

+0

erm似乎無法找到它...它通常位於何處? – Sin5k4

+0

啊。那可能就是問題了。在這裏檢查:http://pradnyanaik.wordpress.com/2009/04/07/communicating-with-ports-using-javaxcomm-package-for-windows/ - 您可能需要找到一個javax-comm包,其中包含屬性文件 – mcfinnigan

+0

OK找到一個包含3個文件的rar文件,其中一個是.properties文件。我如何添加它?從構建路徑/配置構建路徑菜單? – Sin5k4

1

昨天我有同樣的問題,結果發現this

String serialPortID = "/dev/ttyAMA0"; 
System.setProperty("gnu.io.rxtx.SerialPorts", serialPortID); 

Thath是,你需要設置gnu.io.rxtx.SerialPorts系統屬性,和值應該是你要打開的端口的名稱。

+0

你能指出如何設置'gnu.io.rxtx.SerialPorts'系統屬性嗎? – gkiko

+0

這就是我在代碼中做的:'System.setProperty(「gnu.io.rxtx.SerialPorts」,serialPortID);' –

+0

謝謝,這對我很有用。 – Alex