How to C# send AT command to GSM modem and get response

9.6k Views Asked by At


I want to get AT command response.
I want to read SMS from my GSM modem.

I fount some code on google, This script send AT commands but not get response.
This is my code:

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

namespace CSharp_SMS
{
    public partial class Form_SMS_Sender : Form
    {
        private SerialPort _serialPort;
        public Form_SMS_Sender()
        {
            InitializeComponent();
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            string number = textBoxNumber.Text;
            string message = textBoxMessage.Text;

            _serialPort = new SerialPort("COM6", 115200);

            Thread.Sleep(1000);

            _serialPort.Open();

            Thread.Sleep(1000);

            _serialPort.WriteLine("AT" + "\r");



            Thread.Sleep(1000);

            string status = _serialPort.ReadLine();

            labelStatus.Text = "|-"+ status + "-|";

            _serialPort.Close();
        }
    }
}


This code is not work.
not get response, but send AT commands.
How to get response?
Thanks

3

There are 3 best solutions below

0
On BEST ANSWER

if ECHO is off on your modem, you won't get any response to your AT commands.

Ensure echo is enabled by sending ATE1 first

Thread.Sleep(1000);
_serialPort.WriteLine("ATE1" + "\r");

Thread.Sleep(1000);
_serialPort.WriteLine("AT" + "\r");
0
On
        celu.RtsEnable = true;
        celu.DtrEnable = true;
        celu.WriteBufferSize = 3000;
        celu.BaudRate = 9600;
        celu.PortName = "COM26"; // You have check what port your phone is using here, and replace it
        celu.Open();
        //string cmd = "AT+CLIP=1";  // Here you put your AT command
        //celu.WriteLine(cmd);
        celu.WriteLine ("AT+CLIP=1"+"\r");

        Thread.Sleep(500);
        //celu.Open();

        string ss = celu.ReadExisting();
0
On

The answer above is not exactly correct. If Echo is off (ATE0), you will still get a response from the modem but you won't have the command you sent echoed back to you.

So with echo on if you send an AT, you'll receive:

AT
OK

With echo off you'll simply receive:

OK