Here is the code from my application. I hope you find it useful. You also need
to shut down GPRS for this to work. You can set-up the other things like
progress display etc for this to work without any changes, but if GPRS is
shutdown, it is quiet fast.
Vinodh Kumar M. / GreenHex
#define AT_CMD_QUERY_STRING
"AT+CREG=2\r"
#define AT_CMD_RESPONSE_SEARCH_STRING "+CREG: 1,\""
#define AT_CMD_RESPONSE_TIMEOUT_SECONDS 5
/*
* getCellID
*/
static Boolean getCellID(ProgressPtr pProgress, UInt8* LAC, UInt8* CellID) //
locID is 16 characters...
{
Boolean retVal = false;
Err error = errNone;
UInt16 SerialPortID;
Char buf[100];
UInt16 numBytesReceived = 0;
PrgUpdateDialog(pProgress, 0, 4, "", true);
PrgHandleEvent(pProgress, NULL);
/*
#define hsFileCVirtualModemSerLib 'FakM' // Yes : Virtual
Modem
#define hsFileCVirtualGSMSerLib 'VGSM' // Yes : Virtual GSM
#define hsFileCDirectRadioSerLib 'DirR' // Yes : Direct
Radio access SdrvDirectRadio
*/
error = SrmOpen(hsFileCVirtualModemSerLib, 115200, &SerialPortID);
if ((!error) && (SerialPortID))
{
Boolean streamEndFlag = false;
UInt32 timeoutTime = TimGetSeconds() +
AT_CMD_RESPONSE_TIMEOUT_SECONDS; // not more than 5 seconds...
Char* chrLoc = NULL; // location of search string in
modem response
PrgUpdateDialog(pProgress, 0, 5, "", true);
PrgHandleEvent(pProgress, NULL);
SrmSend(SerialPortID, AT_CMD_QUERY_STRING, 10, &error);
if (error)
goto CLOSE_SERIAL;
PrgUpdateDialog(pProgress, 0, 6, "", true);
PrgHandleEvent(pProgress, NULL);
error = SrmSendWait(SerialPortID); // wait until all data is
transmitted... (blocking)
if (error)
goto CLOSE_SERIAL;
do {
UInt32 numBytesInBuffer = 0; // re-initialize
every time.
PrgUpdateDialog(pProgress, 0, 6, "", true);
PrgHandleEvent(pProgress, NULL);
error = SrmReceiveCheck(SerialPortID,
&numBytesInBuffer);
if (error) // can't recover here, yet...
goto CLOSE_SERIAL;
if (numBytesInBuffer > 0)
{
UInt16 i = 0;
for (i = 0 ; i < numBytesInBuffer ; i++)
{
SrmReceive(SerialPortID, (Char*) buf +
numBytesReceived, 1, 0, &error); // get one byte...
++numBytesReceived; // ready for next
char
}
if (StrStr(buf, AT_CMD_RESPONSE_SEARCH_STRING))
// found what we require
streamEndFlag = true;
}
} while ((!error) && (!streamEndFlag) && (timeoutTime >
TimGetSeconds()) && (numBytesReceived < 100)); // artificial exit... have to
fix it.
CLOSE_SERIAL:
if (error == serErrLineErr)
SrmClearErr(SerialPortID);
error = SrmClose(SerialPortID);
if (error) // str stuff
{
PrgUpdateDialog(pProgress, 0, 0, "SerialMgr error.",
true);
PrgHandleEvent(pProgress, NULL);
}
else
{
PrgUpdateDialog(pProgress, 0, 7, "", true);
PrgHandleEvent(pProgress, NULL);
chrLoc = StrStr(buf, AT_CMD_RESPONSE_SEARCH_STRING);
if (chrLoc)
{
MemMove(LAC, chrLoc +
StrLen(AT_CMD_RESPONSE_SEARCH_STRING), 4); // 35
MemMove(CellID, chrLoc +
StrLen(AT_CMD_RESPONSE_SEARCH_STRING) + 7, 4);
if (TxtCharIsAlNum(LAC[0])
&& TxtCharIsAlNum(LAC[1])
&& TxtCharIsAlNum(LAC[2])
&& TxtCharIsAlNum(LAC[3])
&& TxtCharIsAlNum(CellID[0])
&& TxtCharIsAlNum(CellID[1])
&& TxtCharIsAlNum(CellID[2])
&& TxtCharIsAlNum(CellID[3]))
// valid LAC + CellID
{
retVal = true;
}
else
{
PrgUpdateDialog(pProgress, 0, 0,
"Invalid Location.", true);
PrgHandleEvent(pProgress, NULL);
retVal = false;
}
}
}
}
return retVal;
} // getCellID
--
For information on using the ACCESS Developer Forums, or to unsubscribe, please
see http://www.access-company.com/developers/forums/