2012-05-10 22 views
1

我正在使用java庫rxtxx向微控制器發送數據 該應用程序似乎工作但警告出現:rxtxx2.1.7版本不匹配 會影響發送的數據嗎? 我做了測試,但沒有數據被髮送我的代碼是:使用java通過usb串口線連接

import java.util.*; 
import gnu.io.*; 
import java.io.*; 



public class portwrit{ 
    static Enumeration  portList; 
    static CommPortIdentifier portId; 
    static String msgstr="100"; 
    static SerialPort   serialPort; 
    static OutputStream  outputStream; 
    static InputStream inputStream; 
    static Thread readThread; 


    static boolean  outputBufferEmptyFlag = false; 

    public static void main(String[] args) throws NoSuchPortException, PortInUseException { 
    boolean portFound = false; 
    String defaultPort = "COM6"; 


    portList = CommPortIdentifier.getPortIdentifiers(); 

    while (portList.hasMoreElements()) { 
     portId = (CommPortIdentifier) portList.nextElement(); 

     if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 

     if (portId.getName().equals(defaultPort)) { 
      System.out.println("Found port " + defaultPort); 
      portFound = true; 
      try { 
      serialPort = 
       (SerialPort) portId.open("SimpleWrite", 2000); 
      } catch (PortInUseException e) { 
      System.out.println("Port in use."); 
      continue; 
      } 

      try { 
      outputStream = serialPort.getOutputStream(); 
      } catch (IOException e) {} 

      try { 
      serialPort.setSerialPortParams(9600, 
           SerialPort.DATABITS_8, 
           SerialPort.STOPBITS_1, 
           SerialPort.PARITY_NONE); 
      } catch (UnsupportedCommOperationException e) {} 

      try { 
       serialPort.notifyOnOutputEmpty(true); 
      } catch (Exception e) { 
      System.out.println("Error setting event notification"); 
      System.out.println(e.toString()); 
      System.exit(-1); 
      } 

      System.out.println(
       "Writing "+msgstr+"\" to "+serialPort.getName()); 
      try { 
      outputStream.write(Byte.parseByte(msgstr)); 
      } catch (IOException e) {} 

      try { 
       Thread.sleep(2000); // Be sure data is xferred before closing 
      } catch (Exception e) {} 
      serialPort.close(); 
      System.exit(1); 

     } 
     } 
    } 

    if (!portFound) { 
     System.out.println("port " + defaultPort + " not found."); 
    } 
    } 


} 

回答

0

根據this article您還可以安裝RXTX軟件的多個副本。它建議在你的路徑/類路徑中尋找comm.jar或RXTXcomm.jar或類似的東西。

+0

感謝它一直非常有幫助我會嘗試 –

+0

我試過這個軟件包:http://pharos.ece.utexas.edu/wiki/images/7/7e/Ch-rxtx-2.2-20081207-win- x64.zip我不知道它會工作,但沒有警告出現謝謝 –