Beispiel #1
0
void OpSyncDataQueue::Shutdown()
{
    OP_NEW_DBG("OpSyncDataQueue::Shutdown()", "sync");
#ifdef SYNC_QUEUE_ON_DISK
    StopTimeout();
    WriteQueue();
#endif // SYNC_QUEUE_ON_DISK

    ClearCollection(m_items_to_send);
    ClearCollection(m_item_queue);
    ClearCollection(m_received_items);
}
int SerialCortex::CheckTimeout( long *t )
{      
  // Is timeout tunning
  if( *t > 0 )
  {
    if(millis() - *t < 10)
      return(0);     
    else
    {
      StopTimeout( t );
      //Serial.write("timeout\n");
      return(1);
    }       
  }

  return(0);
}
/*-----------------------------------------------------------------------------*/
void SerialCortex::serialRxTask()
{
  static  int  serialRxState      = 0;
  static  int  serialDataReceived = 0;
  static  long timeout            = 0;
  int     cs;
  int     c;

  // check for a received character
  if( (c = serialRxChar()) < 0 )
  {
    // no received char 

    // Check for inter char timeout
    if( CheckTimeout(&timeout) == 1 )
      serialRxState = 0;

    // done
    return;
  }

  // A new character has been received so process it.

  // Start inter char timeout
  StartTimeout( &timeout );  

  // Where are we in the message parsing process ?
  switch( serialRxState )
  {
  case  kRxStateIdle:
    // look for first header byte
    if( c == HEADER_PREAMBLE_1 )
      serialRxState = kRxStateHeader;
    else
      StopTimeout(&timeout);
    // for debug
    MyVexDataRx.header.header_p1 = c;
    break;

  case  kRxStateHeader:
    // look for second header byte
    if( c == HEADER_PREAMBLE_2 )
      serialRxState = kRxStateMessageType;
    else
    {
      // Bad message
      StopTimeout(&timeout);
      serialRxState = kRxStateIdle;
    }
    // for debug
    MyVexDataRx.header.header_p2 = c;
    break;

  case  kRxStateMessageType:
    // We have a good header so next is message type
    MyVexDataRx.header.message_type = c;
    serialRxState = kRxStateDatalen;
    break;

  case  kRxStateDatalen:
    // next is data length byte
    MyVexDataRx.header.datalen = c;
    serialDataReceived = 0;
    serialRxState = kRxStateId;
    break;

  case  kRxStateId:
    // next is packet id byte
    MyVexDataRx.header.id = c;
    serialRxState = kRxStatePad;
    break;

  case  kRxStatePad:
    // next is an unused word alignment byte
    serialRxState = kRxStateData;
    break;

  case  kRxStateData:
    // receive the data packet
    MyVexDataRx.buffer[ serialDataReceived ] = c;
    if( ++serialDataReceived == MyVexDataRx.header.datalen )
      serialRxState = kRxStateChecksum;
    // check for buffer overflow
    if( serialDataReceived >= sizeof(MyVexDataRx.buffer) )
    {
      // Error
      StopTimeout(&timeout);
      serialRxState = kRxStateIdle;
    }            
    break;

  case  kRxStateChecksum:
    // Received checksum byte

    // stop timeout
    StopTimeout( &timeout );

    // calculate received checksum
    cs = VexDataChecksum( &MyVexDataRx );

    // comapare checksums
    if( cs == c )
    {
      // Good data
#ifdef  DEBUG
      VexDataPrint(&MyVexDataRx);
#endif
      serialRxDecode(&MyVexDataRx);
    }
    else
    {
      // Bad data
#ifdef  DEBUG
      Serial.write("bad data\n");
#endif
    }

    // done
    serialRxState = kRxStateIdle;
    break;

  default:
    break;
  }  
}