Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 22
  1. #1

    Comandi AT su seriale

    ciao ragazzi, ho un modem GSM di cui mi è stato rifornito il protocollo ...
    in questo protocollo ci sono tutti i vari comandi per chiamare,mandare sms e cose varie...
    ora : il problema è che i comandi sono del tipo

    ATDnumero_desiderato
    esempio
    atd0340989898

    se usassi l'hyper terminal non avrei problemi e scriverei la stringa dentro!
    Ma se io programmo in java cosa devo mandare fisicamente alla porta seriale ??? una stringa con dentro "ATnumerotelefono" ????
    GRazie mille

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,301

    Moderazione

    Il linguaggio?? :rollo:
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    in java ho scritto...

  4. #4
    ciao ti è andata proprio proprio di culo!

    poco fa ho fatto un sowftware per l'invio di sms e tramite un modem gsm

    ti posto la classe che si occupa della comunicazione seriale
    (ho usato javacomm)

    codice:
    package gsmcomm;
    
    import java.util.*;
    import java.io.*;
    import javax.comm.*;
    import gsmcomm.Sms;
    /**
     * Class Gsm
     *
     */
    public class Gsm {
        // Fields
        
        private static Enumeration	    portList;
        private static CommPortIdentifier portId;
        private static SerialPort	    serialPort;
        private static OutputStream       out;
        private static InputStream        in;
        
        private String dev_serial;
        
        private char invio=(13);
        private char fine=(26);
        private char virgolette=(34);
        
        // Methods
        // Constructors
        
        public Gsm(String dev_serial){
            this.dev_serial=dev_serial;
        }
        
        private void Log(String message){
            Main.Log(message);
        }
        
      /*
       * questo metodo assegna a Serialport il device passato al costrutture,
       *@throws una eccezione se non trova il device o se viene generato un errore
       */
        
        public void SetSerialPort()throws Exception{
            portList = CommPortIdentifier.getPortIdentifiers();
            boolean portFound = false;
            
            while (portList.hasMoreElements()) {
                portId = (CommPortIdentifier) portList.nextElement();
                
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    
                    if (portId.getName().equals(dev_serial)) {
                        this.Log("Found port " + dev_serial);
                        
                        portFound = true;
                        
                        try {
                            serialPort =
                                    (SerialPort) portId.open("SimpleWrite", 2000);
                        } catch (PortInUseException e) {
                            this.Log("Port in use.");
                            
                            continue;
                        }
                        
                        try {
                            out = serialPort.getOutputStream();
                            in  =serialPort.getInputStream();
                        } catch (IOException e) {}
                        
                        try {
                            serialPort.setSerialPortParams(9600,
                                    SerialPort.DATABITS_8,
                                    SerialPort.STOPBITS_1,
                                    SerialPort.PARITY_NONE);
                        } catch (UnsupportedCommOperationException e) {}
                        
                        
                        try {
                            serialPort.notifyOnOutputEmpty(true);
                        } catch (Exception e) {
                            this.Log("Error setting event notification");
                            this.Log(e.toString());
                            System.exit(-1);
                        }
                    }
                }
            }
            
        }
        
        // Operations
        /**
         *
         * @param smsID
         * @return Sms
         */
        public Sms ReadSMS( int smsID) {
            return null;
        }
        /**
         *
         * @param sms
         * @return void
         */
        public void SendSMS( Sms sms) throws Exception {
            
            if(sms.getNumber()==null || sms.getText()==null){
                throw new Exception("Errore , testo o numeri nulli");
            }
            
            try{
                out.write(("AT+CMGF=1"+String.valueOf(this.invio)).getBytes());
                out.write(("AT+CMGS="+String.valueOf(this.virgolette)+sms.getNumber()+String.valueOf(this.virgolette)+String.valueOf(this.invio)).getBytes());
                out.write((sms.getText()+String.valueOf(this.invio)).getBytes());
                out.write(String.valueOf(this.fine).getBytes());
                
                this.Log("invio riuscito:"+sms);
            }catch(Exception e){
                throw new Exception("Errore durante l'invio del messaggio: "+e.getMessage());
            }
            
        }
        /**
         *
         * @param smsID
         * @return void
         */
        public void DeleteSMS(int posizione) throws Exception {
            try{
                out.write(("AT+CMGD="+posizione+String.valueOf(this.invio)).getBytes());
                 
                this.Log("eliminazione dalla SIM riuscita del messaggio in posizione:"+posizione);
            }catch(Exception e){
                throw new Exception("Errore durante l'eliminazione del messaggio in posizione:"+posizione);
            }        
        }
        
        public Sms getSmsFromString(String smsstring){
            
            StringTokenizer t=new StringTokenizer(smsstring,"\n");
            int counter=0;
            int counter_t2=0;
            
            String numero=null;
            String testo=null;
            int id_sim=-1;
            
            while(t.hasMoreTokens()){
                String currentstring=t.nextToken();
                StringTokenizer t2=new StringTokenizer(currentstring,",");
                
                while(t2.hasMoreTokens()){
                    String currenttoken=t2.nextToken();
                    if(counter==0){
                        if(counter_t2==0){
                            //imposto l'id del messaggio sulla SIM
                            id_sim=Integer.parseInt(currenttoken);
                        }else if(counter_t2==2){
                            //imposto il numero del mittente
                            numero=currenttoken;
                        }
                    }
                    counter_t2++;
                }
                
                if(counter==1){
                    testo=currentstring;
                }
                /*reimposto il counter utilizzato quando faccio il parsing della prima linea
                    che contiene:
                 *  token 0:3
                    token 1:"REC READ"
                    token 2:"+39222222222"
                    token 3:"06/01/30
                    token 4:15:06:24+04"
                 */
                counter_t2=0;
                counter++;
            }
            if(numero!=null && testo!=null && id_sim!=-1){
                
                return new Sms(numero, testo, id_sim);
                
            }else{
                
                return null;
                
            }
        }
        
        public void DeleteListSms(ArrayList listsms) throws Exception{
            for(int i=0;i<listsms.size();i++){
                //un messaggio viene automaticamente messo in posizione 1
                this.DeleteSMS(1);
            }
        }
        
        /**
         *
         * @return ArrayList
         */
        public ArrayList ListSMS( ) throws Exception {
            
            ArrayList listasms=new ArrayList();
            
            try{
                out.write(("AT+CMGL="+String.valueOf(this.virgolette)+"ALL"+String.valueOf(this.virgolette)+String.valueOf(this.invio)).getBytes());
                
                Thread.sleep(5000);
                
                byte [] lettura=new byte[in.available()];
                
                int check_lettura=in.read(lettura);
                String buffer=new String(lettura);
                
                int inizio_messaggio=buffer.indexOf("+CMGL: ");
                
                while(inizio_messaggio!=-1){
                    /*
                     tolgo al buffer la parte antecedente all'inizio del messaggio e la lunghezza del
                     codice di inizio messaggio
                     */
                    
                    buffer=buffer.substring(inizio_messaggio+"+CMGL: ".length());
                    
                    /*
                     verifico che ci siano almeno ancora 2 messaggi
                     */
                    int fine_mex=0;
                    if(buffer.indexOf("+CMGL: ")==-1){
                        fine_mex=buffer.length();
                    }else{
                        fine_mex=buffer.indexOf("+CMGL: ");
                    }
                    
                    String mex_corrente=buffer.substring(0,fine_mex);
                    
                    //System.out.println("mex corrente:"+mex_corrente);
                    listasms.add(this.getSmsFromString(mex_corrente));
                    
                    inizio_messaggio=buffer.indexOf("+CMGL: ");
                }
                
            }catch(Exception e){
                throw new Exception("Errore durante la lista dei messaggi: "+e.getMessage());
            }
            return listasms;
        }
    }
    "durante i primi 5 miuti di pioggia nel bosco c'è ancora asciutto, poi quando smetterà di piovere nel bosco cadranno gocce per 5 minuti.....la natura ha un'ottima memoria..."

    http://www.kumbe.it

  5. #5
    oddio che bello...t ringrazio veramente tanto...
    usi elettroterm mod elvx.004k?

  6. #6
    no ho un wavecom M1206B
    "durante i primi 5 miuti di pioggia nel bosco c'è ancora asciutto, poi quando smetterà di piovere nel bosco cadranno gocce per 5 minuti.....la natura ha un'ottima memoria..."

    http://www.kumbe.it

  7. #7
    import javax.comm.*;--> comm.jar
    import gsmcomm.Sms; --> ???

  8. #8
    quando per esempio alla sendsms passi la variabile sms volevo capire se il tipo SMS lo hai creato tu o cè qlc package..

  9. #9
    sms è una mia classe stupidissima

    codice:
    package gsmcomm;
    
    /**
     * Class Sms
     * 
     */
    public class Sms {
      // Fields
    
      private int id;
      // 
      private String number;
      // 
      private String text;
      // Methods
      // Constructors
      
      public Sms(String number,String text,int id){
         
          //pulisco il numero dalle virgolette
          this.number=number.replace('"', ' ');
          this.number=number.replaceFirst("+39", "");
          this.number=number.replaceAll(" ", "");
    
          //pulisco la sttringa text da caratteri pericolosi
          this.text=text.replaceAll("'","^");
          this.id=id;
      }
      // Accessor Methods
      /**
       * Get the value of number
       * 
       * @return the value of number
       */
      public String getNumber (  ) {
        return number;
      }
      
      public int getId(){
        return this.id;
      }
      /**
       * Set the value of number
       * 
       * 
       */
      private void setNumber ( String value  ) {
        number = value;
      }
      /**
       * Get the value of text
       * 
       * @return the value of text
       */
      public String getText (  ) {
        return text;
      }
      /**
       * Set the value of text
       * 
       * 
       */
      private void setText ( String value  ) {
        text = value;
      }
      
      public String toString(){
        return (this.id+" - "+this.number+" - "+this.text);
      }
      // Operations
    }
    "durante i primi 5 miuti di pioggia nel bosco c'è ancora asciutto, poi quando smetterà di piovere nel bosco cadranno gocce per 5 minuti.....la natura ha un'ottima memoria..."

    http://www.kumbe.it

  10. #10
    ciao...
    ho provato a mettere le tue due classi e ho creato un main del tipo :

    Gsm g = new Gsm("COM1");
    //numero, testo messaggio, (Id del messaggio ???)
    Sms sms = new Sms ("339....","messageText",1);
    try {
    g.SendSMS(sms);
    } catch (Exception e) {
    e.printStackTrace();
    }

    mi da errore qui:
    Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
    +39
    ^

    che è???

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.