Tools/Firmware/DynamixelGateway/Host/DynamixelGateway.cc
//Receive a packet.
//Returns length of packet if successfully received
//Returns zero or negative otherwise
//The packet is stored in this->dpacket
Receive a packet.
Returns length of packet if successfully received
Returns zero or negative otherwise
The packet is stored in this->dpacket
Receive a packet . Returns length of packet if successfully received Returns zero or negative otherwise The packet is stored in this - > dpacket
int DynamixelGateway::ReceivePacket()
{
  CHECK_CONNECTION;
  int ret = -1;
  int nChars;
  DynamixelPacketInit(this->dpacket);
  for(int i=0;i<4;i++)
    this->receiveBuf[i]=0;
  int nRead = this->sd->ReadChars((char*)this->receiveBuf,4,DEF_CHAR_TIMEOUT_US);
  if (nRead != 4 || (this->receiveBuf[0] != 0xFF) || (this->receiveBuf[1] != 0xFF) )
  {
    printf("could not read packet header\n");
    return 0;
  }
  for (int ii=0; ii<4; ii++)
    DynamixelPacketProcessChar(this->receiveBuf[ii],this->dpacket);
  int nToRead = this->receiveBuf[3];
  while(ret < 0)
  {
    nChars = this->sd->ReadChars((char*)this->receiveBuf,
                nToRead, DEF_CHAR_TIMEOUT_US);
    if (nChars > 0)
    {
      for (int ii=0; ii<nChars; ii++)
      {
        ret = DynamixelPacketProcessChar(this->receiveBuf[ii],
                                         this->dpacket);
        if (ret > 0)  
          break;
      }
    }
    else
      break;
  }
  return ret;
}
int DynamixelGateway :: ReceivePacket ( ) { CHECK_CONNECTION ; int ret = -1 ; int nChars ; DynamixelPacketInit ( this -> dpacket ) ; for ( int i = 0 ; i < 4 ; i ++ ) this -> receiveBuf [ i ] = 0 ; int nRead = this -> sd -> ReadChars ( ( char * ) this -> receiveBuf , 4 , DEF_CHAR_TIMEOUT_US ) ; if ( nRead != 4 || ( this -> receiveBuf [ 0 ] != 0xFF ) || ( this -> receiveBuf [ 1 ] != 0xFF ) ) { printf ( " \n " ) ; return 0 ; } for ( int ii = 0 ; ii < 4 ; ii ++ ) DynamixelPacketProcessChar ( this -> receiveBuf [ ii ] , this -> dpacket ) ; int nToRead = this -> receiveBuf [ 3 ] ; while ( ret < 0 ) { nChars = this -> sd -> ReadChars ( ( char * ) this -> receiveBuf , nToRead , DEF_CHAR_TIMEOUT_US ) ; if ( nChars > 0 ) { for ( int ii = 0 ; ii < nChars ; ii ++ ) { ret = DynamixelPacketProcessChar ( this -> receiveBuf [ ii ] , this -> dpacket ) ; if ( ret > 0 ) break ; } } else break ; } return ret ; }
Receive a packet.
Receive a packet .
//read off the packet header //process one char at a time //printf("%02x ",(uint8_t)this->receiveBuf[ii]); //got complete packet