daily pastebin goal
23%
SHARE
TWEET

XbeeTools.h

CMUmulti Nov 28th, 2011 10 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma systemFile
  2. #define maxSize 19
  3.  
  4. /*
  5. Developed by T.Friez
  6. Last Updated: 11/11/2011 5:00pm EST
  7. Version #: 1.02
  8. For use with Digi Xbee Radios (Tested with Series 1)
  9. NXT Hardware: http://www.dexterindustries.com/NXTBee.html
  10. VEX Hardware: http://www.sparkfun.com/products/9132
  11. */
  12.  
  13. /////////////////////////
  14. // Initalization Routines
  15. /////////////////////////
  16. #if defined(NXT)
  17. void InitRS485(long nBaudRate = 9600)
  18. {
  19.   nxtEnableHSPort();            //Enable High Speed Port #4
  20.   nxtSetHSBaudRate(nBaudRate);  //Xbee Default Speed
  21.   nxtHS_Mode = hsRawMode;       //Set to Raw Mode (vs. Master/Slave Mode)
  22. }
  23. #elif defined(VEX2)
  24. void InitRS232(TUARTs tSelectedUART = uartOne, TBaudRate tSelectBaud = baudRate115200)
  25. {
  26.   configureSerialPort(tSelectedUART, uartUserControl);
  27.   setBaudRate(tSelectedUART, tSelectBaud);
  28. }
  29. #endif
  30.  
  31. /////////////////////////
  32. // Sending a String
  33. /////////////////////////
  34. #if defined(NXT)
  35. void SendString(string sOutgoing)
  36. {
  37.   char cTemp;
  38.   while(strlen(sOutgoing))
  39.   {
  40.     cTemp = StringGetChar(sOutgoing,0);
  41.     nxtWriteRawHS(cTemp, 1);
  42.     StringDelete(sOutgoing, 0, 1);
  43.     wait1Msec(1);
  44.   }
  45.   wait1Msec(5);
  46. }
  47. #elif defined(VEX2)
  48. void SendString(string sOutgoing)
  49. {
  50.   while(strlen(sOutgoing))
  51.   {
  52.     sendChar(uartOne, StringGetChar(sOutgoing, 0));
  53.     StringDelete(sOutgoing, 0, 1);
  54.     while(!bXmitComplete(uartOne)) EndTimeSlice();
  55.   }
  56.   wait1Msec(5);
  57. }
  58. #endif
  59.  
  60. /////////////////////////
  61. // Receiving a String
  62. /////////////////////////
  63. #if defined(NXT)
  64. void ReceiveString(string &returnString, bool bClearBuffer = true)
  65. {
  66.   char cProcessing[maxSize];
  67.   ubyte cIncomingChar;
  68.   int nCounter = 0;
  69.   int failCount = 0;
  70.  
  71.   //Clear out existing array data
  72.   memset(cProcessing, 0, sizeof(cProcessing));
  73.  
  74.   //Option to clear the receive buffer before waiting for data.
  75.   if(bClearBuffer)
  76.   {
  77.     while(nxtGetAvailHSBytes())
  78.     {
  79.       nxtReadRawHS(cIncomingChar, 1);
  80.       EndTimeSlice();
  81.     }
  82.   }
  83.  
  84.   //Wait for more data to be available (message to be received)
  85.   while(!nxtGetAvailHSBytes()) EndTimeSlice();
  86.  
  87.   wait1Msec(1); //Delay required to process data
  88.  
  89.   //Loop to process the bytes being recieved and add them to the array.
  90.   while(nxtGetAvailHSBytes() && nCounter < (maxSize))
  91.   {
  92.     nxtReadRawHS(cIncomingChar, 1);
  93.     cProcessing[nCounter] = cIncomingChar;
  94.     nCounter++;
  95.     wait1Msec(1);
  96.   }
  97.   //Convert the character array into a ROBOTC style string
  98.   StringFromChars(returnString, cProcessing);
  99. }
  100.  
  101. #elif defined(VEX2)
  102. void ReceiveString(string &sIncoming, bool bClearBuffer = true)
  103. {
  104.   char cProcessing[maxSize];
  105.   char incomingChar = -1;
  106.   int nCounter = 0;
  107.  
  108.   //Clear out existing array data
  109.   memset(cProcessing, 0, sizeof(cProcessing));
  110.  
  111.   if(bClearBuffer)
  112.   {
  113.     while(incomingChar != -1)
  114.     {
  115.       incomingChar = getChar(uartOne);
  116.       EndTimeSlice();
  117.     }
  118.   }
  119.  
  120.   //Wait for Incoming Data
  121.   while(incomingChar == -1)
  122.   {
  123.     incomingChar = getChar(uartOne);
  124.     EndTimeSlice();
  125.   }
  126.  
  127.   //Got a first byte, let's store it in the array
  128.   cProcessing[nCounter] = incomingChar;
  129.  
  130.   wait1Msec(1); //Delay required to process data
  131.  
  132.   //Keep looping until we no longer have data.
  133.   while(incomingChar != -1 && nCounter < maxSize)
  134.   {
  135.     incomingChar = getChar(uartOne);
  136.     nCounter++;
  137.     if(incomingChar != -1)
  138.     {
  139.       cProcessing[nCounter] = incomingChar;
  140.       wait1Msec(1);
  141.     }
  142.   }
  143.   StringFromChars(sIncoming, cProcessing);
  144. }
  145. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top