AN93

cpInBuffRd = WaitForResponse(cpResponse, cpInBuffRd, iTimeoutMs); if(!cpInBuffRd)

exit(0); return cpInBuffRd;

}

//Check for a specific response in the input buffer, and return ptr to what

//follows. If this times out or ERRORs before the response is found then a

//NULL is returned; It keeps reading the ser channel while waiting

char *WaitForResponse(char *cpResponse, char *cpInputBuffer, int iTimeOutInMs)

{

unsigned long ulNoOfbytes;

clock_t

sStartTime = clock();

clock_t

sCurrentTime;

//covert wait time in ms's to clock_t by mutiplying by CLOCKS_PER_SEC/1000

clock_t sWaitTime = (clock_t)(iTimeOutInMs*CLOCKS_PER_SEC)/1000; int iPasses = 0;

int iCharCnt =0;

// set to 0

while(1)

 

{

 

char cTemp = *cpInputWr; *cpInputWr = 0;

char *cpFound = strstr(cpInputBuffer, cpResponse); *cpInputWr = cTemp;

if(cpFound)

{//copy the received bytes for late display

strncpy(cpInput_test, cpInputBuffer, iCharCnt);cpInput_test[iCharCnt]='\0'; return cpFound + strlen(cpResponse);

}

 

// Setup a 50 ms timeout for reads

 

sCOMMTIMEOUTS.ReadIntervalTimeout

= 0;

sCOMMTIMEOUTS.ReadTotalTimeoutMultiplier

= 0;

sCOMMTIMEOUTS.ReadTotalTimeoutConstant

= 50;

sCOMMTIMEOUTS.WriteTotalTimeoutMultiplier

= 0;

sCOMMTIMEOUTS.WriteTotalTimeoutConstant

= 0;

SetCommTimeouts(hCom, &sCOMMTIMEOUTS);

 

//Read the serial port //cpInputWr has char from the port

BOOL bError = !ReadFile(hCom, cpInputWr, 1, &ulNoOfbytes, 0); iCharCount += ulNoOfbytes; iCharCnt+=ulNoOfbytes;

if(bError)

{

strcat(cpErrorString, "Read Error\r\n");

exit(10); // implement a write to file before exit(0)

}

cpInputWr += ulNoOfbytes; // check for a timeout

Rev. 1.3

267

Page 267
Image 267
Silicon Laboratories SI2494/39, SI2493/57/34/15/04 manual Rev 267