Question

I saw that similar topics were posted on this forum, but I simply don't understand how to send AT commands and receive a response. (I started to program in C# several months ago. I'm still an n00b, but I'm working hard to learn it...).

I need to create application which would only receive SMS message through GSM USB dongle. So far I managed to create app that will recognize and connect modem through COM ports that is available. Now I need to push AT commands for receiving messages and displaying them into a textBox. I was wondering if anyone can spare few minutes to explain the process to me, and modify my code with comments so I can finally learn and understand how to use serialPort for communication. What I need to know, when SMS is sent, does this message is received and stored by GSM modem (and it is stored until I send some requests to read them or do I need to send some event that would trigger GSM modem to collect message from ISP)? how to push AT commands and receive their response (I only know that is done by using serialPort object, but doesn't have clue how to do it...)

This is my method for receiving (which I'm stuck BTW... :))

private void receiveMessage()
{
    //commclass is only a class for getting COM port, baud rate and timeout
    CommClass cc = new CommClass();
    cc.setParameters();
    serialPort1.PortName = cc.getPort();
    serialPort1.BaudRate = cc.getBaud();
    serialPort1.ReadTimeout = cc.getTimeout();
    serialPort1.Open();

    if (!serialPort1.IsOpen)
    {
        //MessageBox is written in Croatian language, it is only an alert to check the configuration because port is not opened...
        MessageBox.Show("Modem nije spojen, molimo provjerite konfiguraciju...!");
        //timer1.Stop();
    }
    else
    {
        //this.label2.Text = serialPort1.PortName;
        //this.label2.Visible = true;
        //this.label3.Visible = true;
        //this is where I need to place a code for receiving all SMS messages
        this.serialPort1.Write("AT+CMGL=\"REC UNREAD\"");
    }
    serialPort1.Close();
}

If anyone willing to help, I would appreciate that, if not I would have to deal with it by my self (probably spent few hours/days until I figure it out...)

In both cases, thank you anyway... Cheers.

Was it helpful?

Solution

Sorry for waiting for my reply, been busy lately. In short this is my code for getting message from GSM USB dongle. I hope that it will be useful to someone...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace SMSget
{
public partial class SMSLogPanel : UserControl
{
    SerialPort sp;
    int datab = 0;
    bool dtr = false;
    bool encod;
    Handshake h;
    Parity p;
    int wtimeout = 0;
    StopBits s;

    #region default constructor
    public SMSLogPanel()
    {
        InitializeComponent();
        this.sp = serialPort1 = new SerialPort();
        this.datab = serialPort1.DataBits = 8;
        this.dtr = serialPort1.DtrEnable = true;
        this.encod = serialPort1.Encoding.Equals("iso-8859-1");
        this.h = serialPort1.Handshake = Handshake.RequestToSend;
        this.p = serialPort1.Parity = Parity.None;
        this.wtimeout = serialPort1.WriteTimeout = 300;
        this.s = serialPort1.StopBits = StopBits.One;
        checkLink();
    }
    #endregion
    #region checking communication and setting user controls...
    private void checkLink()
    {
        GetValues value = new GetValues();
        string com = value.getPort();
        int baud = value.getBaud();
        int timeot = value.getTimeout();
        serialPort1.PortName = com;
        serialPort1.BaudRate = baud;
        serialPort1.ReadTimeout = timeot;

        serialPort1.Open();
        if (serialPort1.IsOpen)
        {
            label1.Visible = true;
        }
        else
        {
             MessageBox.Show("Komunikacija sa modemom se ne može uspostaviti, molimo postavite novu konfiguraciju...!");
            this.Controls.Clear();
            SMSConfigPanel cfg = new SMSConfigPanel();
            cfg.Show();
            this.Controls.Add(cfg);
        }
        serialPort1.Close();
    }
    #endregion
    #region panel load method
    private void SMSLogPanel_Load(object sender, EventArgs e)
    {
        setGSM();
    }
    #endregion
    #region execute serialport handler
    public void getMessage()
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(getResponse);
        }
        else
        {
            MessageBox.Show("Nije moguće zaprimiti poruku, komunikacijski port nije otvoren...1");
            return;
        }
    }
    #endregion
    #region get response from modem
    public void getResponse(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort serPort = (SerialPort)sender;
        string input = serPort.ReadExisting();

        if (input.Contains("+CMT:"))
        {

            if (input.Contains("AT+CMGF=1"))
            {
                string[] message = input.Split(Environment.NewLine.ToCharArray()).Skip(7).ToArray();
                textBox1.Text = string.Join(Environment.NewLine, message);
            }
            this.Invoke((MethodInvoker)delegate
            {
                textBox1.Text = input;
            });
        }
        else
        {
            return;
        }
    }
    #endregion
    #region initialize GSM
    private void setGSM()
    {
        serialPort1.Open();

        if (!serialPort1.IsOpen)
        {
            MessageBox.Show("Problem u komunikaciji sa modemom, port nije otvoren...!");
        }
        serialPort1.Write("AT+CMGF=1" + (char)(13));
        serialPort1.Write("AT+CNMI=1,2,0,0,0" + (char)(13));
    }
    #endregion
    #region setiranje timer-a...
    private void timer1_Tick_1(object sender, EventArgs e)
    {
        timer1.Stop();
        getMessage();
        timer1.Start();
    }
    #endregion
}

}

This was only code for testing so it works but there is lot to fix and improve. Basically it will be a nice start for all that are searching something like this...

cheers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top