Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma systemFile
- #define maxSize 19
- /*
- Developed by T.Friez
- Last Updated: 11/11/2011 5:00pm EST
- Version #: 1.02
- For use with Digi Xbee Radios (Tested with Series 1)
- NXT Hardware: http://www.dexterindustries.com/NXTBee.html
- VEX Hardware: http://www.sparkfun.com/products/9132
- */
- /////////////////////////
- // Initalization Routines
- /////////////////////////
- #if defined(NXT)
- void InitRS485(long nBaudRate = 9600)
- {
- nxtEnableHSPort(); //Enable High Speed Port #4
- nxtSetHSBaudRate(nBaudRate); //Xbee Default Speed
- nxtHS_Mode = hsRawMode; //Set to Raw Mode (vs. Master/Slave Mode)
- }
- #elif defined(VEX2)
- void InitRS232(TUARTs tSelectedUART = uartOne, TBaudRate tSelectBaud = baudRate115200)
- {
- configureSerialPort(tSelectedUART, uartUserControl);
- setBaudRate(tSelectedUART, tSelectBaud);
- }
- #endif
- /////////////////////////
- // Sending a String
- /////////////////////////
- #if defined(NXT)
- void SendString(string sOutgoing)
- {
- char cTemp;
- while(strlen(sOutgoing))
- {
- cTemp = StringGetChar(sOutgoing,0);
- nxtWriteRawHS(cTemp, 1);
- StringDelete(sOutgoing, 0, 1);
- wait1Msec(1);
- }
- wait1Msec(5);
- }
- #elif defined(VEX2)
- void SendString(string sOutgoing)
- {
- while(strlen(sOutgoing))
- {
- sendChar(uartOne, StringGetChar(sOutgoing, 0));
- StringDelete(sOutgoing, 0, 1);
- while(!bXmitComplete(uartOne)) EndTimeSlice();
- }
- wait1Msec(5);
- }
- #endif
- /////////////////////////
- // Receiving a String
- /////////////////////////
- #if defined(NXT)
- void ReceiveString(string &returnString, bool bClearBuffer = true)
- {
- char cProcessing[maxSize];
- ubyte cIncomingChar;
- int nCounter = 0;
- int failCount = 0;
- //Clear out existing array data
- memset(cProcessing, 0, sizeof(cProcessing));
- //Option to clear the receive buffer before waiting for data.
- if(bClearBuffer)
- {
- while(nxtGetAvailHSBytes())
- {
- nxtReadRawHS(cIncomingChar, 1);
- EndTimeSlice();
- }
- }
- //Wait for more data to be available (message to be received)
- while(!nxtGetAvailHSBytes()) EndTimeSlice();
- wait1Msec(1); //Delay required to process data
- //Loop to process the bytes being recieved and add them to the array.
- while(nxtGetAvailHSBytes() && nCounter < (maxSize))
- {
- nxtReadRawHS(cIncomingChar, 1);
- cProcessing[nCounter] = cIncomingChar;
- nCounter++;
- wait1Msec(1);
- }
- //Convert the character array into a ROBOTC style string
- StringFromChars(returnString, cProcessing);
- }
- #elif defined(VEX2)
- void ReceiveString(string &sIncoming, bool bClearBuffer = true)
- {
- char cProcessing[maxSize];
- char incomingChar = -1;
- int nCounter = 0;
- //Clear out existing array data
- memset(cProcessing, 0, sizeof(cProcessing));
- if(bClearBuffer)
- {
- while(incomingChar != -1)
- {
- incomingChar = getChar(uartOne);
- EndTimeSlice();
- }
- }
- //Wait for Incoming Data
- while(incomingChar == -1)
- {
- incomingChar = getChar(uartOne);
- EndTimeSlice();
- }
- //Got a first byte, let's store it in the array
- cProcessing[nCounter] = incomingChar;
- wait1Msec(1); //Delay required to process data
- //Keep looping until we no longer have data.
- while(incomingChar != -1 && nCounter < maxSize)
- {
- incomingChar = getChar(uartOne);
- nCounter++;
- if(incomingChar != -1)
- {
- cProcessing[nCounter] = incomingChar;
- wait1Msec(1);
- }
- }
- StringFromChars(sIncoming, cProcessing);
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement