Google
 
Web ppczone.net

View Full Version : how to let AT commands work on Windows Mobile 5?


GAO Hai
01-29-2007, 05:11 AM
Dear Sir/madam,

I use the following code to retrieve Cell ID:

long CGetCellIDDlg::FetchCellID(LPTSTR outData, DWORD & returnLAC, DWORD &
returnCellID)
{
HANDLE hCom;
char * xpos;
char rsltstr[5];
DWORD returnValue;
DWORD LAC;
DWORD CellId;
int bufpos;
DCB dcb;
COMMTIMEOUTS to;
DWORD nWritten;
DWORD event;
DWORD nRead;
char outbuf[20], buf[256];

BYTE comdevcmd[2]= {0x84, 0x00};


hCom=
CreateFile(L"\\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0) ;
//for windows Mobile 5 and pocket PC 2003
if (hCom==NULL || hCom==INVALID_HANDLE_VALUE)
{
TCHAR szBuf[80];
DWORD dw = GetLastError();

// get the most uptodate cells
_stprintf(szBuf, TEXT("CreateFile failed with error %d."), dw);

MessageBox(szBuf, TEXT("Error"), MB_OK);

hCom= NULL;
return -1;
}

if (!GetCommState(hCom, &dcb))
{
return -2;
}

dcb.BaudRate= CBR_115200;
dcb.ByteSize= 8;
dcb.fParity= false;
dcb.StopBits= ONESTOPBIT;

if (!SetCommState(hCom, &dcb))
{
return -3;
}

if (!EscapeCommFunction(hCom, SETDTR))
{
return -4;
}

if (!GetCommTimeouts(hCom, &to))
{
return -6;
}
to.ReadIntervalTimeout= 0;
to.ReadTotalTimeoutConstant= 200;
to.ReadTotalTimeoutMultiplier= 0;
to.WriteTotalTimeoutConstant= 20000;
to.WriteTotalTimeoutMultiplier= 0;
if (!SetCommTimeouts(hCom, &to))
{
return -7;
}

if (!SetCommMask(hCom, EV_RXCHAR))
{
return -8;
}



if (!DeviceIoControl (hCom,0xAAAA5679L, comdevcmd,
sizeof(comdevcmd),0,0,0,0))
{
TCHAR szBuf[80];
DWORD dw = GetLastError();

// get the most uptodate cells
_stprintf(szBuf, TEXT("DeviceIoControl failed with error %d."), dw);

MessageBox(szBuf, TEXT("Error"), MB_OK);

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;
}

buf[bufpos] = 0;

mbstowcs(outData,buf,bufpos+1);

//if ((xpos = strstr(buf,"CREG")) == NULL)
xpos = strstr(buf,"CREG: 2, 1, ");
if(xpos==NULL)
{
xpos = strstr(buf,"CREG: 2, 5, ");
if(xpos==NULL)
{
returnValue = -19;
}
}

//xpos!=NULL;
memcpy(rsltstr,xpos+18,4);
rsltstr[4] = 0;
if (sscanf(rsltstr,"%X",&CellId) != 1)
{
returnValue = -20;
}
else
{
returnCellID=CellId;
returnValue=CellId;

memcpy(rsltstr,xpos+12,4);
rsltstr[4] = 0;
if (sscanf(rsltstr,"%X",&LAC) != 1)
{
returnValue = -21;
}
else
{
returnLAC=LAC;
}
}

if (hCom!=NULL)
{
CloseHandle(hCom);
hCom= NULL;
}
return returnValue;
}


The above code works very well on Pocket PC 2003. However, on Windows mobile
5, the call to DeviceIoControl fails. Error Code: 87 (The parameter is
incorrect).

Could anybody tell how to let AT commands work on Windows Mobile 5?

--
GAO Hai