2013-05-06 2 views
1

다음 코드를 살펴 보시기에 충분히 친절하십시오. 나는 AT 명령을 사용하여 SMS를 보내려고하고, 포트를이 코드에서 AT 명령을 사용하여 SMS를 보낼 수 없습니다.

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

} 

에 액세스 할 수 QextSerialPort을 사용하고 있습니다. 내 동글은 Huwawei USB 동글입니다. 그것은 어쨌든 "메가 폰 모뎀"으로 알려져 있습니다.

제 코드에서는 실제로 SMS를 보낼 수 없습니다. 왜 그런가요? 이 코드를 실행할 때 전화 번호를 편집해야합니다. QT, USB 프로그래밍 및 AT 명령어가 새로 도입되었습니다. 심지어 huwawei에 속하는 3 개의 포트가 있기 때문에 나는 올바른 포트에 액세스하고 있는지 여부를 모른다. 내 출력은 다음과 같습니다.

SMS를 올바르게 보내도록 도와주세요.

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 –

+0

V.250을 아직 읽었습니까? "\ n"이 잘못된 명령을 종료합니다. V.250은 AT 명령 줄을 ''으로 끝내야한다고 명시합니다. "\ r"(실제로는 'S3'값이지만 항상 13이어야합니다). – hlovdal

답변

2

귀하의 문제는 다음

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

항상 모뎀에 대한, 당신은 MUST 대기를 AT 명령 줄을 보낸 후 최종 결과 코드 (예 : 일반적으로 OK 또는)에는 몇 가지가 있지만 모두 처리 할 수 ​​있도록 준비해야합니다. 최종 결과 코드를 기다리는 방법의 예는 소스 코드 atinout을 볼 수 있습니다.이 프로그램은 AT 명령 목록을 읽고 모뎀에 보내고 응답을 인쇄하는 작은 프로그램입니다.

대기하지 않고 다음 명령은 현재 실행중인 명령을 중단합니다. AT 명령의 중단은 V.250의 "5.6.1 명령 취소"절에 정의되어 있습니다. AT 명령어를 다루는 경험이 거의 없다면, 그 스펙을 반드시 읽어야합니다. 또한 사용하는 + CMG ... 명령에 대해 27.005를 읽는 것이 좋습니다. at-command tag information에서 사양에 대한 링크를 찾을 수 있습니다.

AT+CMGS 특히 텍스트를 보내기 전에 "\ r \ n>"을 기다려야합니다 (my other answe 참조).

+0

답장을 보내 주셔서 감사합니다. 코드를 업데이트했습니다. 한번보세요. 당신의 대답에 주어진 링크는 C 프로그램을위한 것입니다. 나는 전혀 이해하지 못했습니다! –

+0

프로그램에 대해 무엇을 이해하지 못하셨습니까? – hlovdal

+0

정말 고맙습니다. C++의 예제를 사용하지 않고도 이러한 것들을 이해할 수 없습니다. 나는 C를 모르지만 사실 C++에 조금 익숙하지 않다. –

관련 문제