【问题标题】:Unable to send SMS using AT Commands无法使用 AT 命令发送 SMS
【发布时间】:2013-04-30 10:11:03
【问题描述】:

我正在使用QextSerialPort 访问端口

#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 命令发送短信。我的加密狗是华为 USB 加密狗。无论如何,它被称为“MegaFone Modem”。

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

更新

#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;

}

【问题讨论】:

  • 你看这里并首先尝试这个程序吗? codeproject.com/Articles/120638/3G-Modem-Internet-Dialer
  • 你读过 V.250 了吗?您正在用“\n”终止命令,这是错误的,V.250 明确表示 AT 命令行应该以&lt;CR&gt; 终止,例如"\r"(实际上是 S3 值,但应该始终为 13)。

标签: c++ qt sms usb at-command


【解决方案1】:

您的问题如下:

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

总是在向调制解调器发送 AT 命令行后,您必须等待最终结果代码(例如通常为OKERROR,尽管还有更多,您必须做好准备处理所有这些。关于如何等待最终结果代码的示例,您可以查看atinout 的源代码,这是一个读取 AT 命令列表的小程序,将它们发送到调制解调器并打印响应)。

因为不等待以下命令将中止当前正在执行的命令。 AT 命令的中止在 V.250 的“5.6.1 中止命令”部分中定义。如果您对处理 AT 命令没有什么经验,那么该规范是必须阅读的。此外,您可以很好地阅读 27.005 以了解您使用的 +CMG... 命令。您可以在 at-command tag information 上找到规范的链接。

对于AT+CMGS,您还必须在发送文本之前等待“\r\n>”,请参阅my other answer。

【讨论】:

  • 感谢您的回复。我更新了代码,请看一下。您的答案中给出的链接是针对 C 程序的,我根本什么都不懂!
  • 你对这个程序有什么不明白的地方?
  • 非常感谢您的帮助。如果没有 C++ 的工作示例,我将无法理解这些东西。我不懂C,其实我对C++也有点陌生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
相关资源
最近更新 更多