在JAVA中列出传出的串行端口

时间:2015-08-10 10:58:12

标签: java bluetooth

我正在开发一个服务器应用程序来链接arduino和scratch,以便使用java net beans进行串口通信。我在项目中添加了以下jar文件

  1. Arduino的。罐

  2. Firmata。罐

  3. RXTX jar

  4. 我已将所有相应的RXTX文件保存在相应的文件夹中,并且我能够毫无错误地运行我的程序,但我的主要目的是在GUI中列出com端口。但是我的程序列出了除BLUETOOTH模块(HC05)的输出串行com端口之外的所有串行端口。我是否需要添加任何其他库或任何其他jar文件来列出传出端口?如果我需要添加文件,请告诉我相同的步骤。下面是选择串口然后列出串口的代码。

      package listserialports;
    
        import a4s.A4S;
        import java.io.IOException;
        import java.util.Vector;
        import java.util.logging.Level;
        import java.util.logging.Logger;
    
        public class serialportselection extends javax.swing.JFrame
        {
    
            static String str;    
            static Vector listData = new Vector(8);
    
            public serialportselection()
            {
                initComponents();
    
                addData();
            }
    
            @SuppressWarnings("unchecked")
            // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
            private void initComponents() {
    
                jLabel1 = new javax.swing.JLabel();
                jButton1 = new javax.swing.JButton();
                jComboBox2 = new javax.swing.JComboBox();
    
               setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                setBackground(new java.awt.Color(51, 255, 102));
    
                jLabel1.setFont(new java.awt.Font("Microsoft PhagsPa", 1, 18)); // NOI18N
                jLabel1.setText("SELECT THE PORT");
    
                jButton1.setFont(new java.awt.Font("Microsoft PhagsPa", 1, 18)); // NOI18N
                jButton1.setText("CONNECT");
                jButton1.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        jButton1ActionPerformed(evt);
                    }
                });
    
                jComboBox2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
                jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select" }));
                jComboBox2.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        jComboBox2ActionPerformed(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()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1)
                            .addGap(81, 81, 81)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(126, 126, 126)
                            .addComponent(jButton1)))
                    .addContainerGap(67, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(71, 71, 71)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(71, 71, 71)
                    .addComponent(jButton1)
                    .addContainerGap(97, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            try {
    
                Runtime r = Runtime.getRuntime();
                r.exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe C:\\Users\\Admin\\Desktop\\fwdbckpwm12.sb2");
    
                 //r.exec("C:\\Windows\\notepad.exe C:\\Windows\\ss.txt");
                A4S a4sObj = new A4S(new String[]{jComboBox2.getSelectedItem().toString()});
    
            } catch (IOException ex) {
                Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        
    
        private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        }                                          
    
         public void addData()
        {
             ListSerialPorts listSerPortObj = new ListSerialPorts();//addedline
    
            listData = listSerPortObj.LoadComPorts();//addedline
    
            for (int index = 0; index < listData.size(); index++)
            {
                 str = (String) listData.get(index);
                 System.out.println(str);               
                 jComboBox2.addItem(str);
           }       
    
        }
    
        public static void main(String args[])
        {
    
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new serialportselection().setVisible(true);
                }
            });       
    
        }
    
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JComboBox jComboBox2;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration                   
    }
    
        /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package listserialports;
    
    import gnu.io.CommPortIdentifier;
    import java.util.Enumeration;
    import java.util.Vector;
    
    /*
     * Get list of ports available on this particular computer
     */
    public class ListSerialPorts
    {
                public static void main(String[] args)
                {
    
                }
    
                public Vector LoadComPorts()
                {
                           int totalElements;
                            CommPortIdentifier portId;
                            Enumeration en = CommPortIdentifier.getPortIdentifiers();
                            Vector listData = new Vector(8);
                            // Walk through the list of port identifiers and, if it
                                   // is a serial port, add its name to the list.
                            while (en.hasMoreElements())
                            {
                                        portId = (CommPortIdentifier) en.nextElement();
                                        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
                                        {
                                                    listData.addElement(portId.getName());
                                        }
                            }
    
                            totalElements = listData.size();
    
                            //Iterate through the vector
                            for (int index = 0; index < totalElements; index ++)
                            {
                                        System.out.println(listData.get(index));
                            }
    
                            return listData;
                }
    
    }
    

0 个答案:

没有答案