version

Home

Programmers guide
Ex. ReadPort
Ex. SendAT

Send files

s25 HyperTerminal

Internet Connection

Programs

Tips & Tricks

Cable Scheme

Wap

Files

Links


E-m@il



  • Invio comandi AT a modem S25
    L'esempio presentato mostra come inviare comandi AT al cell. e riceverne la risposta. Prima di iniziare ad analizzare codice č propedeutico consultare la pagina S25HyperTerminal. Per provare l'applicazione č consigliabile scaricarsi il Manuale dei comandi AT al fine di conoscere i comandi interpretabili dal modem del cell.

    Codice:

    /* Files: SendAT.java */

    import java.awt.*;
    import java.awt.event.*;
    import java.io.InputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.Enumeration;
    import java.util.TooManyListenersException;
    import javax.comm.*;

    public
    class
    SendAT extends Frame

    implements ActionListener,
    ItemListener,
    SerialPortEventListener,
    WindowListener {

    /* GUI Objects. */
    private Label lATCommand, lResult;
    private Button bSend;
    private TextArea taResult;
    private TextField tfATCommand;
    private Choice chCOM;

    /* Connection Objects. */
    private static Enumeration portList;
    private static CommPortIdentifier portId;
    private static SerialPort serialPort;
    private static String COM;
    private static PrintStream printStream;
    private static InputStream inputStream;

    /**
    * Main method (Entry point application).
    */

    public static void main(String args[]) {

    SendAT sendAT = new SendAT();
    sendAT.pack();
    sendAT.setVisible(true);
    }

    /**
    * Constructor method.
    */

    public SendAT() {

    inizializeGUI(); }

    /**
    * Build the graphics of the application.
    */
    private void inizializeGUI() {

    /* Set Layout. */
    this.setSize(new Dimension(347, 358));
    this.setLayout(null);
    this.setBackground(Color.lightGray);
    this.setResizable(false);
    this.setTitle("Send AT - http://web.tiscalinet.it/SiemensS25");
    this.addWindowListener(this);

    /* Set Choice. */
    chCOM = new Choice();
    chCOM.addItem("Select Port");
    chCOM.addItem("COM1");
    chCOM.addItem("COM2");
    chCOM.addItem("COM3");
    chCOM.addItem("COM4");
    chCOM.addItem("COM5");
    chCOM.setBounds(new Rectangle(15, 31, 107, 22));
    chCOM.addItemListener(this);

    /* Set TextField. */
    tfATCommand = new TextField();
    tfATCommand.setBounds(new Rectangle(14, 95, 313, 24));
    tfATCommand.setText("AT+CLCK=cs,1");

    /* Set TextArea. */
    taResult = new TextArea("",100,100,1);
    taResult.setBounds(new Rectangle(13, 190, 317, 145));

    /* Set Button. */
    bSend = new Button();
    bSend.setBounds(new Rectangle(130, 139, 72, 24));
    bSend.setLabel("send");
    bSend.addActionListener(this);
    bSend.setEnabled(false);

    /* Set Labels. */
    lATCommand = new Label();
    lATCommand.setBounds(new Rectangle(15, 79, 101, 18));
    lATCommand.setText("AT Command");
    lResult = new Label();
    lResult.setBounds(new Rectangle(15, 173, 94, 17));
    lResult.setText("Result");

    /* Add the graphics objects to layout. */
    this.add(taResult, null);
    this.add(tfATCommand, null);
    this.add(bSend, null);
    this.add(lResult, null);
    this.add(lATCommand, null);
    this.add(chCOM, null);

    }

    /**
    * Capture the button events.
    */

    public void actionPerformed(ActionEvent e) {

    // get AT Command from textfield.
    String sAT = tfATCommand.getText().trim();

    // send AT Command to COM port.
    printStream.println(sAT);

    }

    /**
    * Capture the choice events and inizialize
    * the connection.
    */

    public void itemStateChanged(ItemEvent e) {

    System.out.println("Connecting....");

    // get port selected from choice.
    COM = (String)e.getItem();

    // if not port selected?
    if(COM.equals("Select Port")) {

    taResult.setText("Select COM");
    } else {
    // port selected.
    bSend.setEnabled(true);
    chCOM.setEnabled(false);

    // get to all the present ports.
    portList = CommPortIdentifier.getPortIdentifiers();

    // slide the found ports.
    while (portList.hasMoreElements()) {

    // assign port.
    portId = (CommPortIdentifier) portList.nextElement();

    // is serial port?
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

    // is selected port?
    if (portId.getName().equals(COM)) {
    try {

    // open port connection.
    serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);

    // open input stream.
    inputStream = serialPort.getInputStream();

    // open output stream.
    printStream = new PrintStream(serialPort.getOutputStream());

    // set connection.
    serialPort.setSerialPortParams(9600,
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
    System.out.println("Connection performed");

    // add port to events listener.
    serialPort.addEventListener(this);
    // notify data on COM.
    serialPort.notifyOnDataAvailable(true);

    } catch (UnsupportedCommOperationException ucoe) {

    ucoe.printStackTrace();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    } catch (PortInUseException piue) {
    taResult.setText("Port In Use!");
    piue.printStackTrace();
    } catch (TooManyListenersException tmle) {
    tmle.printStackTrace();
    }
    }
    }
    }
    }
    }

    /**
    * Capture COM port events.
    */

    public void serialEvent(SerialPortEvent event) {

    // filter event type.
    switch(event.getEventType()) {

    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;

    case SerialPortEvent.DATA_AVAILABLE:
    System.out.println("\nDATA AVAILABLE");

    // build COM read buffer.
    byte[] readBuffer = new byte[100];

    try {

    // number buffer.
    int numBytes;
    // if data present?
    while(inputStream.available() > 0) {
    // read buffer.
    numBytes = inputStream.read(readBuffer);
    }

    String sBuffer = new String(readBuffer).trim();
    System.out.println("Result--> " + sBuffer);

    // set textArea.
    taResult.appendText(sBuffer);

    } catch(IOException e) {
    e.printStackTrace(); }
    break;
    }
    }

    /* Capture window events */
    public void windowClosed(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowActivated(WindowEvent event) {}
    public void windowDeactivated(WindowEvent event) {}
    public void windowOpened(WindowEvent event) {}
    public void windowClosing(WindowEvent event) {

    System.exit(0);
    }
    } // end of class.

    Funzionamento:
    Dopo aver istanziato il costruttore della classe attraverso il metodo main(), viene invocato per primo il metodo inizializeGUI() che provvede a disegnare tutta l'interfaccia grafica.

    FIGURA 1 - esempio di blocco tastiera (Comando AT --> "AT+CLCK=cs,1")

    Premettiamo che nel programma vengono implentati 4 ascoltatori di eventi:
    • Ascoltatore eventi finestra
    • Ascoltatore eventi Trasmissione/Ricezione porta COM
    • Ascoltatore eventi pulsante
    • Ascoltatore eventi choice

    Proprio attorno questi ascoltatori gira l'esecuzione del programma. Selezionando una porta COM, la choice lancia un evento che viene catturato dal metodo itemStateChanged(), quest'ultimo avvalendosi di stream di Input/Output e di oggetti presenti nelle javacomm esegue il codice necessario per la connessione alla COM. Stabilita la connessione, l'applicazione attende inserimento di un comando AT. Premuto il pulsante "send" viene lanciato un evento che invoca il metodo actionPerformed(), quest'ultimo implementa il codice relativo alla spedizione di un comando AT mediante uno stream di output. La risposta del modem viene catturata dal metodo serialEvent che rileva tutte le operazioni di ricezione eseguite dalla porta COM e le stampa a video nella textArea.

    Download the zip file:
    source.zip



L'autore del sito non č responsabile per i danni che le informazioni contenute in queste pagine possono arrecare al vostro sistema. Tutti i marchi citati in queste pagine sono copyrights dei rispettivi proprietari.