2016-04-28 11 views
1

zugeordnet ist Ich muss mit einem Arduino über Bluetooth kommunizieren. Das Arduino ist mit einem HC-06 Bluetooth-Modul verbunden, aber das ist nicht der Punkt, weil ich bereits (mit einigen Gurus von Arduino) das Bluetooth-Modul und die Paarung überprüft habe.Liste und Verbindung zu einer seriellen Schnittstelle, die einem Bluetooth-Gerät auf Windows mit RXTX-Bibliothek

Also, wie Sie wissen, wenn Sie Ihren PC zu einem Bluetooth-Gerät paaren, könnte es ein paar COM-Ports zuweisen. Um über Bluetooth zu kommunizieren, müssen Sie Ihre Nachrichten über den ausgehenden Port senden. Ich habe ein kleines Java-Programm geschrieben, das die COM-Ports auflistet: Beim ersten Mal kann ich den ausgehenden Port sehen. Zum zweiten Mal wird dieser COM-Port nicht in der Liste angezeigt. Offensichtlich können Sie sich auch nicht damit verbinden.

Hat jemand eine Idee von dem, was passiert? Dies ist mein (einfacher) Code, der in vielen Tutorials sehr häufig vorkommt.

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

Ich bin fest und verzweifelt, weil es nicht sinnvoll scheint.

Vielen Dank. Michele

Antwort

-1

Werfen Sie einen Blick darauf. (Von YOUTUBE übernommen. Der Autor ist Taha Emara) Ich habe einige Änderungen vorgenommen und es läuft gut.

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

Mit dieser Menge von UI-Code ist diese Antwort nicht lesbar. –