Bonjour j'aimerai pouvoir changer un String dans ma classe main et j'utiliserai celui dans mon autre classe comment puis je faire cela??
merci bien de votre aide
Bonjour j'aimerai pouvoir changer un String dans ma classe main et j'utiliserai celui dans mon autre classe comment puis je faire cela??
merci bien de votre aide
Je ne comprend pas ce que tu souhaites faire ?!
Tu veux passer un String de la classe main à une autre classe ? Si c'est çà il te suffit de le passer en argument du constructeur de l'autre classe ou par l'intermédiaire d'une méthode de l'autre classe.
je vais peut etre mieux expliquer pour que tout le monde comprenne.
Dans ma classe principale j'ai une combobox avec plusieur valeur.
Lorsque je choisi cette valeur j'aimerai que ma 2em classe (qui envoie des données via mon port com vert un autre pc) envoi le texte de ma combobox.
J'espère que vous avez compris??
Merci de votre aide
si j'ai bien compris, tout est simple tu cré une methode qui retourne une string et tu la recupere dans ta classe ou tu as instancier la classe
hein?
La j'ai rien compris à ce que tu me dis dsl
en gros je choisi dans ma combobox le texte que je veut (dans ma 1er classe)
et avec ma 2em classe j'envoie ce contenue vers un autre pc
voila
Une fois que tu as choisi le texte de ta JComboBox il faut le récupérer et pour ça il faut faire :
Ensuite tu dois la passer à ta deuxième classe qui va se charger de faire le transfert sur l'autre PC.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 String maChaine = (String)(maComboBox.getSelectedItem());
Je suppose donc que tu as déjà une méthode dans ta 2ème classe qui te permet d'envoyer une chaine de caractère, non ?
Si oui, elle doit prendre en argument un objet de type String et donc tu l'appeleras depuis ta classe principale avec la chaine sélectionnée.
Donc en gros tu as :
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 class Principale { private JComboBox _maComboBox; private MaDeuxiemeClasse _deuxiemeClasse; public void comboBoxChanged() { String maChaineSelectionnee = (String)(maComboBox.getItemSelected()); _deuxiemeClasse.envoie(maChaineSelectionnee); } } class MaDeuxiemeClasse { public void envoie(String maChaine) { // Envoie de la chaine .... } }
Une fois que ça sera fait, il te faudra détecter à quel moment la sélection de la JComboBox a été modifiée, pour ça il te faut un listener sur cette dernière, cf. :
http://www.developpez.net/forums/sho...d.php?t=190313
J'espère avoir été plus clair![]()
Dans ce cas il te suffit de détecter l'évènement de sélection de ta JComboBox et d'en récupérer l'objet sélectionné (dans ton cas un String) à l'aide de la méthode getSelectedItem() et tu la donnes à ta 2ème classe par l'intermédiaire d'une méthode du genre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 public void envoieChaine(String maChaine) { // Envoie de la chaine }
Me revoila alors voici mes 2 classes
Main :
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
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 import java.io.*; import java.util.*; import javax.comm.*; 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 = null; jTextArea1.append("Début création liste des Ports\n"); jComboBox1 = new javax.swing.JComboBox(PortComManager.getPortList().toArray()); 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()); }//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 }
Le souci est que quand je clique sur mon jbutton2 la combobox ne se rempli pas et pas de message d'erreur pouvez vous m'aider?
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 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 list = new ArrayList(); Enumeration portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { list.add((CommPortIdentifier) portList.nextElement()); } return list; } public void write(String data) { } public String read() { try { inputStream = serialPort.getInputStream(); } catch (IOException e) {} String chaine = ""; byte[] readBuffer = new byte[20]; try { while (inputStream.available() > 0) { 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
As-tu vérifié si la méthode PortComManager.getPortList() te retournait les données attendues ?
c'est bon j'ai réussi à trouver pour cela maintenant j'aimerai que quand je clique sur le bouton 3 je puisse recevoir les données d'un autre pc
Merci à voici
Réponse pour la combobox :
Main :
PortComManager :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 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"); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 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; }
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 :
_maChaineRecue est mise à jour à chaque fois que tu reçois un évènement DATA_AVAILABLE.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 public String getMaChaineRecue() { return _maChaineRecue; }
c'est bon j'ai tout réussi à faire je vous en fait part voici donc mes 2 classes.
Main :
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
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 }
Merci bien à vous pour votre aide
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) { } }
et problème résolu
Merci bien de votre aide
Partager