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

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

问题描述

我正在使用 QextSerialPort 访问端口

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 命令发送 SMS.我的加密狗是华为 USB 加密狗.无论如何,它被称为MegaFone 调制解调器".

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

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

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. 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 Huawei. My output is as follows.

更新

#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
");
    cout << "
";
    port->write("AT+CMGF=1 
 ");
    cout << "
";
    port->write("AT+CMGS=0776255495
");
    cout << "
";
    port->write("Hello Test SMS
");
    cout << "
";
    //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 命令行之后,您必须等待最终结果代码(例如,通常是 OKERROR还有一些,你必须准备好处理所有的.关于如何等待最终结果代码的例子,你可以看atinout,这是一个用于读取 AT 命令列表、将它们发送到调制解调器并打印响应的小程序.

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 命令的中止在 V.250 中的5.6.1 中止命令"一节中定义.如果您对处理 AT 命令的经验很少,那么该规范是必读的.你也可以很好地阅读 27.005 的 +CMG... 命令.您可以在 at-command 标签信息上找到规范的链接.

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.

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

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

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