serial fails after Wire.read()

I am using two slave ATmega328 to control several multiplexers and 2 sensors. A master ATmega328 sends multiplexer switch states to the slaves and requests sensor data via SPI. The master receives multiplexer switch states from Matlab via serial. The sensor data is also sent from the master via serial to Matlab for processing.

Matlab <---serial---> master <---SPI---> slaves

I am currently able to send the switch states from Matlab to the master and to the slaves. The multiplexers function as expected. Now I am working on getting the sensor data from the slaves to the master and then to Matlab. However, I have a problem when the master reads the sensor data. Here is the relevant part of my arduino code:

    Serial.println('a');
    while (Serial.available() > 0 && handshake != 'y'){
      handshake = Serial.read();
      if (handshake != 'y') {
        Serial.println('a');
      }      
    }
    handshake = 'n';

    // receive sensing code here
  Wire.requestFrom(1, 8);    // request 8 bytes from slave device #1
  
  String string;
  char c;

    while(Wire.available()){
      c = Wire.read();
      string = string +c;
    }

    //Serial.println(string);


    Serial.println('d');
    while (Serial.available() > 0 && handshake != 'y'){
      handshake = Serial.read();
      if (handshake != 'y') {
        Serial.println('d');
      }  
    }
    handshake = 'n';

The 'handshake' code is only there for debugging purposes. You can see that I have commented out the Serial.println(string) part. I am not looking to read that from serial in Matlab yet because of the following problem. When I comment out this:

    while(Wire.available()){
      c = Wire.read();
      string = string +c;
    }

and run my code, the multiplexers actuate correctly (and both handshakes are made). However, with the above uncommented, the code does not run to the second handshake. I don't get any errors in Arduino, but I do get a timeout error in Matlab while waiting for the second handshake from the master.

This makes me suspect something about Wire.read() is causing a problem.

I have tried the Wire.read loop in other ways but I get the same result. For example

  do
  {
    c = Wire.read();
    string = string + c;

  } while (Wire.available());

and

  for (i=0; i<8; i++){
    c = Wire.read();
    string = string + c;
  }

Any help is much appreciated
Thanks

General rule of thumb is not to use String.
I always use char arrays.

Partial bits of code is pointless in this instance.
.

Just a thought, you might be getting spurious clock/transitions from the multiplexer when you switch. Just a guess but that is where I would start looking. You can get a Saleae logic analyzer and watch the pins to see what is happening. That device will save you tons of time once you learn how to use it.

Thanks for the reply iee488

I sencerly do not believe that posting 480 lines of code will be productive for anyone. Here is a summary of what my code is doing

Global variables
void setup()
wire.begin()
serial.begin()
handshake code with Matlab
void loop()
function call for getting multiplexer switch states from Matlab (char array)
function call for getting multiplexer switch hold time from Matlab (single float number)
function call for reformatting data from matlab for transmission to slave MCU, sending multiplexer switch state commands to the slave MCU, and relaying slave sensor data to Matlab

The code that I posted is for relaying sensor data from the slave device to Matlab. I suspect Wire.read() is hanging, since the second handshake is never completed in Matlab. I can't image why any of my other code would matter. That said if you see room for code elsewhere to be relevant let me know, and I will post that code but not all of it as that would waste every ones time.

Thanks for the reply gilshultz.

I too suspect something like your suggestion to be the culprit here. I don't have access to a device like that. I may purchase one. In the meantime, lets assume that I have one and confirmed that I do have spurious clock transitions. What should i do next? Maybe I can implement that solution and see what happens.