 //Main loop
 void loop() 
 {
-
+char serial_out[100];
   if (Serial.available() || current_buffer[0]) {
     int num_read = 0;
     char serial_buffer[65];
   
     //num_read = Serial.readBytes(serial_buffer, 64); //Read up to 64 bytes
     
-    serial_buffer[0] = Serial.read();
-    num_read = 1;
+    int incomingByte = Serial.read();
+      if (incomingByte != -1){
+      serial_buffer[0] = incomingByte;
+      num_read = 1;
+    }
     serial_buffer[num_read] = 0;// terminate the string
     if (serial_buffer[0] || current_buffer[0]){
       strcat(current_buffer, serial_buffer);

                int pot_index, pot_min, pot_max, pot_home;
                sscanf(tmp_str + i + 2, "%d", &pot_index);
                potentiometers_control.get_limits(pot_index, &pot_min, &pot_max, &pot_home);
                Serial.write("PP"); // potentiometer parameters
                Serial.print(pot_index); 
                Serial.write(' ');
                Serial.print(pot_min);
                Serial.write(' ');
                Serial.print(pot_max);
                Serial.write(' ');
                Serial.print(pot_home);
                Serial.write('#'); 
                i += 4;
              }
              else{
                          // for all motors, unformated data
                int index;
                Serial.write("G ");
                for(index = 0; index < motors_control.num_motors; index++) {
                  Serial.write("Motor: ");
                  Serial.println(index);
                  for (byte j = 0 ; j < motors_control.sensors[index].count ; ++j) {
                    byte sensor_index = motors_control.sensors[index].sensors_array[j].index;
                    byte type = motors_control.sensors[index].sensors_array[j].type;
                    Serial.write("Sensor index: ");
                    Serial.print(sensor_index);
                    Serial.write(", Type: ");
                
                    if (POTENTIOMETER == type)
                      Serial.write("potentiometer, Value: ");
                      Serial.print(potentiometers_control.get_position(sensor_index));
                      Serial.write(", Is whitin limits: ");
                      Serial.println(potentiometers_control.isWithinLimits(sensor_index));
                    }
                }
                Serial.write("#");
                i += 2;
              }
        }
        else
         if (tmp_str[i] == 'T' || tmp_str[i] == 't'){// test connection
           Serial.write("T#");
         }
    }
    else

                Serial.println();
              #endif

             parse_and_execute_commands(current_buffer + i, j - i, serial_out);
             if (serial_out[0])
               Serial.write(serial_out);
             
                    
              // remove the current executed command
