2013-05-21 11 views
0

우선 영어로 유감입니다. 모국어가 아닙니다.버튼이있는 RXTX를 사용하는 입력 및 출력 스트림

직렬 포트와 통신하기 위해 RxTx 라이브러리로 자바 인터페이스를 만들려고합니다. Java 지식이 매우 제한되어 있기 때문에 Jigloo (내게 부끄러움)를 사용하여 인터페이스를 만들었습니다.

Java 인터페이스에서 JButton을 눌러 Java에서 내 전자 보드로 데이터를 보내려하고 있습니다. 하지만 여기에 문제가 있습니다. 어떻게 해야할지 정말 모르겠습니다. 내가 InputStreamOutputStream을 써야한다는 것을 알았지 만, 그것을 어디에 두어야할까요? 다른 수업이나 수업을 만들어야합니까? 여기

내 자바 UI의 코드입니다 : 당신이 알아 내기 위해 아래 순서대로 링크에서 RXTX 샘플을 살펴 수 있다고 생각

package projetPic; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.PrintStream; 
import java.net.InetAddress; 
import javax.swing.BorderFactory; 

import javax.swing.ComboBoxModel; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.JTextPane; 

import javax.swing.WindowConstants; 
import javax.swing.border.BevelBorder; 
import javax.swing.SwingUtilities; 

import gnu.io.*; 

public class ElectroFrame extends javax.swing.JFrame { 
    private JPanel jPanel1; 
    private JButton jButton1; 
    private JButton jButton6; 
    private JButton jButton5; 
    private JTextField jTextField1; 
    private JButton jButton4; 
    private JButton jButton3; 
    private JLabel jLabel1; 
    private JLabel jLabel2; 
    private JLabel jLabel3; 
    private JLabel jLabel4; 
    private JLabel jLabel5; 
    private JComboBox jComboBox1; 
    private JLabel jLabel8; 
    private JLabel jLabel7; 
    private JTextField jTextField3; 
    private JLabel jLabel6; 
    private JButton jButton7; 
    private JTextField jTextField2; 
    private JButton jButton2; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       ElectroFrame inst = new ElectroFrame(); 
       inst.setLocationRelativeTo(null); 
       inst.setVisible(true); 
      } 
     }); 
    } 

    public ElectroFrame() { 
     super(); 
     initGUI(); 
    } 

    private void initGUI() { 
     try { 
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
      { 
       jPanel1 = new JPanel(); 
       getContentPane().add(jPanel1, BorderLayout.CENTER); 
       GridBagLayout jPanel1Layout = new GridBagLayout(); 
       jPanel1Layout.rowWeights = new double[] {0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1}; 
       jPanel1Layout.rowHeights = new int[] {20, 75, 29, 32, 55, 36, -171, 35, 42, 38, 42, 20}; 
       jPanel1Layout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1}; 
       jPanel1Layout.columnWidths = new int[] {123, 150, 152, 7}; 
       jPanel1.setLayout(jPanel1Layout); 
       jPanel1.setBackground(new java.awt.Color(198,226,255)); 
       jPanel1.setPreferredSize(new java.awt.Dimension(584, 500)); 
       { 
        jButton1 = new JButton(); 
        jPanel1.add(jButton1, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jButton1.setText("Compteur binaire"); 
        jButton1.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent evt) { 
          jButton1ActionPerformed(evt); 
         } 
        }); 
       } 

       { 
        jButton2 = new JButton(); 
        jPanel1.add(jButton2, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jButton2.setText("Compteur Chenillard"); 
        jButton2.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent evt) { 
          jButton2ActionPerformed(evt); 
         } 
        }); 
       } 
       { 
        jLabel1 = new JLabel(); 
        jPanel1.add(jLabel1, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
       } 
       { 
        jButton3 = new JButton(); 
        jPanel1.add(jButton3, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jButton3.setText("Compteur Johnson"); 
        jButton3.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent evt) { 
          jButton3ActionPerformed(evt); 
         } 
        }); 
       } 
       { 
        jButton4 = new JButton(); 
        jPanel1.add(jButton4, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jButton4.setText("Clignoter"); 
        jButton4.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent evt) { 
          jButton4ActionPerformed(evt); 
         } 
        }); 
       } 
       { 
        jTextField1 = new JTextField(); 
        jPanel1.add(jTextField1, new GridBagConstraints(2, 6, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jTextField1.setPreferredSize(new Dimension(150,40)); 
       } 
       { 
        jButton5 = new JButton(); 
        jPanel1.add(jButton5, new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jButton5.setText("Calculer le CRC"); 
        jButton5.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent evt) { 
          jButton5ActionPerformed(evt); 
         } 
        }); 
       } 
       { 
        jButton6 = new JButton(); 
        jPanel1.add(jButton6, new GridBagConstraints(0, 8, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jButton6.setText("Calculer le LRC"); 
        jButton6.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent evt) { 
          jButton6ActionPerformed(evt); 
         } 
        }); 
       } 
       { 
        jTextField2 = new JTextField(); 
        jPanel1.add(jTextField2, new GridBagConstraints(2, 8, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jTextField2.setPreferredSize(new Dimension(150,40)); 
       } 
       { 
        jButton7 = new JButton(); 
        jPanel1.add(jButton7, new GridBagConstraints(0, 10, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jButton7.setText("Afficher"); 
        jButton7.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent evt) { 
          jButton7ActionPerformed(evt); 
         } 
        }); 
       } 
       { 
        jLabel2 = new JLabel(); 
        jPanel1.add(jLabel2, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jLabel2.setText("Projet 2TI - Electronique - 2012-2013"); 
       } 
       { 
        jLabel3 = new JLabel(); 
        jPanel1.add(jLabel3, new GridBagConstraints(1, 3, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jLabel3.setText("LED"); 
       } 
       { 
        jLabel4 = new JLabel(); 
        jPanel1.add(jLabel4, new GridBagConstraints(1, 5, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jLabel4.setText("CRC"); 
       } 
       { 
        jLabel5 = new JLabel(); 
        jPanel1.add(jLabel5, new GridBagConstraints(1, 7, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jLabel5.setText("LRC"); 
       } 
       { 
        jLabel6 = new JLabel(); 
        jPanel1.add(jLabel6, new GridBagConstraints(1, 9, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jLabel6.setText("7 segments"); 
       } 
       { 
        jTextField3 = new JTextField(); 
        jPanel1.add(jTextField3, new GridBagConstraints(2, 10, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jTextField3.setPreferredSize(new Dimension(150,40)); 
       } 
       { 
        jLabel7 = new JLabel(); 
        jPanel1.add(jLabel7, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
       } 
       { 
        jLabel8 = new JLabel(); 
        jPanel1.add(jLabel8, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jLabel8.setText("Port COM"); 
       } 
       { 
        ComboBoxModel jComboBox1Model = 
          new DefaultComboBoxModel(
            new String[] { "Item One", "Item Two" }); 
        jComboBox1 = new JComboBox(); 
        jPanel1.add(jComboBox1, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 
        jComboBox1.setModel(jComboBox1Model); 
       } 
      } 
      pack(); 
      setSize(600, 520); 
     } catch (Exception e) { 
      //add your error handling code here 
      e.printStackTrace(); 
     } 
    } 



    private void jButton1ActionPerformed(ActionEvent evt) { 
     System.out.println("jButton1.actionPerformed, event="+evt); 

    } 

    private void jButton2ActionPerformed(ActionEvent evt) { 
     System.out.println("jButton2.actionPerformed, event="+evt); 
     //TODO add your code for jButton2.actionPerformed 
    } 

    private void jButton3ActionPerformed(ActionEvent evt) { 
     System.out.println("jButton3.actionPerformed, event="+evt); 
     //TODO add your code for jButton3.actionPerformed 
    } 

    private void jButton4ActionPerformed(ActionEvent evt) { 
     System.out.println("jButton4.actionPerformed, event="+evt); 
     //TODO add your code for jButton4.actionPerformed 
    } 

    private void jButton5ActionPerformed(ActionEvent evt) { 
     System.out.println("jButton5.actionPerformed, event="+evt); 
     //TODO add your code for jButton5.actionPerformed 
    } 

    private void jButton6ActionPerformed(ActionEvent evt) { 
     System.out.println("jButton6.actionPerformed, event="+evt); 
     //TODO add your code for jButton6.actionPerformed 
    } 

    private void jButton7ActionPerformed(ActionEvent evt) { 
     System.out.println("jButton7.actionPerformed, event="+evt); 
     //TODO add your code for jButton7.actionPerformed 
    } 
} 

답변

0

방법이 라이브러리의 작품 :

http://rxtx.qbang.org/wiki/index.php/Two_way_communcation_with_the_serial_port

너무 어렵지 않습니다. 당신이 필요합니다 :

하나의 클래스를 직렬 포트 연결 처리 : TwoWaySerialComm

직렬 포트에 쓸 수있는 또 다른 하나 SerialWriter, 마지막으로

를 다른 클래스 (이 꼭 필요하지만 권장 아니다) GUI와 직렬 포트 기록기 사이의 다리를 만드십시오 (즉 ControllerMVC 모델).

JButton을 누를 때마다 Controller 메서드를 호출해야하며이 메서드로 무엇을 처리해야하는지 알아야합니다.