如何使用C ++在usb调制解调器华为E171上切换2G \ 3G模式?

时间:2014-05-02 17:44:49

标签: c++ usb at-command modem

我在C ++上编写了小程序,她切换调制解调器2G \ 3G模式。 它不起作用: - (

progrm从调制解调器读取数据,如果发送AT-Comands调制解调器没有应答。

请帮助我; - )

// huawei_mode_switcher 
#include <windows.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){

    LPCTSTR sPortName = "//./COM13";
    char data[] = "AT^SYSCFG=13,1,3FFFFFFF,2,4";
    DWORD dwSize = sizeof(data); 
    DWORD dwBytesWritten;    
    HANDLE hSerial = CreateFile(sPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

    if(hSerial==INVALID_HANDLE_VALUE){

        if(GetLastError()==ERROR_FILE_NOT_FOUND)
    {
        cout << "com port zanyat\n";
    }
    cout << "other error\n";

}
    else {

    BOOL iRet = WriteFile (hSerial,data,dwSize,&dwBytesWritten,NULL);

    Sleep(100);
        while(1)
    {
        DWORD iSize;
      char sReceivedChar;
      while (true)
      {
            ReadFile(hSerial, &sReceivedChar, 1, &iSize, 0); 
            if (iSize > 0)   
                cout << sReceivedChar;
      }

    }

}


system("pause");
return 0;
}

1 个答案:

答案 0 :(得分:2)

这一行

DWORD dwSize = sizeof(data);

dwSize设置为字符串的大小,包括末尾的空字符,我认为你不想发送它。命令必须以\r字符结尾。尝试:

char data[] = "AT^SYSCFG=13,1,3FFFFFFF,2,4\r";
DWORD dwSize = strlen(data);    // use strlen instead of sizeof

(请参阅下面的hlovdal评论以供参考。另外http://en.wikipedia.org/wiki/Hayes_command_set#The_basic_Hayes_command_set。)