2012-07-29 46 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