while (1)
{
char buffer[40]; /* buffer to accumulate DMM chars in */
DWORD now;
int n,cnt;
/* Set up interval to wait for next sample */
next_read+=gSampleInterval;
now=TimeTick;
if (now <next_read)
OSTimeDly(next_read-now);
/* An RTOS function that yields for some number of ticks */
else
next_read=TimeTick;
write(fddvm,"\r",1); /* Ask DVM for a new sample */
cnt=0;
/* Read data from DVM */
while(1)
{
n=ReadWithTimeout(fddvm,buffer+cnt,40-cnt,TICKS_PER_SECOND);
if (n <=0)
{/* DVM is probably off */
next_read=TimeTick+gSampleInterval;
DVMState=DvmDead;
break;
}
else
{
cnt+=n;
buffer[cnt]=0;
if (buffer[cnt-1]=='\r')
{
if ((strncmp(buffer,"TEMP",4)==0))
{/* Last DVM reading was a Temperature */
int t;
t= atoi(buffer+5);
DVMState=DvmOk;
StoreSample(t);
}
else
{
DVMState=DvmSetWrong;
}
break;
}
}
}/* While reading */
}/* While forever */
Listing 1This code gathers samples from the DVM and stores them in a circular buffer. It also detects errors in the DVM connectivity
and settings.