bonjour
depuis un moment j'ai essayer de créer un application java qui envairer un sms par intermédiaire du mobile connecter a mon ordi.
le problème est que si j'exécute l'application il ne ce passe rien mes si je déconnecte la prise usb du mobile alors il ce mes tous
de suite après a envoyer le sms.
voici mon code :
ou
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 java.io.*; import javax.comm.*; import java.util.*; public class PortWriter { static Enumeration ports; static CommPortIdentifier pID; static OutputStream outStream; static SerialPort serPort; static BufferedReader is = null; static PrintStream os; static int i=0; public PortWriter() throws Exception{ try { //serPort = (SerialPort)pID.open("/dev/ttyUSB0",2000); serPort = (SerialPort)pID.open("COM3",2000); System.out.println(); System.out.println(); serPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); try { is = new BufferedReader(new InputStreamReader(serPort.getInputStream())); } catch (IOException e) { System.err.println("Can't open input stream: write-only"); is = null; } os = new PrintStream(serPort.getOutputStream(),true, "US-ASCII"); } catch (PortInUseException e) { System.out.println("PortInUseException : "+e); } catch (Exception e) { System.out.println("PortInUseException : "+e); } } public static void main(String[] args) throws Exception{ ports = CommPortIdentifier.getPortIdentifiers(); while(ports.hasMoreElements()) { pID = (CommPortIdentifier)ports.nextElement(); System.out.println("Port " + pID.getName()); if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL) { //if (pID.getName().equals("/dev/ttyUSB0")) if (pID.getName().equals("COM3")) { PortWriter pWriter = new PortWriter(); System.out.println("USB found"); sendData(); try { closingPort(); } catch (Exception e) { System.out.println("error in calling closingPort()"); System.out.println(e.toString()); } } } } } public static void sendData() { try { String sCMGF="AT+CMGF=0\r\n"; send(sCMGF); Thread.sleep(1000); readLine(); String sCMGS="AT+CMGS=\"+33600000000\"\r\n"; send(sCMGS); Thread.sleep(1000); readLine(); String smsMessage="hi test4 testing done\032\r\n"; send(smsMessage); } catch (Exception e) { System.out.println("could not write to outputstream:"); System.out.println(e.toString()); } } //called finally to close the port public static void closingPort() throws Exception { try { if (is != null) is.close(); } catch (IOException e) { System.out.println("could not close is"); System.out.println(e.toString()); } try { if (os != null) os.close(); } catch (Exception e) { System.out.println("could not close os"); System.out.println(e.toString()); } try { if (serPort != null) serPort.close(); } catch (Exception e) { System.out.println("could not close serPort"); System.out.println(e.toString()); } } //send the string to the gsm modem public static void send(String cmd) { try { os.write(cmd.getBytes()); } catch (IOException e) { System.out.println("IO Exception : "+e); } } // to read line after each send of string public static void readLine() { try { // Read the response String response = is.readLine(); System.out.println("Response is :"+response); } catch (IOException e) { System.out.println("IO Exception : "+e); } } }
Merci.
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 import javax.comm.*; import java.io.*; import java.util.*; public class SimpleWrite implements Runnable, SerialPortEventListener { public void run() { } static Enumeration portList; static CommPortIdentifier portId; static String messageString = "AAA"; static char ch = '"'; static String dest = ch + "+33600000000" + ch; // 11 Digit Mobile Number. static InputStream inputStream; static SerialPort serialPort; static OutputStream outputStream; 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: { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = ""; try { while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.err.println("Error while reading Port " + e); } break; } } //switch } public SimpleWrite(SerialPort serial) { try { inputStream = serial.getInputStream(); try { serial.addEventListener(this); } catch (TooManyListenersException e) { System.out.println("Exception in Adding Listener" + e); } serial.notifyOnDataAvailable(true); } catch (Exception ex) { System.out.println("Exception in getting InputStream" + ex); } } public static void main(String[] args) { String line1 = "AT+CSMS=1\r\n"; String line2 = "AT+CMGS=" + dest + "\r\n"; String line3 = messageString + "\r\n"; portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM3")) { System.out.println("SMS Sending....Port Found"); try { serialPort = (SerialPort) portId.open("com3.modem", 2000); SimpleWrite wr = new SimpleWrite(serialPort); } catch (PortInUseException e) { System.out.println("Port In Use " + e); } try { outputStream = serialPort.getOutputStream(); } catch (IOException e) { System.out.println("Error writing to output stream " + e); } try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { } try { outputStream.write(line1.getBytes()); outputStream.write(line1.getBytes()); outputStream.write(line2.getBytes()); outputStream.write(line3.getBytes()); outputStream.write(26); outputStream.flush(); } catch (Exception e) { System.out.println("Error writing message " + e); } } } } } /** * show text in the text window * * @param Text text string to show on the display */ public static void showText(String Text) { System.out.println(Text); } }
Partager