Problem with Serial Communication per PHP

I build a 3x3 LED Matrix to controll it from the iPad (or so) via HTML and PHP/Apache on the Serialport of a computer.
I send following 12 digit long string with help of the serial.class.php to the arduino board: xxRACA334455
First 2 char are to decide which method in the sketch should be used, digit 3 to 6 are a placeholder, since later I want to give each LED in my Matrix a seperate color, and the last 6 char are the color (as hex)
I have a method implemented to calculate the int-value from the Hexcode. Since Im stuck to windows I can't let the arduino reply the recieved data via Serial, and I canT have the Serial Monitor from Arduino open, since that would block the communication of the PHP.

As I said, I send the String xxRACA334455 to the Arduino. With a seperate Port-Monitor I checked the COM Port and this String is really send, only the Arduino doesn't recieve it properly and changes the color sort of randomly.
When I am sending the same String per Arduinos SerialMonitor, it works quite fine and sets the proper color.
I know 12 char is quite a long command, but actually it should handel that, doesn't it? Are there any special circumstances I didn't know with php which I didn't took care of in my code?

Files are attached.

And here my schematic for the Matrix

It would be great if anyone could help me with this problem. Also apart from the Colorproblem, is the code for the LED-Powercontrolling ok?

serial.php (1.79 KB)

source_matrix.pde (9.09 KB)

void fetchCommand(){
  dat_ind = 0;
   while(Serial.available() > 0){
      if(dat_ind < 12) {
          inChar =; // 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;

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 “” 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.

Wohow, it's working... the arduino finally understands reliable what my PHP is talking about freudig rümhüpft
I used your input well and also i switched to serproxy for datatransfer rather then php.serial.class.php
At last i can concentrate an build the interface ^^

Thanks PaulS, and your right, I didn't understood or better I supressed my memorys with the char Array. Sadly I am not that well with Pointers, Adresses and Value Arrays ^^; I'm just a simple guy.

But in this case MSLENGTH was an Const Int (forgot to declare it as const, but had that function, since I wanted my commands to be only 12 Silebles long) but I prefer your idea a lot more, since i now can have commands with different length. makes it a lot easier.

FUN FUN FUN FUN, all the good things about Ki-Chan`s is I'm the only one.

FUN FUN FUN FUN, all the good things about Ki-Chan`s is I'm the only one.

Great sense of humor, Tigger.