Serial-Port controlled I2C-connection

Hi together,

i have a system set up, where my arduino controlls a I2C-connection. That works very well as long as i have programmed the commands to send directly, like that:

#include <Wire.h>

int incomingByte = 0;
int add=0;
int com=0;
byte commands[20];

void setup()
{
  Wire.begin();                   // join i2c bus (address optional for master)
  Serial.begin(9600);
  pinMode(35,OUTPUT);
  digitalWrite(35,0);
}

void loop()
{
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if(incomingByte == 9) {
      digitalWrite(13,1);
      Wire.beginTransmission(60);     // transmit to device #4
      Wire.send(0x03);                // sends one byte  
      Wire.send(0x02);                // sends one byte  
      Wire.send(0x02);                // sends one byte  
      Wire.send(0x00);                // sends one byte  
      byte a = Wire.endTransmission();      // stop transmitting
      Serial.write(a);
      Serial.flush();
      digitalWrite(13,0);
    } 
  }
}

Now, i want to send the commands for the I2C-connection via serial connection (i can then controll the I2C via a Software on PC).
Therefore, i've set the following code up:

#include <Wire.h>

int incomingByte = 0;
int add=0;
int com=0;
byte commands[20];

void setup()
{
  Wire.begin();                   // join i2c bus (address optional for master)
  Serial.begin(9600);
  pinMode(35,OUTPUT);
  digitalWrite(35,0);
}

void loop()
{
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if(incomingByte == 9) {
      digitalWrite(13,1);
      int i=0;
      while(Serial.available()>0) {
        commands[i]=Serial.read();
        commands[i+1]=Serial.read();
        i+=2;
      }
      Wire.beginTransmission(60);     // transmit to device #4
      for(int a=0; a<5; a++) {
        Wire.send(commands[a]);
      }
      byte a = Wire.endTransmission();      // stop transmitting
      Serial.write(a);
      Serial.flush();
      digitalWrite(13,0);
    } 
  }
}

The system waits for a '9' zu receive, then it reads in the commands to send as long as no command is available any more (the PC-software makes sure that the array is not overran ;)). After this, the I2C-connection starts and sends out all the commands of the array. But unfortunately, my system does not respond as it should do... Can anyone image where's the difference between both codes?!

Thank you,
Max

      while(Serial.available()>0) {
        commands[i]=Serial.read();
        commands[i+1]=Serial.read();
        i+=2;
      }

Serial data transmission is relatively slow (especially at 9600 baud). You are testing that there is at least one byte available to read. If so, you read both of them. Do you see the problem with that?

What is sending the data to the Arduino? Binary or ascii data? The if(incomingByte == 9) statement implies binary. Is all the data binary?

Can anyone image where's the difference between both codes?!

In the first code, you are sending 4 bytes. In the second, you are sending 10 bytes - some of which might actually be valid.

You really need to terminate, somehow, the data being sent to the Arduino, and read serial data until that terminating byte arrives, before trying to Wire.send() the data.