IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Lier un String entre 2 classes


Sujet :

avec Java

  1. #21
    Membre régulier Avatar de scorbo
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    176
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Décembre 2002
    Messages : 176
    Points : 83
    Points
    83
    Par défaut
    Comment ça ? Tu veux envoyer une commande au 2ème PC pour qu'il t'envoie des données lorsque tu cliques sur le bouton 3 ? Ou tu veux afficher les données qu'un autre PC t'as envoyé lorsque tu cliques sur le bouton 3 ?

    Si c'est la 2ème solution, il te suffit d'écrire une méthode dans ta classe PortComManager pour récupérer les données reçues :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    public String getMaChaineRecue() {
        return _maChaineRecue;
    }
    _maChaineRecue est mise à jour à chaque fois que tu reçois un évènement DATA_AVAILABLE.

  2. #22
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Points : 35
    Points
    35
    Par défaut
    c'est bon j'ai tout réussi à faire je vous en fait part voici donc mes 2 classes.

    Main :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    import javax.comm.*;
     
    @SuppressWarnings("serial")
    public class Main extends javax.swing.JFrame
    {
        private PortComManager manager;
     
        public Main()
        {
            manager = new PortComManager();
            initComponents();
        }
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        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();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jComboBox1.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    jComboBox1ItemStateChanged(evt);
                }
            });
     
            jButton1.setText("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);
     
            jButton3.setText("Lecture");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
     
            jButton4.setText("Ecriture");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
     
            jButton5.setText("Quitter");
            jButton5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton5ActionPerformed(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()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 738, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                            .addGap(31, 31, 31))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(205, 205, 205)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton4)
                        .addComponent(jButton1)
                        .addComponent(jButton5))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        manager.init();
    }//GEN-LAST:event_jButton1ActionPerformed
     
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jTextArea1.setText("");
        jComboBox1.removeAllItems();
        jTextArea1.append("Début création liste des Ports\n");
        System.out.println(PortComManager.getPortList());
        System.out.println(PortComManager.getPortList().toArray());
        for (Object o : PortComManager.getPortList())
        {
        	jComboBox1.addItem(o);
        }
        jTextArea1.append("Fin création liste des Ports\n");
    }//GEN-LAST:event_jButton2ActionPerformed
     
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    	jTextArea1.setText(manager.read());
    }//GEN-LAST:event_jButton3ActionPerformed
     
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        manager.write(jComboBox1.getSelectedItem().toString());
        manager.write(jComboBox1.getSelectedItem().toString());
    }//GEN-LAST:event_jButton4ActionPerformed
     
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        System.exit(1);
    }//GEN-LAST:event_jButton5ActionPerformed
     
    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        manager.setPortId((CommPortIdentifier)jComboBox1.getSelectedItem());
    }//GEN-LAST:event_jComboBox1ItemStateChanged
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Main().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        // End of variables declaration//GEN-END:variables
    }
    PortComManager :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
     
    public class PortComManager implements SerialPortEventListener
    {
        public static String defaultPort="/dev/ttyS0";
        private SerialPort serialPort;
        private InputStream inputStream;
        private OutputStream outputStream;
        private CommPortIdentifier portId;
     
        public PortComManager()
        {
            boolean		      portFound = false;    
            Enumeration 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;
    		    this.init();
    		} 
    	    } 
    	} 
    	if (!portFound)
            {
                System.out.println("port " + defaultPort + " not found.");
    	}
     
        }
     
        public void init() {
            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) {}
     
            try
            {
                outputStream = serialPort.getOutputStream();
            }
            catch (IOException e) {}
     
            try
            {
                outputStream.write("+++".getBytes());
            }
            catch (IOException e) {}
        }
     
        public CommPortIdentifier getPortId() {
            return this.portId;
        }
     
        public void setPortId(CommPortIdentifier portId){
            this.portId = portId;
        }
     
        public static List<CommPortIdentifier> getPortList()
        {
        	List<CommPortIdentifier> list = new ArrayList<CommPortIdentifier>();
        	Enumeration portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements()) 
            {
                list.add((CommPortIdentifier) portList.nextElement());
            }
            return list;
     
        }
     
        public void write(String data)
        {
        	System.out.println(data);
        	try
            {
                outputStream.write(data.getBytes());
                outputStream.write("\n".getBytes());
            }
            catch (IOException e) {}
        }
     
        public String read()
        {
        	String chaine = "";
            byte[] readBuffer = new byte[20];
            try
            {
            	System.out.println("Je suis ici aussi");
            	int numBytes = inputStream.read(readBuffer);
            	for(int i=0; i<numBytes; i++)
            	{
            		chaine = chaine + (char) readBuffer[i];
            	}
            	System.out.println(chaine);
            }
            catch (IOException e) {}
            return chaine;
        }
     
        public void serialEvent(SerialPortEvent arg0)
        {
     
        }
    }
    Merci bien à vous pour votre aide

    et problème résolu

    Merci bien de votre aide

  3. #23
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Points : 35
    Points
    35
    Par défaut
    J'ai gagné

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Réponses: 3
    Dernier message: 22/11/2005, 11h12
  2. Pb sur les String entre navigateurs
    Par chpog dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 20/09/2005, 10h59
  3. Comparaison entre les classes et les fonctions
    Par Ashgenesis dans le forum Langages de programmation
    Réponses: 6
    Dernier message: 08/09/2005, 19h09
  4. Réponses: 5
    Dernier message: 17/08/2005, 12h40
  5. Pb accès entre 2 classes static
    Par d.w.d dans le forum C++
    Réponses: 5
    Dernier message: 23/02/2005, 19h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo