无法使用AT命令发送短信 [英] Unable to send SMS using AT Commands

查看:559
本文介绍了无法使用AT命令发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请仔细阅读下面的代码。我使用 QextSerialPort 访问端口

Please be kind enough to have a look at the following code. I am using QextSerialPort to access ports

#include <qstring.h>
#include <qdebug.h>
#include <QCoreApplication>



int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
    QextSerialPort *port;
    QString portName;

    int counter=0;

    //Navigate through ports untill you find huwawei USB dongle
    while(counter<ports.size())
    {
     portName = ports[counter].portName;
    QString productId= ports[counter].productID;
    QString physicalName = ports[counter].physName;
    QString vendorId = ports[counter].vendorID;
    QString friendName = ports[counter].friendName;


    string convertedPortName = portName.toLocal8Bit().constData();
    string convertedProductId = productId.toLocal8Bit().constData();
    string convertedPhysicalName = physicalName.toLocal8Bit().constData();
    string convertedVendorId = vendorId.toLocal8Bit().constData();
    string convertedFriendName = friendName.toLocal8Bit().constData();

    cout << "Port Name: " << convertedPortName << endl;
    cout << "Product ID:" << convertedProductId << endl;
    cout << "Physical Name: " << convertedPhysicalName << endl;
    cout << "Vendor Id: " << convertedVendorId << endl;
    cout << "Friend Name: " << convertedFriendName << endl;
    cout << endl;
    counter++;


    //Break if you found Huwawei USB dongle, assign the port to a new port

    if (std::string::npos != convertedFriendName.find("HUAWEI Mobile Connect - 3G Modem"))
    {
      std::cout << "found!" << std::endl;
      port = new QextSerialPort(portName);
      break;
    }
    }


    //Write and send the SMS
    port->open(QIODevice::ReadWrite) ;
    cout << port->isOpen() << endl;
    port->write("AT+CFUN=1");
    port->write("AT+CMGF=1 ");
    port->write("AT+CMGS=1234567");
    port->write("Hello Test SMS");
    //port->write("0x1A");
    port->flush();

    port->close();
    cout << port->isOpen() << endl;

    system("pause");
    return 0;

}

在此代码中,我尝试使用AT发送短信命令。我的dongle是一个Huwawei USB dongle。它被称为MegaFone调制解调器无论如何。

In this code, I am trying to send SMS using AT commands. My dongle is a Huwawei USB dongle. It's known as "MegaFone Modem" anyway.

在我的代码中,我实际上无法发送任何短信。这是为什么?请注意,您必须在运行此代码时编辑电话号码。请注意,我是新的QT,USB编程和AT命令。我甚至不知道是否正在访问正确的端口,因为有3个端口属于huwawei。我的输出如下。

In my code, I am unable to send any SMS actually. Why is that? Please note you have to edit the phone number when you run this code. Please note I am very new to QT, USB Programming and AT commands. I even don't know whether I am accessing the correct port, because there are 3 ports belong to huwawei. My output is as follows.

请帮我正确发送短信。

UPDATE

#include <qstring.h>
#include <qdebug.h>
#include <QCoreApplication>



int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
    QextSerialPort *port;
    QString portName;

    int counter=0;

    //Navigate through ports untill you find huwawei USB dongle
    while(counter<ports.size())
    {
     portName = ports[counter].portName;
    QString productId= ports[counter].productID;
    QString physicalName = ports[counter].physName;
    QString vendorId = ports[counter].vendorID;
    QString friendName = ports[counter].friendName;


    string convertedPortName = portName.toLocal8Bit().constData();
    string convertedProductId = productId.toLocal8Bit().constData();
    string convertedPhysicalName = physicalName.toLocal8Bit().constData();
    string convertedVendorId = vendorId.toLocal8Bit().constData();
    string convertedFriendName = friendName.toLocal8Bit().constData();

    cout << "Port Name: " << convertedPortName << endl;
    cout << "Product ID:" << convertedProductId << endl;
    cout << "Physical Name: " << convertedPhysicalName << endl;
    cout << "Vendor Id: " << convertedVendorId << endl;
    cout << "Friend Name: " << convertedFriendName << endl;
    cout << endl;
    counter++;


    //Break if you found Huwawei USB dongle, assign the port to a new port

    if (std::string::npos != convertedFriendName.find("HUAWEI Mobile Connect - 3G Modem"))
    {
      std::cout << "found!" << std::endl;
      port = new QextSerialPort(portName);
      break;
    }
    }


    //Write and send the SMS
    port->open(QIODevice::ReadWrite) ;
    cout << port->isOpen() << endl;
    port->write("AT+CFUN=1\n");
    cout << "\n";
    port->write("AT+CMGF=1 \n ");
    cout << "\n";
    port->write("AT+CMGS=0776255495\n");
    cout << "\n";
    port->write("Hello Test SMS\n");
    cout << "\n";
    //port->write("0x1A");
    port->flush();

    port->close();
    cout << port->isOpen() << endl;

    system("pause");
    return 0;

}


推荐答案

如下:

port->write("AT+CFUN=1");
port->write("AT+CMGF=1 ");
port->write("AT+CMGS=1234567");
port->write("Hello Test SMS");

始终在向调制解调器发送AT命令行后,必须等待对于最终结果代码(例如通常 OK ERROR ,虽然还有一些,你必须准备处理有关如何等待最终结果代码的示例,您可以查看 atinout

Always after sending a AT command line to the modem, you MUST wait for the final result code (e.g. typically OKor ERROR although there are some more, and you must be prepared to handle all of them. For an example of how to wait for final result codes, you can look at the source code of atinout, which is a tiny program for reading a list of AT commands, send them to the modem and print the responses).

因为没有等待,下面的命令将中止AT命令的列表,这是一个微小的程序,用于读取AT命令列表,将它们发送到调制解调器并打印响应。当前正在执行命令。 AT命令的中止在V.250中的5.6.1中止命令部分中定义。如果你没有处理AT命令的经验,那么必须阅读该规范。此外,你会很好地阅读27.005的+ CMG ...命令使用。您可以在命令标记信息中找到指定的链接。

Because without waiting the following command will abort the currently executing command. Abortion of AT commands is defined in section "5.6.1 Aborting commands" in V.250. If you have little experience with handling AT commands, that specification is a must read. Also you would do well in reading 27.005 for the +CMG... commands you use. You find links to the specifications on the at-command tag information.

对于 AT + CMGS ,您必须在发送文本前等待\r\\\
>,参见其他answe r。

For AT+CMGS specifically you must also wait for "\r\n> " before sending the text, see my other answer.

这篇关于无法使用AT命令发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆