2012-07-29 44 views
2

我想通過串口使用java中的RxTx檢測設備,並且設備已編程,如果它從計算機接收到特定單詞,它將回復「ok」,如果計算機收到確定..它將停止發送該單詞並突出顯示設備已連接。請幫幫我。還有一件事..我必須檢查每個端口..請你會編碼一種方法,自動循環通過端口,直到設備被檢測到。 即使處於無限循環,我的代碼也只發送一次該字。 代碼:谷歌搜索的我想在java中使用RxTx通過串行端口檢測設備

private void cb1KeyPressed(java.awt.event.KeyEvent evt) {        
    // TODO add your handling code here: 
    try{ 
    l1.setText("Port: "+cb1.getSelectedItem().toString()+" is Selected"); 
    selectedPort = cb1.getSelectedItem().toString();// TODO add your handling code here 
    rs.connect(selectedPort); 
    for(;;) 
    { 
     CommPortSender.send(new ProtocolImpl().getMessage("KITM"));//send message 
     if(pi.rmess().equalsIgnoreCase("OK"))//received message 
     { 
      l1.setText("The Device is attached to: "+selectedPort); 
      CommPortSender.send(new ProtocolImpl().getMessage("OK ACK"));//send message 
      break; 
     } 
     else 
     { 
      rs.disconnect(selectedPort); 
      continue; 
     } 
    } 
    } 
    catch(Exception e){} 

} 

回答

0
static void listPorts() 
    { 
     java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); 
     while (portEnum.hasMoreElements()) 
     { 
      CommPortIdentifier portIdentifier = portEnum.nextElement(); 
      System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType())); 
     }   
    } 

    static String getPortTypeName (int portType) 
    { 
     switch (portType) 
     { 
      case CommPortIdentifier.PORT_I2C: 
       return "I2C"; 
      case CommPortIdentifier.PORT_PARALLEL: 
       return "Parallel"; 
      case CommPortIdentifier.PORT_RAW: 
       return "Raw"; 
      case CommPortIdentifier.PORT_RS485: 
       return "RS485"; 
      case CommPortIdentifier.PORT_SERIAL: 
       return "Serial"; 
      default: 
       return "unknown type"; 
     } 
    } 

5分鐘可以告訴你同樣的事情。

+1

ghostbust555>嘿感謝哥們爲你付出的努力..但是這不是我a了...這是任何人都可以做的最簡單的事情..請你再回顧我的問題:) – Manu 2012-07-30 06:22:04