[clos]commande AT

Fermé
doubayba Messages postés 2 Date d'inscription jeudi 3 mai 2012 Statut Membre Dernière intervention 29 juin 2012 - 29 juin 2012 à 19:16
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 29 juin 2012 à 23:13
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.ServeurSMS.Managed;


import java.io.IOException;
//import java.io.InputStream;
import java.io.InputStreamReader;
//import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.rmi.RemoteException;
import java.util.logging.Level;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.JOptionPane;



import com.sun.comm.Win32Driver;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;


public class EnvoiSms {


private CommPortIdentifier portId;
public SerialPort serialPort;
private static OutputStreamWriter out;
private static InputStreamReader in;


public EnvoiSms(){
// String ligne="";
// try {
// BufferedReader fichier = new BufferedReader(new FileReader("portCom.prop"));
// ligne = fichier.readLine();
// System.out.println(ligne);
//
// }
//
// catch (IOException e) {
// System.out.println(e);
// }


Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
String rep;
try {
//identification port
portId = CommPortIdentifier.getPortIdentifier("COM4");

// portId = CommPortIdentifier.getPortIdentifier(ligne);
} catch (NoSuchPortException e) {
System.out.println("erreur récupération de l'identifiant du port");}
if(portId.isCurrentlyOwned())
{
System.out.println("Port non disponible owner est : "+portId.getCurrentOwner());
}
else
{
System.out.println("Port "+portId.getName()+" is not owned.");
}

try {
//ouvrir port
serialPort = (SerialPort) portId.open("ENVOI SMS1", 2000);
} catch (PortInUseException e) {
System.out.println("erreur sur port : "+portId.getName()+" "+e.toString());
}
System.out.println("ouverture");
try {

out = new OutputStreamWriter(serialPort.getOutputStream(),"UTF-8");

in = new InputStreamReader(serialPort.getInputStream(),"UTF-8");
} catch (IOException e) {
System.out.println("Erreur Définition Stream de lecture");
}
try {

try {
serialPort.setSerialPortParams(
9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
System.out.println("Erreur paramétre du port");
}

}
catch (Exception eGSM) {
System.out.println("commande AT GSM Erreur"+eGSM.toString());
}


}


// public void ReceptionSMS (String msg){
// String rep;
// try {
// rep = sendAT("AT+CMGF=1");
// sendAT("AT+CSCS="HEX"");
// sendAT("AT+CSMP=17,167,0,0");
// System.out.println("Réponse Service = "+rep);
// // read sms response
//
// if (rep.startsWith("+CPMS")) // list sms SM, ME or MT
// {
// rep = sendAT("AT+CPMS="+""");
// }
// else if (rep.startsWith("+CMGR")) // read sms OK
// {
// rep = sendAT("AT+CMGR=");
// }
// }
// catch (RemoteException ex) {
// java.util.logging.Logger.getLogger(EnvoiSms.class.getName()).log(Level.SEVERE, null, ex);
// }
// catch (InterruptedException ex) {
// java.util.logging.Logger.getLogger(EnvoiSms.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
//



public void envoyer(String dst,String msg){
String rep;


try {
rep = sendAT("AT+CMGF=1");
//sendAT("AT+CSCS="HEX"");
//sendAT("AT+CSMP=17,167,0,0");
System.out.println("Réponse Service = "+rep);
rep=sendAT("AT+CMGS="+"""+dst+""");
Thread.sleep(2000);
System.out.println("Réponse Service = "+rep);
sendAT2(msg);
rep=sendAT("\u001A");

//logger.info("tel "+dst+"msg "+msg+">> rep :"+rep);
System.out.println("la rep de l envoi "+rep);
System.out.println("message envoyé");
Thread.sleep(5000);
System.out.println("fin session envoi");

}
catch (RemoteException ex) {
java.util.logging.Logger.getLogger(EnvoiSms.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
java.util.logging.Logger.getLogger(EnvoiSms.class.getName()).log(Level.SEVERE, null, ex);
}


}
public void FermerPort(){
serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnRingIndicator(true);
serialPort.notifyOnBreakInterrupt(true);
serialPort.close();
}

public String sendAT(String atcommand) throws java.rmi.RemoteException {
String s="";
try {
System.out.println("sendAT "+atcommand);
writeln(atcommand);
Thread.sleep(300);
s = read();
}
catch (Exception e) {
System.out.println("ERROR: send AT command failed; " + "Command: " + atcommand + "; Answer: " + s + " " + e);
JOptionPane.showMessageDialog(null,"ERROR: send AT command failed; " + "Command: " + atcommand + ";\n Answer: " + s + "\n " + e,"SMS",JOptionPane.ERROR_MESSAGE);

} // catch
return s;
}
public void writeln(String s) throws Exception {
System.out.println("writeln"+s);
out.write(s);
out.write('\r');
out.flush();
}
public String read() throws Exception {
int n, i;
char c;
String answer = new String("");
for (i = 0; i < 10; i++) {
while (in.ready()) {
n = in.read();
if (n != -1) {
c = (char)n;
answer = answer + c;
}
else break;
}

}
return answer;
}
public String sendAT2(String atcommand) throws java.rmi.RemoteException {
String s="";
try {

writeln2(atcommand);
Thread.sleep(300);
s = read();

}
catch (Exception e) {
System.out.println("ERROR: send AT command failed; " + "Command: " + atcommand + "; Answer: " + s + " " + e);
JOptionPane.showMessageDialog(null,"ERROR: send AT command failed; " + "Command: " + atcommand + ";\n Answer: " + s + "\n " + e,"SMS",JOptionPane.ERROR_MESSAGE);

} // catch
return s;
}
public void writeln2(String s) throws Exception {

out.write(s);
// out.write('\r');
out.flush();
}
public String stringToHexa(String texte) {
int c;//int's equivalent to char
String s="00";//separator
//To safe memory - limite gc requests
StringBuffer buff = new StringBuffer(texte.length());
for (int i = 0; i < texte.length(); i++) {
c=texte.charAt(i);
String mot=Integer.toHexString(c);
int k=mot.length();
for(int l=0;l<4-k;l++)mot="0"+mot;
buff.append(mot);
}
return buff.toString();
}
public static void main (String[] args){
EnvoiSms e = new EnvoiSms();
e.envoyer("num tel","bonjour tout le monde");
}
}

2 réponses

doubayba Messages postés 2 Date d'inscription jeudi 3 mai 2012 Statut Membre Dernière intervention 29 juin 2012
29 juin 2012 à 19:22
svp aidez moi , je sais pas eske le code est correcte ou nn ??? svp c urgeeeeeeeeeeeent :(
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
29 juin 2012 à 23:13
Salut (ou pas),

L'urgence est pour toi pas pour nous!

Sujet clos.


-----

"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
Rejoignez-nous