GetCREG.txt
资源名称:GetCREG.rar [点击查看]
上传用户:lun929
上传日期:2022-05-21
资源大小:1k
文件大小:5k
源码类别:
GPS编程
开发平台:
Visual C++
- 标题:AT命令获取基站小区方法
- 出处:巳度
- 时间:Wed, 03 Feb 2010 21:05:06 +0000
- 作者:eyeman
- 地址:http://www.sduw.com/read.php?21
- 内容:
- #include "stdafx.h"
- #include
- char* GetCREG( char * comPort );
- char* GetCSQ( char * comPort );
- char* GetCREG( char * comPort )
- {
- HANDLE hCom;
- int bufpos;
- DCB dcb;
- COMMTIMEOUTS to;
- DWORD nWritten;
- DWORD event;
- DWORD nRead;
- static char outbuf[20], buf[256];
- BYTE comdevcmd[2]= {0x84, 0x00};
- hCom= CreateFile( L"COM2:" ,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
- if (hCom==NULL || hCom==INVALID_HANDLE_VALUE)
- {
- hCom= NULL;
- return "";
- }
- if (!GetCommState(hCom, &dcb))
- {
- return "ERROR:GetCommState Failed";
- }
- dcb.BaudRate= CBR_115200;
- dcb.ByteSize= 8;
- dcb.fParity= false;
- dcb.StopBits= ONESTOPBIT;
- if (!SetCommState(hCom, &dcb))
- {
- return "ERROR:SetCommState Failed";
- }
- EscapeCommFunction(hCom, SETDTR);
- EscapeCommFunction(hCom, SETRTS);
- GetCommTimeouts(hCom, &to);
- //if (!EscapeCommFunction(hCom, SETDTR))
- //{
- // return "-4";
- //}
- //if (!EscapeCommFunction(hCom, SETRTS))
- //{
- // return "-5";
- //}
- //if (!GetCommTimeouts(hCom, &to))
- //{
- // return "-6";
- //}
- to.ReadIntervalTimeout= 0;
- to.ReadTotalTimeoutConstant= 200;
- to.ReadTotalTimeoutMultiplier= 0;
- to.WriteTotalTimeoutConstant= 20000;
- to.WriteTotalTimeoutMultiplier= 0;
- SetCommTimeouts(hCom, &to);
- //if (!SetCommTimeouts(hCom, &to))
- //{
- // return "-7";
- //}
- if (!SetCommMask(hCom, EV_RXCHAR))
- {
- return "-8";
- }
- DWORD rildevresult=0,nReturned=0;
- if (!DeviceIoControl (hCom,0xAAAA5679L, comdevcmd, sizeof(comdevcmd),0,0,0,0))
- {
- return "-9";
- }
- bufpos = 0;
- strcpy(outbuf,"AT+creg=2r");
- if (!WriteFile(hCom, outbuf, 10, &nWritten, NULL))
- {
- return "-10";
- }
- if (nWritten != 10)
- {
- return "-11";
- }
- if (!WaitCommEvent(hCom, &event, NULL))
- {
- return "-12";
- }
- while(1)
- {
- if (!ReadFile(hCom, buf+bufpos, 256 - bufpos, &nRead, NULL))
- {
- return "-13";
- }
- if (nRead == 0)
- break;
- bufpos += nRead;
- if (bufpos >= 256)
- break;
- }
- strcpy(outbuf,"AT+creg?r");
- if (!WriteFile(hCom, outbuf, 9, &nWritten, NULL))
- {
- return "-14";
- }
- if (nWritten != 9)
- {
- return "-15";
- }
- if (!WaitCommEvent(hCom, &event, NULL))
- {
- return "-16";
- }
- while(1)
- {
- if (!ReadFile(hCom, buf+bufpos, 256 - bufpos, &nRead, NULL))
- {
- return "-17";
- }
- if (nRead == 0)
- break;
- bufpos += nRead;
- if (bufpos >= 256)
- break;
- }
- puts(buf);
- rildevresult = 0;
- if (!EscapeCommFunction(hCom, CLRDTR))
- {
- return "-4";
- }
- if (hCom!=NULL)
- {
- CloseHandle(hCom);
- hCom= NULL;
- }
- char* cregResponse = strpbrk( buf, "CREG " );
- return cregResponse;
- }
- char* GetCSQ( char * comPort )
- {
- HANDLE hCom;
- int bufpos;
- DCB dcb;
- COMMTIMEOUTS to;
- DWORD nWritten;
- DWORD event;
- DWORD nRead;
- static char outbuf[20], buf[256];
- BYTE comdevcmd[2]= {0x84, 0x00};
- hCom= CreateFile( L"COM2:" ,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
- if (hCom==NULL || hCom==INVALID_HANDLE_VALUE)
- {
- hCom= NULL;
- return "";
- }
- if (!GetCommState(hCom, &dcb))
- {
- return "ERROR:GetCommState Failed";
- }
- dcb.BaudRate= CBR_115200;
- dcb.ByteSize= 8;
- dcb.fParity= false;
- dcb.StopBits= ONESTOPBIT;
- if (!SetCommState(hCom, &dcb))
- {
- return "ERROR:SetCommState Failed";
- }
- EscapeCommFunction(hCom, SETDTR);
- EscapeCommFunction(hCom, SETRTS);
- GetCommTimeouts(hCom, &to);
- to.ReadIntervalTimeout= 0;
- to.ReadTotalTimeoutConstant= 200;
- to.ReadTotalTimeoutMultiplier= 0;
- to.WriteTotalTimeoutConstant= 20000;
- to.WriteTotalTimeoutMultiplier= 0;
- SetCommTimeouts(hCom, &to);
- if (!SetCommMask(hCom, EV_RXCHAR))
- {
- return "-8";
- }
- DWORD rildevresult=0,nReturned=0;
- if (!DeviceIoControl (hCom,0xAAAA5679L, comdevcmd, sizeof(comdevcmd),0,0,0,0))
- {
- return "-9";
- }
- bufpos = 0;
- strcpy(outbuf,"AT+CSQr");
- if (!WriteFile(hCom, outbuf, 9, &nWritten, NULL))
- {
- return "-14";
- }
- if (nWritten != 9)
- {
- return "-15";
- }
- if (!WaitCommEvent(hCom, &event, NULL))
- {
- return "-16";
- }
- while(1)
- {
- if (!ReadFile(hCom, buf+bufpos, 256 - bufpos, &nRead, NULL))
- {
- return "-17";
- }
- if (nRead == 0)
- break;
- bufpos += nRead;
- if (bufpos >= 256)
- break;
- }
- puts(buf);
- rildevresult = 0;
- if (!EscapeCommFunction(hCom, CLRDTR))
- {
- return "-4";
- }
- if (hCom!=NULL)
- {
- CloseHandle(hCom);
- hCom= NULL;
- }
- char* csqResponse = strpbrk( buf, "CSQ " );
- return csqResponse;
- }
- Generated by Bo-blog 2.1.1 Release