請耐心等待看看下面的代碼。我使用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。我的輸出如下。
請幫我正確發送短信。
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;
}
你看這裏,先試試這個程序嗎? http://www.codeproject.com/Articles/120638/3G-Modem-Internet-Dialer – 2013-05-06 16:46:57
你讀過V.250嗎?你用「\ n」來終止命令,這是錯誤的,V.250明確指出AT命令行應該以「」結尾,例如, 「\ r」(實際上是'S3'值,但應該始終爲13)。 –
hlovdal
2013-05-08 18:19:33