Search code examples
c#at-command

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



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


Solution

  • 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");