WM5.0下如何获取基站信息(LAC、Cellid)

WM5.0下如何获取基站信息(LAC、Cellid)

在WM5.0下获得CELLID、LAC的途径有两条:利用串口发送AT指令或是利用RIL来获取。RIL(Radio Interface Layer)是微软自己开发的一个库,它的程序有固有的特点,在获取CELLID上,它其实是对第一种方法的封装,两者本质是一样的。但要注意是:串口一旦打开,就难以关闭,除非重启机器(可能涉及到底层的中断),另外并不是所有的设备都可以取到CELLID。
下面的这段代码就是利用COM口来获取CELLID,并不保证所有的设备都支持。

typedef struct
{
 char c[12];
 char    iLac[4];
 char s[4];
 char iId[4];
} TCREG_DATA;

void Get_Cellid(void)
{
 char m_sTemp[12] = {0};
 strcat(m_sTemp,"COM");
 for(int i = 9; i > 0; -- i)
 {
  char ch1;
  _itoa(i,&ch1,10);
  strcat(m_sTemp,&ch1);
  strcat(m_sTemp,":");

  TCREG_DATA* pData = (TCREG_DATA*)GetCREG(m_sTemp);
  if(!pData)
   continue;

  char szNum1[8] = {0};
  char szNum2[8] = {0};

  strcpy(szNum1,pData->iLac);
  strcpy(szNum2,pData->iId);

  int iLac = (int)strtol(szNum1,NULL,16);
  int iId  = (int)strtol(szNum2,NULL,16);

  if (iLac && iId)
  {
   sprintf(m_sCell.LAC,"%06d",iLac);
   sprintf(m_sCell.ID,"%06d",iId);
   break;
  }
 }
}
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};
 WCHAR m_sCom[12] = {0};
 mbstowcs(m_sCom,comPort,strlen(comPort));

 hCom= CreateFile( m_sCom ,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);

 if (hCom==NULL || hCom==INVALID_HANDLE_VALUE)
 {
  hCom= NULL;
  return NULL;
 }

 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=2\r");

 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\0" );

 return cregResponse;
}

void Get_Cellid(void)
{
 char m_sTemp[12] = {0};
 strcat(m_sTemp,"COM");
 for(int i = 9; i > 0; -- i)
 {
  char ch1;
  _itoa(i,&ch1,10);
  strcat(m_sTemp,&ch1);
  strcat(m_sTemp,":");

  TCREG_DATA* pData = (TCREG_DATA*)GetCREG(m_sTemp);
  if(!pData)
   continue;

  char szNum1[8] = {0};
  char szNum2[8] = {0};

  strcpy(szNum1,pData->iLac);
  strcpy(szNum2,pData->iId);

  int iLac = (int)strtol(szNum1,NULL,16);
  int iId  = (int)strtol(szNum2,NULL,16);

  if (iLac && iId)
  {
   sprintf(m_sCell.LAC,"%06d",iLac);
   sprintf(m_sCell.ID,"%06d",iId);
   break;
  }
 }
}

你可能感兴趣的:(WM5.0下如何获取基站信息(LAC、Cellid))