void fetchCommand(){
dat_ind = 0;
while(Serial.available() > 0){
if(dat_ind < 12) {
inChar = Serial.read(); // Read a character
//Serial.print( inChar);
inData[dat_ind] = inChar; // Store i
//Serial.print( " "); Serial.println( inData[dat_ind]);
dat_ind++; // Increment where to write next
}
} //Serial.print(inData);
//Serial.println(" infetch");
inData[dat_ind] = '\0'; // Null terminate the string*/
}
Serial data transmission is slow. This function might be called after 3 characters have arrived. Those three characters will be put into the array, which will be NULL-terminated, and the function will then return.
void clearCommand(){
for(int i=0;i<13;i++){
inData[i] = 0;
}
dat_ind=0;
//Serial.flush();
}
One NULL is all that is needed to terminate processing of data in the array. When you write code to set all positions in the array to NULL, it indicates that you do not understand this simple concept.
Using Serial.flush() to throw away random amounts of data is rarely a good idea. That it is commented out, but not deleted, leads me to suspect that you will try to put it back some day. Don't!
if( dat_ind >= MSGLENGTH){
All capital letter names are, by convention, constants. MSGLENGTH is not.
You are only processing the data that was read from the serial port when a complete packet was received. That's good. A "complete packet" simply consists of 12 characters. That's bad. Very bad.
What you need to do is add start-of-packet and end-of-packet markers. Send something like "<xxRACA334455>" from the PHP script, instead of "xxRACA334455". Then, make fetchCommand throw away any characters until it read a start-of-packet marker (<).
Return true or false from fetchCommand (it's return type should be bool, not void). If the end of the serial data stream is encountered (Serial.available() returns 0) but the end-of-packet marker has not arrived, return false. If the end-of-packet marker arrives, break out of the while loop, and return true.
In the while loop, read each character available, and append it onto the end of the array, checking to make sure that there is room, unless the character is the end-of-packet marker. Of course, this means that the array must be NULL-terminated after every character is added, not once at the end. It also means that the index is not to be reset in fetchCommand.