ok, I'll try to explain you the concept part..if you then tell me more precisely what you really need I can help in the details of twisting this function to your own needs.
1st - declaration of the arraychar serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won't work properly
this is important to store long information coming from the serial buffer all in one variable.
if you know the length of your string you can change the number 100 with watever suits you best:
you said that you need 2 bytes = 2x8bit = 16 bits + the endofline or a separator chars
just put that number instead of 100
BEWARE of the type declaration, here the array is declared as a "char", but you can make it an int if you want just numbers to be stored in the array
2nd - the read string function//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0; // the progressive array index so that every bit is stored in another index of the array
if(serialAvailable()) { // this allows to execute the function only if there really is data in the serial buffer
while (serialAvailable()){ // we've cheched that there is data in the buffer...just loop through it untill there are bits in it
strArray[i] = serialRead(); // store in the array declared at the beginning at the specified index the first available bit of the serial buffer... when you read it the value will be automatically removed from the serial buffer
i++; //increment the index counter so that the next value will be stored in the next array index
} //return to the while loop
strArray[i] = 13 //optional: now the while loop is finished, hence there is no more data in the serial buffer. you can now eventually add by hand some bits to your array. this is how you create your own protocol. store a carriage return or end of line or whatever else.
}
}
this is a stripped down version of example number 4 but you can also refer to the previous examples, maybe example 1, for the most simple integration of the same concept in the loop function
3rd - usage in the loop function//read the serial port and create a string out of what you read
readSerialString(serInString);
- just put into your loop in the moment when you know that there might be info in your serial buffer
- pass it the name of the array declared at the beginning
and the trick is done!
right after that line your "serInString" will be loaded with all the needed data... (only if there actually was data in the serial buffer)
hope this helps..
if you tell us exactly what your string should look like I might be able to help in reusing the function for your pourposes.
b.