Commands description
updated on 06-11-2009

dsNav communication

Tx - Rx procedures example

TX

An example on how to realize a generic command sending procedure with Processing language.

 

byte[] TxBuff = new byte[144];  // Transmission buffer

int TxHeadLen = 4;              // TX header length (@ + Id + Cmd + CmdLen)

int[] TxIntValue = new int[128]; // int value buffer

 

void TxData(int Id, int Cmd, int ValueLen, int IntFlag)

{/* Transmit a string toward dsNavCon board

         Id         = address of the device, always 0 in our configuration

         Cmd        = Command as described later

         ValueLen   = the number of parameters to send

         IntFlag    = the type of the parameters to send

*/

  int TxBuffCount, ChkSum = 0, CmdLen = 0;

 

  if (IntFlag == 0)  // IntFlag=0 means that a byte variable has to be sent

  {

    CmdLen = ValueLen; 

    for (TxBuffCount = 0; TxBuffCount < ValueLen; TxBuffCount++)

    {

      TxBuff[(TxBuffCount*2)+TxHeadLen] = (byte)(TxIntValue[TxBuffCount]);

    }

  }

  if (IntFlag == 1)  // an integer variable has to be sent

  {

    CmdLen = ValueLen*2;          // 1 int value = 2 bytes to transmit

    for (TxBuffCount = 0; TxBuffCount < ValueLen; TxBuffCount++)

    {

      TxBuff[(TxBuffCount*2)+TxHeadLen] = (byte)(TxIntValue[TxBuffCount] >> 8);

      TxBuff[(TxBuffCount*2)+TxHeadLen+1] = (byte)(TxIntValue[TxBuffCount]);

    }

  }

  if (IntFlag == 2) // a long variable has to be sent

  {

    CmdLen = ValueLen*4;        // 1 long value = 4 bytes to transmit

    for (TxBuffCount = 0; TxBuffCount < ValueLen; TxBuffCount++)

    {

      TxBuff[(TxBuffCount*4)+TxHeadLen] = (byte)(TxIntValue[TxBuffCount] >> 24);

      TxBuff[(TxBuffCount*4)+TxHeadLen+1] = (byte)(TxIntValue[TxBuffCount] >> 16);

      TxBuff[(TxBuffCount*4)+TxHeadLen+2] = (byte)(TxIntValue[TxBuffCount] >> 8);

      TxBuff[(TxBuffCount*4)+TxHeadLen+3] = (byte)(TxIntValue[TxBuffCount]);

    }

  }

  TxBuff[0] = (byte)('@');

  TxBuff[1] = (byte)(Id);

  TxBuff[2] = (byte)(Cmd);

  TxBuff[3] = (byte)(CmdLen+1);  // included CheckSum

  for (i=0;i<(TxHeadLen+CmdLen);i++)

  {

   ChkSum += TxBuff[i];

  }

  TxBuff[TxHeadLen+CmdLen] = (byte)(ChkSum);

 

  for (i=0;i<(TxHeadLen+CmdLen+1);i++)

  {

    RS232Port.write(TxBuff[i]);

  }

 }

 

After filling TxIntValue[] buffer with the desired values, the above described TxData() procedure can be called.

Example:

TxIntValue[0] = DesCoordX;

TxIntValue[1] = DesCoordY;

TxData(0, 'P', 2, 1);

Means: send to device with Id=0 the command 'P' gettting 2 values from TX buffer as integer

RX

An example of a function usable to receive commands from dsNav. This function controls the command packet received for coherence (correct header + known command + checksum ok) and returns TRUE to the calling procedure only if everything's ok:

 

boolean RxData(char Cmd,int Len)

{

  int ChkSum = 0;

  for (i=0; i < Len+HeadLen+1; i++) // loops for all data expected and only for that

  {

     RxBuff[i] = (RS232Port.read()); // fills the receive buffer

  }

     

  if (RxBuff[0] != '@') // is the header correct, is that the correct byte sequence ?

  {

     RS232Port.clear();

     return false;

  }

  else if (RxBuff[2] != Cmd) // is that the expected command ?

  {

     RS232Port.clear();

     return false;

  }

  else if (RxBuff[3] != (Len+1)) // does it send the right number of characters ?

  {

     RS232Port.clear();

     return false;

  }

     

  for (i=0; i < Len+HeadLen; i++)  //  ChkSum excluded

  {

    ChkSum += (char)(RxBuff[i]);

  }

  ChkSum = ChkSum % 256;

  if (RxBuff[i] != ChkSum) // is the received checksum equal to the computed one ?

  {

    RS232Port.clear();

   return false;

  }

/* if everythingÕs ok the buffer is analyzed for the expected data by the calling procedure, otherwise the serial port is cleared and a FALSE condition is returned

*/

  return true;

}

 

Calling the above RxData() procedure the RX buffer is filled with the received data. If the function returns TRUE to the calling procedure, this buffer is analyzed for the expected values.

Example:

if (RxData('A',13))

{

 MesSpeed = ((RxBuff[HeadLen] << 8(RxBuff[HeadLen+1]));// two bytes form an integer variable

 Current = (float)((RxBuff[HeadLen+2] << 8) + (RxBuff[HeadLen+3]));

 Xpos = ((RxBuff[HeadLen+4] << 8) + (RxBuff[HeadLen+5]));

 Ypos = ((RxBuff[HeadLen+6] << 8) + (RxBuff[HeadLen+7]));

 MesAngle = ((RxBuff[HeadLen+8] << 8) + (RxBuff[HeadLen+9]));

 CompassAngle = ((RxBuff[HeadLen+10] << 8) + (RxBuff[HeadLen+11])) / 10;

 IdlePerc = RxBuff[HeadLen+10];

}