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

时间:2022-11-09 09:12:22

I have write small programm on C++, she switch modem 2G\3G mode. its not work :-(

我在c++编写了一个小程序,她切换了modem 2G\3G模式。它不工作:-(

progrm read data form modem, if send AT-Comands modem not answer.

程序读取数据格式调制解调器,如果发送AT-Comands调制解调器不应答。

please help me ;-)

请帮助我;-)

// 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 个解决方案

#1


2  

This line

这条线

DWORD dwSize = sizeof(data);

sets dwSize to the size of the string including the null-character at the end, which I don't think you want to send. And the command must end with the \r character. Try:

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

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

(See hlovdal's comment below for reference. Also http://en.wikipedia.org/wiki/Hayes_command_set#The_basic_Hayes_command_set.)

(参见下面的hlovdal的评论,以供参考。同时http://en.wikipedia.org/wiki/Hayes_command_set The_basic_Hayes_command_set)。

#1


2  

This line

这条线

DWORD dwSize = sizeof(data);

sets dwSize to the size of the string including the null-character at the end, which I don't think you want to send. And the command must end with the \r character. Try:

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

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

(See hlovdal's comment below for reference. Also http://en.wikipedia.org/wiki/Hayes_command_set#The_basic_Hayes_command_set.)

(参见下面的hlovdal的评论,以供参考。同时http://en.wikipedia.org/wiki/Hayes_command_set The_basic_Hayes_command_set)。