2016-04-28 54 views
1

我必须通过蓝牙与arduino进行通信。 arduino连接到HC-06蓝牙模块,但这不是重点,因为我已经检查过(与一些Arduino的大师)蓝牙模块和配对。所以,如你所知,当你将你的电脑配对到蓝牙设备时,它可以分配给它一些COM端口。要通过蓝牙进行通信,您必须通过传出端口“发送消息”。我写了一个列出COM端口的小程序:第一次运行它时,我也可以看到输出端口。第二次,该COM端口将不会显示在列表中。显然你不能连接到它。使用RXTX库列出并连接到分配给Windows上的蓝牙设备的串行端口

有没有人知道发生了什么?这是我的(简单)代码,在许多教程中很常见。

java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); 
    while (portEnum.hasMoreElements()) 
    { 
     CommPortIdentifier portIdentifier = portEnum.nextElement(); 
     System.out.println(portIdentifier.getName()); 
    }  

我被卡住,绝望,因为它似乎没有道理。

谢谢。 Michele

回答

-1

看看这个。 (来自YOUTUBE。作者es Taha Emara)我做了一些修改,运行良好。

package serial_send; 

//import gnu.io.CommPortIdentifier; 
import gnu.io.CommPortIdentifier; 

/** 
* 
* @author Administrador 
*/ 
public class Test extends javax.swing.JFrame { 

    /** 
    * Creates new form Test 
    */ 
    public Test() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jButton1 = new javax.swing.JButton(); 
     jComboBox1 = new javax.swing.JComboBox(); 
     jTextField1 = new javax.swing.JTextField(); 
     jButton2 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jButton1.setText("puertos disponibles"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "", "", "" })); 
     jComboBox1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jComboBox1ActionPerformed(evt); 
      } 
     }); 

     jButton2.setText("enviar"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap(123, Short.MAX_VALUE) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGap(83, 83, 83)) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) 
          .addComponent(jTextField1) 
          .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)) 
         .addGap(112, 112, 112)))) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(29, 29, 29) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(30, 30, 30) 
       .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(34, 34, 34) 
       .addComponent(jButton2) 
       .addContainerGap(56, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); 
     int i = 0; 
     String[] r = new String[25]; 

     while (portEnum.hasMoreElements() && i < 20) { 
      CommPortIdentifier portIdentifier = portEnum.nextElement(); 

      r[i] = portIdentifier.getName();//+ " - " + getPortTypeName(portIdentifier.getPortType()) ; 

      System.out.println("i= " + i); 


      i++; 


     } 

     jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(r)); 
    }           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     String x = jTextField1.getText(); 
     Serial sr = new Serial(); 

     Object selectedItem = jComboBox1.getSelectedItem(); 

     String com = selectedItem.toString(); 
     sr.ser(x.getBytes(), com); 
     sr.close(); 


    }           

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    }           

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Test().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JComboBox jComboBox1; 
    private javax.swing.JTextField jTextField1; 
    // End of variables declaration     
} 
+1

有了这么多的UI代码,这个答案是不可读的。 –