Lien entre deux classe

Résolu
Bouboukick Messages postés 78 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 14 octobre 2008 - 20 mai 2008 à 14:02
Bouboukick Messages postés 78 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 14 octobre 2008 - 27 mai 2008 à 10:48
Bonjour, je suis occupé de faire un programme sous NetBeans et j'ai 2 fichier l'un pour mon interface graphique l'autre pour me connecter à mon port com et attendre que des données arrivent. Le problème est que lorsque je lance mon projet (les 2 fichiers sont dans le meme block) seul mon interface graphique se lance. Il doit exister un moment pour exécuter les 2 en meme temps en mettant une ligne dans mon programme main(interface) non?

Voici mes 2 fichier :

Main : (interface graphique)

package blackbox;
import java.io.*;
import java.util.*;
import javax.comm.*;

public class Main extends javax.swing.JFrame
{
    static CommPortIdentifier portId;
    static Enumeration          portList;

    public Main()
    {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

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

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

}                                       

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements())
    {
        portId = (CommPortIdentifier) portList.nextElement();
        jComboBox1.addItem(portId.getName());
    }
}                                       
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Main().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.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration
}

Read : (connection port com et attente données)

package blackbox;
import java.io.*;
import java.util.*;
import javax.comm.*;

public class Read implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration          portList;
    InputStream              inputStream;
    SerialPort              serialPort;
    Thread              readThread;

    public static void main(String[] args) {
    boolean              portFound = false;
    String              defaultPort = "/dev/ttyS0";

     if (args.length > 0) {
        defaultPort = args[0];
    }
  
    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (portId.getName().equals(defaultPort)) {
            System.out.println("Found port: "+defaultPort);
            portFound = true;
            Read reader = new Read();
        }
        }
    }
    if (!portFound) {
        System.out.println("port " + defaultPort + " not found.");
    }
    
    }

    public Read() {
    try {
        serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
    } catch (PortInUseException e) {}

    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {}

    try {
        serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {}

    serialPort.notifyOnDataAvailable(true);

    try {
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                       SerialPort.STOPBITS_1,
                       SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {}

    readThread = new Thread(this);

    readThread.start();
    }

    public void run() {
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {}
    }

    public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {

    case SerialPortEvent.BI:

    case SerialPortEvent.OE:

    case SerialPortEvent.FE:

    case SerialPortEvent.PE:

    case SerialPortEvent.CD:

    case SerialPortEvent.CTS:

    case SerialPortEvent.DSR:

    case SerialPortEvent.RI:

    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        break;

    case SerialPortEvent.DATA_AVAILABLE:

        byte[] readBuffer = new byte[20];
        try {
        while (inputStream.available() > 0)
                {
                    int numBytes = inputStream.read(readBuffer);
        }

        System.out.print(new String(readBuffer));
        } catch (IOException e) {}

        break;
    }
    }

}

merci d'avance

Bretzel : Tout est be qui finit bien!!

3 réponses

Bouboukick Messages postés 78 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 14 octobre 2008
27 mai 2008 à 10:48
pas de merci à vous car j'ai trouver pas grace à vous ciao

Bretzel : Tout est be qui finit bien!!
3
Bouboukick Messages postés 78 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 14 octobre 2008
21 mai 2008 à 15:09
Ce que je veux exactement faire c'est :

Lorsque je lance mon programme l'interface apparait (fichier Main) et lorsque je clique su un bouton l'autre fichier se met en route.

Comment je pui réaliser cela.

merci d'avance pour votre aide

Bretzel : Tout est be qui finit bien!!
0
Bouboukick Messages postés 78 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 14 octobre 2008
26 mai 2008 à 08:59
personne pour moi?

Bretzel : Tout est be qui finit bien!!
0
Rejoignez-nous