2013-05-06 141 views
1

請耐心等待看看下面的代碼。我使用QextSerialPort訪問端口無法使用AT命令發送短信

#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調制解調器」。

在我的代碼中,我無法實際發送任何SMS。這是爲什麼?請注意,您在運行此代碼時必須編輯電話號碼。請注意我對QT,USB編程和AT命令非常陌生。我甚至不知道我是否正在訪問正確的端口,因爲有3個端口屬於huwawei。我的輸出如下。

請幫我正確發送短信。

enter image description here

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; 

} 
+0

你看這裏,先試試這個程序嗎? http://www.codeproject.com/Articles/120638/3G-Modem-Internet-Dialer – 2013-05-06 16:46:57

+0

你讀過V.250嗎?你用「\ n」來終止命令,這是錯誤的,V.250明確指出AT命令行應該以「」結尾,例如, 「\ r」(實際上是'S3'值,但應該始終爲13)。 – hlovdal 2013-05-08 18:19:33

回答

2

你的問題如下:

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

永遠的AT命令行發送到調制解調器,你MUST等待後最終結果代碼(例如,通常爲OKERROR雖然還有一些,並且您必須準備好處理所有這些問題。有關如何等待最終結果代碼的示例,您可以查看atinout的源代碼,這是一個用於讀取AT命令列表的小程序,將它們發送到調制解調器並打印響應)。

因爲不等待下面的命令就會中止當前執行的命令。 AT命令的中斷在V.250中的「5.6.1中止命令」一節中定義。如果您在處理AT命令方面經驗不足,則必須閱讀該規範。你也可以在閱讀27.005中使用+ CMG ...命令。您可以在at-command tag information找到指定的鏈接。

對於AT+CMGS具體而言,您還必須在發送文本之前等待「\ r \ n>」,請參閱my other answe r。

+0

感謝您的回覆。我更新了代碼,請看看。你答案中給出的鏈接是用於C程序,我根本不瞭解任何內容! – 2013-05-07 06:32:20

+0

你對該計劃有什麼不瞭解? – hlovdal 2013-05-08 18:16:43

+0

我非常感謝你的幫助。如果沒有使用C++的示例,我無法理解這些東西。我不知道C,實際上我對C++也有點新鮮感。 – 2013-05-08 18:31:49