avec ce code j'ai pu envoyer un sms avec le 1er frame
mon problème est le suivant, j'ai 3 classe ClassePort1, frame et frame2
je veux envoyer le sms avec le 2eme frame (frame2)
cad le 1er frame permet d'ouvrir le port com et le 2eme sert a envoyer le sms
merci d'avance

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
172
173
174
//Classe ClassePort1.java
 
package essai1;
 
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.JOptionPane;
import java.io.*;
import com.sun.comm.Win32Driver;
 
 
public class ClassePort1 implements SerialPortEventListener{
 
 
	static String TimeStamp;     
	SerialPort comPort;
	CommPortIdentifier portId;
	OutputStream os;
	static InputStream is;
	static PrintWriter sortie;
	Thread readThread;
	private static BufferedReader bufRead;
	private String messageString = "";
	private OutputStream outputStream = null;
 
 
	public ClassePort1()
	{
 
		Win32Driver w32Driver = new Win32Driver();
		w32Driver.initialize();
	}
 
 
	public void openPort(String Portname)
	{ 
		try{
			portId=CommPortIdentifier.getPortIdentifier(Portname);
			ComControl();
 
		}
 
		catch
		(Exception e) {
			//Creates today's date/time
			TimeStamp = new java.util.Date().toString();
			System.out.println(TimeStamp + ": ERROR " + portId);
			System.out.println(TimeStamp + ": msg1 - " + e); 
		}
	}
 
	public void ComControl() {
 
		try {
			TimeStamp = new java.util.Date().toString();
 
			comPort = (SerialPort) portId.open("DriveTestOmniacom", 10000);
			System.out.println("Le port : " + portId.getName() + " est ouvert");
 
		} catch (PortInUseException e) {}
		try {
 
			os = comPort.getOutputStream();
 
			bufRead = new BufferedReader(new InputStreamReader(comPort.getInputStream()));
 
			sortie = new PrintWriter(os, true);
			System.out.println("OK");
 
		} catch (IOException e) {System.out.println("Yaraab");}
 
		try {
			comPort.addEventListener(this);
		} catch (TooManyListenersException e) {}
		comPort.notifyOnDataAvailable(true);
		try {
 
			comPort.setSerialPortParams(4800,
					SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1,
					SerialPort.PARITY_NONE);
			/*
			 public abstract void setFlowControlMode(int flowcontrol)
                                 throws UnsupportedCommOperationException
            Sets the flow control mode
            flowcontrol - Can be a bitmask combination of
            FLOWCONTROL_NONE: no flow control
            FLOWCONTROL_RTSCTS_IN: RTS/CTS (hardware) flow control for input
            FLOWCONTROL_RTSCTS_OUT:	RTS/CTS (hardware) flow control for output
            FLOWCONTROL_XONXOFF_IN:	XON/XOFF (software) flow control for input
            FLOWCONTROL_XONXOFF_OUT:	XON/XOFF (software) flow control for output
			FLOWCONTROL_NONE: no flow control
			 */
 
			comPort.setFlowControlMode(comPort.FLOWCONTROL_NONE) ;
 
			comPort.setDTR(false);
			comPort.setRTS(false);
 
		} catch (UnsupportedCommOperationException e) {}
 
		readThread = new Thread();
		readThread.start();             
	}
 
	public void closePort()
	{
		comPort.close();
	}
 
	/*
	public static void ModeText(String str)
	{String reponse="";
	try
	{	int c;        
 
	System.out.println("nom");
	sortie.println(str);
	reponse=  (String)  bufRead.readLine().trim();
	System.out.println( "hhh"+reponse);
	Thread.sleep(3000);            
 
	}
	catch (Exception e)
	{
		JOptionPane.showMessageDialog(null,"Exeption dans envoyer()2. "+e.getMessage(),"Envoi...",JOptionPane.ERROR_MESSAGE);
	}
	}
 
	 */
 
	public void envoyer(String str) throws IOException
	{
		os.write( (byte[]) str.getBytes());
		os.flush();
		os.close();
 
		System.out.println("Le port : est ouvert");
 
		try
		{
			Thread.sleep(3000);
 
		}
		catch (InterruptedException ex)
		{
			Logger.getLogger(ClassePort1.class.getName()).log(Level.SEVERE, null, ex);
		}
 
	}
 
	public void serialEvent(SerialPortEvent arg0)
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}
 
 
	public void dial(String phoneNumber) {
		try {
			//dial to this phone number
			messageString = "ATD" + phoneNumber + ";\n\r";
			outputStream.write(messageString.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//Classe frame
package essai1;
 
import java.awt.BorderLayout;
import java.awt.EventQueue;
 
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.SerialPort;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.ImageIcon;
 
 
 
public class frame extends JFrame {
 
	private JPanel contentPane;
	//numéro de version pour la classe
	private static final long serialVersionUID = 1L;
	String reponse="";
	CommPortIdentifier portId;
	BufferedReader in;
	PrintWriter out;
	SerialPort serialPort;
	JComboBox comboBox = new JComboBox();
	JComboBox comboBox_1 = new JComboBox();
	JButton btnNewButton = new JButton("Connect");
	JButton btnSend = new JButton("Send");
 
	private static ClassePort1 port1;
 
	//static final private char cntrlZ=(char)26;
	/**
         * Create the frame.
         */
	public frame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
 
		JLabel lblDevice = new JLabel("Device");
		GridBagConstraints gbc_lblDevice = new GridBagConstraints();
		gbc_lblDevice.insets = new Insets(0, 0, 5, 5);
		gbc_lblDevice.gridx = 1;
		gbc_lblDevice.gridy = 3;
		contentPane.add(lblDevice, gbc_lblDevice);
 
		JLabel lblPorts = new JLabel("Port");
		GridBagConstraints gbc_lblPorts = new GridBagConstraints();
		gbc_lblPorts.insets = new Insets(0, 0, 5, 5);
		gbc_lblPorts.gridx = 4;
		gbc_lblPorts.gridy = 3;
		contentPane.add(lblPorts, gbc_lblPorts);
 
		//JComboBox comboBox = new JComboBox();
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 4;
		contentPane.add(comboBox, gbc_comboBox);
		comboBox.addItem("Nokia");
		comboBox.addItem("Samsung");
		comboBox.addItem("Sagem");
		comboBox.addItem("huawei");
 
		//JComboBox();
		GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
		gbc_comboBox_1.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_1.gridx = 4;
		gbc_comboBox_1.gridy = 4;
		contentPane.add(comboBox_1, gbc_comboBox_1);
 
		//("Connect");
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton.gridx = 5;
		gbc_btnNewButton.gridy = 4;
		contentPane.add(btnNewButton, gbc_btnNewButton);
 
		//("Send");
		GridBagConstraints gbc_btnSend = new GridBagConstraints();
		gbc_btnSend.insets = new Insets(0, 0, 0, 5);
		gbc_btnSend.gridx = 5;
		gbc_btnSend.gridy = 6;
		contentPane.add(btnSend, gbc_btnSend);
 
		comboBox_1.addActionListener(new FormeListener());
		btnNewButton.addActionListener(new BoutonListener());
		//btnSend.addActionListener(new BoutonListener2());
 
		listePortsDispo();
	}
 
	public void listePortsDispo(){
 
		System.err.println("recherche..." );
 
		Enumeration portList=CommPortIdentifier.getPortIdentifiers();
 
		if (portList==null)
 
		{
 
			System.err.println("Aucun port de communication détecté" );
 
			return;
 
		}
 
		while (portList.hasMoreElements()){
 
			portId=(CommPortIdentifier)portList.nextElement();
 
			comboBox_1.addItem(portId.getName());
 
			System.out.println("* "+portId.getName());
 
		} //fin while
 
	} //fin de la methode listePortsDispo()
 
	public class BoutonListener implements ActionListener{
 
		public void actionPerformed(ActionEvent arg0) {
 
			port1.openPort(reponse);
 
			new frame2().setVisible(true);
			dispose();
		}
	}
 
	class FormeListener implements ActionListener{
 
		public void actionPerformed(ActionEvent e) {
 
			reponse=comboBox_1.getSelectedItem().toString();
		}               
	}
 
	/*
	public static void envoie_sms(String sms, String phn)throws IOException
 
	{	
		try
		{          
		    port1.envoyer("AT+CMGF=1"+"\r");
 
			port1.envoyer("at+cmgs=\""+phn+"\"\r");
 
			port1.envoyer(sms+cntrlZ+"\r");
 
			Thread.sleep(1500);
		}
 
		catch (Exception ex)
 
		{
			Logger.getLogger(ClassePort1.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
	 */
 
	/* 
  class BoutonListener2  implements ActionListener{
 
	public void actionPerformed(ActionEvent e) {
 
		try
		{
	    envoie_sms("Si tu recois ce message fais moi un bip pour tester mon programme","22867992");
		}
 
		catch (Exception ex)
		{
			Logger.getLogger(ClassePort1.class.getName()).log(Level.SEVERE, null, ex);
		}	 
	}
}  
	 */
 
	/*
			String[] a = {"22515346"};
 
			try
 
			{
 
				for(int i=0;i<a.length;i++)
				{
					envoie_sms("Si tu recois ce message fais moi un bip pour tester mon programme",a[i]);
				}
			}
 
			catch (Exception ex)
			{
				Logger.getLogger(ClassePort1.class.getName()).log(Level.SEVERE, null, ex);
			}	 
		}
	}  
	 */
 
	public static void main(String args[]) throws NoSuchPortException{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame frame = new frame();
					frame.setVisible(true);
 
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
 
		port1=new ClassePort1();
 
		new frame ();
 
		//port1.closePort();
	}
}
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
//classe frame2.java
package essai1;
 
import java.awt.BorderLayout;
 
public class frame2 extends JFrame {
 
	private JPanel contentPane;
 
	/**
         * Launch the application.
         */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame2 frame = new frame2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	/**
         * Create the frame.
         */
	public frame2() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
 
		JButton btnEnvoyer = new JButton("envoyer");
		GridBagConstraints gbc_btnEnvoyer = new GridBagConstraints();
		gbc_btnEnvoyer.gridx = 2;
		gbc_btnEnvoyer.gridy = 2;
		contentPane.add(btnEnvoyer, gbc_btnEnvoyer);
		btnEnvoyer.addActionListener(new BoutonListener2());
	}
 
 
	class BoutonListener2  implements ActionListener{
 
		public void actionPerformed(ActionEvent e) {
 
 
		}
	}  
 
 
}