예제 #1
0
int
waitForETX( int fd, unsigned char *buf, int  *len )
{
  static int pos, loop, val, dlen;
#ifdef IO_DEBUG
  int i;
#endif
  pos = 2; loop = 0; dlen = -1;
  while( loop<MAX_NUM_LOOPS ) {
    val = bytesWaiting( fd );
    if (val>0) {
      read( fd, &(buf[pos]), 1 );
      pos++;
      if (pos>5) {
	dlen = buf[5] + 9; /* std length (9 char) + data length - end chars (2)*/
      }
      if (dlen>0 && pos>=dlen) {
	  if (buf[dlen-2]==B_ESC && buf[dlen-1]==B_ETX) {
	      *len = dlen;
#ifdef IO_DEBUG
	      fprintf( stderr, "- answer ->" );
	      for (i=0;i<pos;i++)
		  fprintf( stderr, "[0x%s%x]", buf[i]<16?"0":"", buf[i] );
	      fprintf( stderr, "\n" );
#else
#ifdef DEBUG
	      fprintf( stderr, "(%d)", *len );
#endif
#endif
	      return(TRUE);
	  } else {
#ifdef IO_DEBUG
	      fprintf( stderr, "- wrong answer ->" );
	      for (i=0;i<pos;i++)
		  fprintf( stderr, "[0x%s%x]", buf[i]<16?"0":"", buf[i] );
	      fprintf( stderr, "\n" );
#else
#ifdef DEBUG
	      fprintf( stderr, "-" );
#endif
#endif
	      return(FALSE);
	  }
      }

    } else {
      usleep(100);
      loop++;
    }
  }
#ifdef IO_DEBUG
  fprintf( stderr, "\n" );
#endif
  return(FALSE);
}
예제 #2
0
bool Socket::waitForData(size_t timeToWait, size_t timeToCheck)
{
  size_t MaxCount = timeToWait / timeToCheck;
  static size_t count = 0;
  while (bytesWaiting() == 0)
  {
    if (++count < MaxCount)
      ::Sleep(timeToCheck);
    else
      return false;
  }
  return true;
}
예제 #3
0
int
waitForAnswer( int fd, unsigned char *buf, int *len )
{
  int loop = 0;
  *len = 0;
  while( loop<MAX_NUM_LOOPS ) {
    if (bytesWaiting( fd )) {
      read( fd, &(buf[*len]), 1 );
      *len = *len+1;
      if (*len>=2 && buf[*len-2]==B_ESC && buf[*len-1]==B_STX ) {
	buf[0] = B_ESC;
	buf[1] = B_STX;
	*len = 2;
	return(waitForETX(fd,buf,len));
      }
    } else {
      usleep(100);
      loop++;
    }
  }
  return(FALSE);
}