如何用C#向GSM调制解调器发送AT命令并获得响应

问题描述 投票:0回答:3


我想得到 AT 命令响应。
我想从 GSM 调制解调器读取短信。

我在google上找到了一些代码,这个脚本发送AT命令但没有得到响应。
这是我的代码:

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();
        }
    }
}


此代码不起作用。
没有得到响应,但发送 AT 命令。
如何得到回应?
谢谢

c# at-command
3个回答
2
投票

如果调制解调器上的 ECHO 关闭,您将不会收到对 AT 命令的任何响应。

首先发送 ATE1 确保启用回显

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

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

2
投票
        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
投票

上面的答案并不完全正确。如果 Echo 关闭 (ATE0),您仍会收到调制解调器的响应,但不会收到您发送的命令回显给您。

因此,如果您发送 AT,则在 echo 打开的情况下,您将收到:


好的

关闭回声后,您只会收到:

好的

© www.soinside.com 2019 - 2024. All rights reserved.