无法使用AT命令发送短信

时间:2013-05-06 15:50:18

标签: 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命令发送短信。我的加密狗是Huwawei USB加密狗。无论如何,它被称为“MegaFone调制解调器”。

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

请帮我正确发送短信。

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;

}

1 个答案:

答案 0 :(得分:2)

您的问题如下:

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命令的中止在章节&#34; 5.6.1中止命令&#34;中定义。在V.250。如果您没有处理AT命令的经验,那么必须阅读该规范。你也可以阅读27.005获得你使用的+ CMG ...命令。您可以在at-command tag information找到指向规范的链接。

对于AT+CMGS,您还必须等待&#34; \ r \ n&gt; &#34;在发送文本之前,请参阅my other answe r。