c++ - 无法使用 AT 命令发送短信

标签 c++ qt sms usb at-command

我正在使用 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 命令发送 SMS。我的加密狗是华为USB加密狗。无论如何,它被称为“MegaFone 调制解调器”。

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

enter image description here

更新

#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 命令行后,您必须等待最终结果代码(例如通常是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++ - 无法使用 AT 命令发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16402403/

相关文章:

c++ - 如何使用Qt检查路径中是否存在程序?

c++ - 空的括号初始化列表发出有关缺少字段初始化器的警告

c++ - 如何在 ZeroMQ(C++) 中使用 XPUB 和 XSUB 实现带有代理的 Pub-Sub 网络?

c++ - QMainWindow::keyReleaseEvent 未按预期工作

c++ - 使用带有 qt c++ 的 mavlink 协议(protocol)从串口读取

php - 如何编码法语/西类牙语字符以使用 clickatell 的 http api?

c++ - 运算符 [] 不匹配

c++ - 让回调记住一些信息的好方法(C++)

android - 发送短信的号码数量有限制吗?

python - 短信文本解决方案